From 7ceaaaf631a5e7315c179d36173f1d3c057c92cf Mon Sep 17 00:00:00 2001 From: Antonio Date: Sun, 30 Sep 2018 22:49:23 +0300 Subject: [PATCH 1/9] Initial commit --- CPP/data-structures/ResizableArray.h | 130 +++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 CPP/data-structures/ResizableArray.h diff --git a/CPP/data-structures/ResizableArray.h b/CPP/data-structures/ResizableArray.h new file mode 100644 index 0000000..18a0e90 --- /dev/null +++ b/CPP/data-structures/ResizableArray.h @@ -0,0 +1,130 @@ +/* + ResizableArray.h + Copyright Antonio Macovei +*/ + +#ifndef RESIZABLEARRAY_H_ +#define RESIZABLEARRAY_H_ + +template +class ResizableArray { + private: + int defaultCapacity = 1; + + int numElements; + int maxCapacity; + T *data; + + void resizeArray() { + maxCapacity = maxCapacity + 1; + T *temp; + temp = new T[maxCapacity]; + for (int i = 0; i < numElements; i++) { + temp[i] = data[i]; + } + delete[] data; + data = temp; + } + + // Shift elements one position to the right + void shift_elements_right(int start) { + for (int i = numElements; i > start; i--) { + data[i] = data[i - 1]; + } + } + + // Shift elements one position to the left + void shift_elements_left(int stop) { + for (int i = satop; i < numElements; i++) { + data[i] = data[i + 1]; + } + } + + public: + // Constructor + ResizableArray() { + numElements = 0; + maxCapacity = defaultCapacity; + + data = new T[maxCapacity]; + } + + // Destructor + ~ResizableArray() { + delete[] data; + } + + /** + * Adds the specified element at the end of the array. + * + * @param element Element to be added at the end of the array. + */ + void add_last(T element) { + if (numElements == maxCapacity) { + resizeArray(); + } + data[numElements] = element; + numElements++; + } + + /** + * Adds the specified element at the beginning of the array. + * + * @param element Elment to be added at the beginning of the array. + */ + void add_first(T element) { + if (numElements == maxCapacity) { + resizeArray(); + } + numElements++; + shift_elements_right(0); + data[0] = element; + } + + /** + * Removes and returns the last element of the array. + * + * @return Value of the last element stored in the array. + */ + void remove_last() { + if (numElements == 0) return; + numElements--; + } + + /** + * Removes and returns the first element of the array. + * + * @return Value of the first element stored in the array. + */ + void remove_first() { + if (numElements == 0) return; + shift_elements_left(0); + numElements--; + } + + /** + * Checks if the array contains any elements. + * + * @return True if the array contains no elements, False otherwise. + */ + bool is_empty() { + if (numElements == 0) + return true; + return false; + } + + /** + * Returns the number of elements in the array. + * + * @return The number of elements stored in the array. + */ + int size() { + return numElements; + } + + // Getters + T *get_data() { + return data; + } +}; +#endif // RESIZABLEARRAY_H_ \ No newline at end of file From 6dc315667502750c6fc2c4183ed37c30558f100f Mon Sep 17 00:00:00 2001 From: Antonio Date: Sun, 30 Sep 2018 22:54:22 +0300 Subject: [PATCH 2/9] Added me in CONTRIBUTORS.md --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index c6d76e6..b035b7b 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,3 +1,4 @@ # Add yourself in this file if you are a contributor for Hacktoberfest 2018 - Radu Stochitoiu +- Antonio Macovei \ No newline at end of file From 21463c5e6b1db30f70811130f339ab9d8b166153 Mon Sep 17 00:00:00 2001 From: Antonio Date: Sun, 30 Sep 2018 23:01:40 +0300 Subject: [PATCH 3/9] Added DoublyLinkedList.h --- CPP/data-structures/DoublyLinkedList.h | 169 +++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 CPP/data-structures/DoublyLinkedList.h diff --git a/CPP/data-structures/DoublyLinkedList.h b/CPP/data-structures/DoublyLinkedList.h new file mode 100644 index 0000000..94cef57 --- /dev/null +++ b/CPP/data-structures/DoublyLinkedList.h @@ -0,0 +1,169 @@ +/* + DoublyLinkedList.h + Copyright Antonio Macovei +*/ + +#ifndef DOUBLYLINKEDLIST_H_ +#define DOUBLYLINKEDLIST_H_ + +template +struct Node { + T data; + Node *next; + Node *prev; + + explicit Node(T data) { + this->data = data; + this->next = nullptr; + this->prev = nullptr; + } +}; + +template +class DoublyLinkedList { + private: + Node *head; + Node *tail; + int numElements; + + // Returns the pos'th node in the list. + Node *goToPos(int pos) { + int count = 0; + Node *it = head; + + while (count != pos) { + it = it->next; + count++; + } + return it; + } + + public: + // Constructor + DoublyLinkedList() { + head = nullptr; + tail = nullptr; + numElements = 0; + } + + // Destructor + ~DoublyLinkedList() { + while (head != nullptr) { + Node *temp = head; + head = head->next; + delete temp; + } + } + + /** + * Adds a new node at the beginning of the list. + * + * @param data Data to be added at the end of the list. + */ + void add_first(T data) { + if (head == nullptr) { + head = new Node(data); + head->next = nullptr; + head->prev = nullptr; + tail = head; + } else { + Node *new_node; + new_node = new Node(data); + new_node->next = head; + new_node->prev = nullptr; + head->prev = new_node; + head = new_node; + } + numElements++; + } + + /** + * Adds a new node at the end of the list. + * + * @param data Data to be added at the end of the list. + */ + void add_last(T data) { + if (head == nullptr) { + head = new Node(data); + head->next = nullptr; + head->prev = nullptr; + tail = head; + } else { + Node *new_node; + new_node = new Node(data); + new_node->next = nullptr; + new_node->prev = tail; + tail->next = new_node; + tail = new_node; + } + numElements++; + } + + /** + * Removes the last node of the list. + */ + void remove_last() { + if (numElements == 0) return; + if (numElements == 1) { + delete tail; + tail = nullptr; + head = nullptr; + numElements = 0; + } else { + Node *new_tail = tail->prev; + tail->prev->next = nullptr; + delete tail; + tail = new_tail; + numElements--; + } + } + + /** + * Removes the first node of the list. + */ + void remove_first() { + if (numElements == 0) return; + if (numElements == 1) { + delete head; + tail = nullptr; + head = nullptr; + numElements = 0; + } else { + Node *new_head = head->next; + head->next->prev = nullptr; + delete head; + head = new_head; + numElements--; + } + } + + /** + * Check if the list contains any elements. + * + * @return True if the list contains no elements, False otherwise. + */ + bool is_empty() { + if (numElements == 0) + return true; + return false; + } + + /** + * Get the number of nodes in the list. + * + * @return The number of nodes stored in the list. + */ + int size() { + return numElements; + } + + // Getters & Setters + Node *get_head() { + return head; + } + + Node *get_tail() { + return tail; + } +}; +#endif // DOUBLYLINKEDLIST_H_ \ No newline at end of file From ffb872aa6d01c32461deafc1cdf6be01ee20f4a3 Mon Sep 17 00:00:00 2001 From: Antonio Date: Sun, 30 Sep 2018 23:04:48 +0300 Subject: [PATCH 4/9] Added Deque.h --- CPP/data-structures/Deque.h | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 CPP/data-structures/Deque.h diff --git a/CPP/data-structures/Deque.h b/CPP/data-structures/Deque.h new file mode 100644 index 0000000..350e86a --- /dev/null +++ b/CPP/data-structures/Deque.h @@ -0,0 +1,51 @@ +/* + Deque.h + Copyright Antonio Macovei +*/ + +#ifndef DEQUE_H_ +#define DEQUE_H_ + +#include "DoublyLinkedList.h" + +template +class Deque { + private: + DoublyLinkedList deque; + + public: + void push_front(T data) { + deque.add_first(data); + } + + void push_back(T data) { + deque.add_last(data); + } + + void pop_front() { + deque.remove_first(); + } + + void pop_back() { + deque.remove_last(); + } + + T front() { + if (!deque.is_empty()) + return deque.get_head()->data; + } + + T back() { + if (!deque.is_empty()) + return deque.get_tail()->data; + } + + int size() { + return deque.size(); + } + + bool empty() { + return deque.is_empty(); + } +}; +#endif // DEQUE_H_ \ No newline at end of file From 26a74e4a94d80d524becf7c1c1132936b271eb80 Mon Sep 17 00:00:00 2001 From: Antonio Date: Sun, 30 Sep 2018 23:09:18 +0300 Subject: [PATCH 5/9] Added Hashtable.h --- CPP/data-structures/Hashtable.h | 95 +++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 CPP/data-structures/Hashtable.h diff --git a/CPP/data-structures/Hashtable.h b/CPP/data-structures/Hashtable.h new file mode 100644 index 0000000..8dd1cc0 --- /dev/null +++ b/CPP/data-structures/Hashtable.h @@ -0,0 +1,95 @@ +/** + * hashtable.h + * Copyright Antonio Macovei + */ + +#ifndef HASHTABLE_H_ +#define HASHTABLE_H_ +#include + +#define TRESHOLD 0.75 + +template struct elem_info { + Tkey key; + Tvalue value; +}; + +template class Hashtable { + private: + std::list > * H; // pointer to buckets + int HMAX; // number of buckets + unsigned int (*hash)(Tkey); // pointer to hash function + unsigned int size; // number of elements in hashtable + + public: + // Constructor + Hashtable(int hmax, unsigned int (*h)(Tkey)) { + HMAX = hmax; + H = new std::list >[hmax]; + hash = h; + } + + // Destructor + ~Hashtable() { + delete[] H; + } + + /** + * Adds an element to the hashtable + * + * @param key The key of the element to be added + * @param value The value of the element to be added + */ + void put(Tkey key, Tvalue value) { + unsigned int index = hash(key) % HMAX; + bool found = false; + for(auto it : H[index]) { + if(it.key == key) { + it.value = value; + found = true; + } + } + if(!found) { + elem_info new_elem; + new_elem.key = key; + new_elem.value = value; + H[index].push_back(new_elem); + } + } + + /** + * Adds an element to the hashtable + * + * @param key The key of the searched element + * @return the value found with the key + */ + Tvalue get(Tkey key) { + unsigned int index = hash(key) % HMAX; + for(auto it : H[index]) { + if(it.key == key) { + return it.value; + } + } + return Tvalue(); + } + + /** + * Gets the hashtable size. + * + * @return Number of elements in the hashtable. + */ + int get_size() { + return size; + } + + /** + * Gets the hastable capacity. + * + * @return Number of buckets in the hashtable. + */ + int get_capacity() { + return HMAX; + } +}; + +#endif // HASHTABLE_H__ \ No newline at end of file From 8f6aaf57707773c3f13b5e5365b2dd9e5415a259 Mon Sep 17 00:00:00 2001 From: Antonio Date: Sun, 30 Sep 2018 23:15:35 +0300 Subject: [PATCH 6/9] Deleted unnecessary file --- CPP/data-structures/Hashtable.h | 95 --------------------------------- 1 file changed, 95 deletions(-) delete mode 100644 CPP/data-structures/Hashtable.h diff --git a/CPP/data-structures/Hashtable.h b/CPP/data-structures/Hashtable.h deleted file mode 100644 index 8dd1cc0..0000000 --- a/CPP/data-structures/Hashtable.h +++ /dev/null @@ -1,95 +0,0 @@ -/** - * hashtable.h - * Copyright Antonio Macovei - */ - -#ifndef HASHTABLE_H_ -#define HASHTABLE_H_ -#include - -#define TRESHOLD 0.75 - -template struct elem_info { - Tkey key; - Tvalue value; -}; - -template class Hashtable { - private: - std::list > * H; // pointer to buckets - int HMAX; // number of buckets - unsigned int (*hash)(Tkey); // pointer to hash function - unsigned int size; // number of elements in hashtable - - public: - // Constructor - Hashtable(int hmax, unsigned int (*h)(Tkey)) { - HMAX = hmax; - H = new std::list >[hmax]; - hash = h; - } - - // Destructor - ~Hashtable() { - delete[] H; - } - - /** - * Adds an element to the hashtable - * - * @param key The key of the element to be added - * @param value The value of the element to be added - */ - void put(Tkey key, Tvalue value) { - unsigned int index = hash(key) % HMAX; - bool found = false; - for(auto it : H[index]) { - if(it.key == key) { - it.value = value; - found = true; - } - } - if(!found) { - elem_info new_elem; - new_elem.key = key; - new_elem.value = value; - H[index].push_back(new_elem); - } - } - - /** - * Adds an element to the hashtable - * - * @param key The key of the searched element - * @return the value found with the key - */ - Tvalue get(Tkey key) { - unsigned int index = hash(key) % HMAX; - for(auto it : H[index]) { - if(it.key == key) { - return it.value; - } - } - return Tvalue(); - } - - /** - * Gets the hashtable size. - * - * @return Number of elements in the hashtable. - */ - int get_size() { - return size; - } - - /** - * Gets the hastable capacity. - * - * @return Number of buckets in the hashtable. - */ - int get_capacity() { - return HMAX; - } -}; - -#endif // HASHTABLE_H__ \ No newline at end of file From 38b244847577064546e263a5328fb143ce0874e2 Mon Sep 17 00:00:00 2001 From: Antonio Date: Sun, 30 Sep 2018 23:16:24 +0300 Subject: [PATCH 7/9] Added Hashtable.h --- CPP/data-structures/Hashtable.h | 95 +++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 CPP/data-structures/Hashtable.h diff --git a/CPP/data-structures/Hashtable.h b/CPP/data-structures/Hashtable.h new file mode 100644 index 0000000..8dd1cc0 --- /dev/null +++ b/CPP/data-structures/Hashtable.h @@ -0,0 +1,95 @@ +/** + * hashtable.h + * Copyright Antonio Macovei + */ + +#ifndef HASHTABLE_H_ +#define HASHTABLE_H_ +#include + +#define TRESHOLD 0.75 + +template struct elem_info { + Tkey key; + Tvalue value; +}; + +template class Hashtable { + private: + std::list > * H; // pointer to buckets + int HMAX; // number of buckets + unsigned int (*hash)(Tkey); // pointer to hash function + unsigned int size; // number of elements in hashtable + + public: + // Constructor + Hashtable(int hmax, unsigned int (*h)(Tkey)) { + HMAX = hmax; + H = new std::list >[hmax]; + hash = h; + } + + // Destructor + ~Hashtable() { + delete[] H; + } + + /** + * Adds an element to the hashtable + * + * @param key The key of the element to be added + * @param value The value of the element to be added + */ + void put(Tkey key, Tvalue value) { + unsigned int index = hash(key) % HMAX; + bool found = false; + for(auto it : H[index]) { + if(it.key == key) { + it.value = value; + found = true; + } + } + if(!found) { + elem_info new_elem; + new_elem.key = key; + new_elem.value = value; + H[index].push_back(new_elem); + } + } + + /** + * Adds an element to the hashtable + * + * @param key The key of the searched element + * @return the value found with the key + */ + Tvalue get(Tkey key) { + unsigned int index = hash(key) % HMAX; + for(auto it : H[index]) { + if(it.key == key) { + return it.value; + } + } + return Tvalue(); + } + + /** + * Gets the hashtable size. + * + * @return Number of elements in the hashtable. + */ + int get_size() { + return size; + } + + /** + * Gets the hastable capacity. + * + * @return Number of buckets in the hashtable. + */ + int get_capacity() { + return HMAX; + } +}; + +#endif // HASHTABLE_H__ \ No newline at end of file From fb0d9949655ebb5ed99854d3acaaea3bf20e4131 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 1 Oct 2018 12:25:11 +0300 Subject: [PATCH 8/9] Fixed indentation and comments --- CPP/data-structures/Hashtable.h | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/CPP/data-structures/Hashtable.h b/CPP/data-structures/Hashtable.h index 8dd1cc0..46a6858 100644 --- a/CPP/data-structures/Hashtable.h +++ b/CPP/data-structures/Hashtable.h @@ -1,8 +1,3 @@ -/** - * hashtable.h - * Copyright Antonio Macovei - */ - #ifndef HASHTABLE_H_ #define HASHTABLE_H_ #include @@ -22,24 +17,17 @@ template class Hashtable { unsigned int size; // number of elements in hashtable public: - // Constructor Hashtable(int hmax, unsigned int (*h)(Tkey)) { HMAX = hmax; H = new std::list >[hmax]; hash = h; } - // Destructor ~Hashtable() { delete[] H; } - /** - * Adds an element to the hashtable - * - * @param key The key of the element to be added - * @param value The value of the element to be added - */ + // Adds an element to the hashtable void put(Tkey key, Tvalue value) { unsigned int index = hash(key) % HMAX; bool found = false; @@ -57,12 +45,7 @@ template class Hashtable { } } - /** - * Adds an element to the hashtable - * - * @param key The key of the searched element - * @return the value found with the key - */ + // Gets an element from the hashtable Tvalue get(Tkey key) { unsigned int index = hash(key) % HMAX; for(auto it : H[index]) { @@ -73,20 +56,12 @@ template class Hashtable { return Tvalue(); } - /** - * Gets the hashtable size. - * - * @return Number of elements in the hashtable. - */ + // Gets the hashtable size. int get_size() { return size; } - /** - * Gets the hastable capacity. - * - * @return Number of buckets in the hashtable. - */ + // Gets the hastable capacity. int get_capacity() { return HMAX; } From f911921d48f984b1fc5101194614a4637cf3c106 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 1 Oct 2018 12:31:42 +0300 Subject: [PATCH 9/9] Fixed indentation v2 --- CPP/data-structures/Hashtable.h | 90 ++++++++++++++++----------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/CPP/data-structures/Hashtable.h b/CPP/data-structures/Hashtable.h index 46a6858..578e3eb 100644 --- a/CPP/data-structures/Hashtable.h +++ b/CPP/data-structures/Hashtable.h @@ -10,61 +10,61 @@ template struct elem_info { }; template class Hashtable { - private: - std::list > * H; // pointer to buckets - int HMAX; // number of buckets - unsigned int (*hash)(Tkey); // pointer to hash function - unsigned int size; // number of elements in hashtable +private: + std::list > * H; // pointer to buckets + int HMAX; // number of buckets + unsigned int (*hash)(Tkey); // pointer to hash function + unsigned int size; // number of elements in hashtable - public: - Hashtable(int hmax, unsigned int (*h)(Tkey)) { - HMAX = hmax; - H = new std::list >[hmax]; - hash = h; - } +public: + Hashtable(int hmax, unsigned int (*h)(Tkey)) { + HMAX = hmax; + H = new std::list >[hmax]; + hash = h; + } - ~Hashtable() { - delete[] H; - } + ~Hashtable() { + delete[] H; + } - // Adds an element to the hashtable - void put(Tkey key, Tvalue value) { - unsigned int index = hash(key) % HMAX; - bool found = false; - for(auto it : H[index]) { - if(it.key == key) { - it.value = value; - found = true; - } - } - if(!found) { - elem_info new_elem; - new_elem.key = key; - new_elem.value = value; - H[index].push_back(new_elem); + // Adds an element to the hashtable + void put(Tkey key, Tvalue value) { + unsigned int index = hash(key) % HMAX; + bool found = false; + for(auto it : H[index]) { + if(it.key == key) { + it.value = value; + found = true; } } + if(!found) { + elem_info new_elem; + new_elem.key = key; + new_elem.value = value; + H[index].push_back(new_elem); + } + } - // Gets an element from the hashtable - Tvalue get(Tkey key) { - unsigned int index = hash(key) % HMAX; - for(auto it : H[index]) { - if(it.key == key) { - return it.value; - } + // Gets an element from the hashtable + Tvalue get(Tkey key) { + unsigned int index = hash(key) % HMAX; + for(auto it : H[index]) { + if(it.key == key) { + return it.value; } - return Tvalue(); } + return Tvalue(); + } - // Gets the hashtable size. - int get_size() { - return size; - } + // Gets the hashtable size. + int get_size() { + return size; + } - // Gets the hastable capacity. - int get_capacity() { - return HMAX; - } + // Gets the hastable capacity. + int get_capacity() { + return HMAX; + } }; #endif // HASHTABLE_H__ \ No newline at end of file