From 7ceaaaf631a5e7315c179d36173f1d3c057c92cf Mon Sep 17 00:00:00 2001 From: Antonio Date: Sun, 30 Sep 2018 22:49:23 +0300 Subject: [PATCH 1/3] 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 12b8fcb05ec2af9f9c0e28306aa447554a10d733 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 1 Oct 2018 12:17:36 +0300 Subject: [PATCH 2/3] Fixed indentation, comments, remove_first and remove_last --- CPP/data-structures/ResizableArray.h | 202 +++++++++++---------------- 1 file changed, 85 insertions(+), 117 deletions(-) diff --git a/CPP/data-structures/ResizableArray.h b/CPP/data-structures/ResizableArray.h index 18a0e90..b87e60e 100644 --- a/CPP/data-structures/ResizableArray.h +++ b/CPP/data-structures/ResizableArray.h @@ -1,130 +1,98 @@ -/* - 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; +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]; } - - // 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]; - } + 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]; - } + // 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]; + } + +public: + ResizableArray() { + numElements = 0; + maxCapacity = defaultCapacity; + data = new T[maxCapacity]; + } + + ~ResizableArray() { + delete[] data; + } + + // Adds the specified element at the end of the array. + void add_last(T element) { + if (numElements == maxCapacity) { + resizeArray(); } - - // 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; + data[numElements] = element; + numElements++; + } + + // Adds the specified element 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. + void remove_last() { + if (numElements == 0) return; + numElements--; + } + + // Removes and returns the first element of the array. + void remove_first() { + if (numElements == 0) return; + shift_elements_left(0); + numElements--; + } + + // Checks if the array contains any elements. + bool is_empty() { + if (numElements == 0) + return true; + return false; + } + + // Returns the number of elements in the array. + int size() { + return numElements; + } + + // Getters + T *get_data() { + return data; + } }; #endif // RESIZABLEARRAY_H_ \ No newline at end of file From db4d549d5aa3ee0e108adb558bf454d1c14b29bb Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 1 Oct 2018 12:20:55 +0300 Subject: [PATCH 3/3] Fixed indentation, comments, remove_first and remove_last v2 --- CPP/data-structures/ResizableArray.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/CPP/data-structures/ResizableArray.h b/CPP/data-structures/ResizableArray.h index b87e60e..fc435d5 100644 --- a/CPP/data-structures/ResizableArray.h +++ b/CPP/data-structures/ResizableArray.h @@ -66,16 +66,20 @@ class ResizableArray { } // Removes and returns the last element of the array. - void remove_last() { - if (numElements == 0) return; + T remove_last() { + if (numElements == 0) return T; + T elem = data[numElements]; numElements--; + return elem; } // Removes and returns the first element of the array. - void remove_first() { - if (numElements == 0) return; + T remove_first() { + if (numElements == 0) return T; + T elem = data[0]; shift_elements_left(0); numElements--; + return elem; } // Checks if the array contains any elements. @@ -90,7 +94,6 @@ class ResizableArray { return numElements; } - // Getters T *get_data() { return data; }