diff --git a/LinearList/arrayList.cpp b/LinearList/arrayList.cpp new file mode 100644 index 0000000..dc0ec46 --- /dev/null +++ b/LinearList/arrayList.cpp @@ -0,0 +1,133 @@ +#include +#include +#include "linearList.h" +#include "arrayList.h" + +template +int search(const T& x, const T* arr, int n, int length) { + if (n >= length) + return -1; + if (arr[n] == x) + return n; + return search(x, arr, n + 1 , length); +} + +template +arrayList::arrayList(int initialCapacity) { + if (initialCapacity < 1) { + std::cout << "Initial Capacity Must Be > 0" << std::endl; + //¿¼ÂÇthrow exception + } + element = new T[initialCapacity]; + listSize = 0; +} + +template +arrayList::arrayList(const arrayList& theList) { + //É±´ + arrayLength = theList.arrayLength; + listSize = theList.listSize; + element = new T[arrayLength]; + for (int i = 0; i < listSize; i++) { + element[i] = theList.element[i]; + } +} + +template +arrayList::~arrayList() { + delete [] element; +} + +template +int arrayList::capacity() { + return arrayLength; +} + +template +bool arrayList::empty() const { + //If empty, return true; + if (listSize == 0) return true; + return false; +} + +template +int arrayList::size() const { + //Return Array Size + return listSize; +} + +template +T& arrayList::get(int theIndex) const { + // Return Arr[theIndex] + if (theIndex < 0 || theIndex >= listSize) { + throw std::out_of_range("Index out of bounds: " + std::to_string(theIndex)); + } + return element[theIndex]; +} + +template +int arrayList::indexOf(const T& theElement) const { + //Return the index of "theElement" first appeared + return search(theElement, element, 0, listSize); +} + +template +T* arrayList::changeLength(T* x, int oldlength, int newlength) { + //Arr length altered + if (newlength < 0) { + throw std::invalid_argument("newlength must be non-negative: " + std::to_string(newlength)); + } + T* tmp = new T[newlength]; + int min = std::min(newlength, oldlength); + for (int i = 0; i < min; i++) { + tmp[i] = x[i]; + } + delete[] x; + return tmp; +} + + +template +void arrayList::erase(int theIndex) { + //Erase Arr[theIndex] , remember to move the rest + if (theIndex < 0 || theIndex >= listSize) { + throw std::out_of_range("Index out of bounds: " + std::to_string(theIndex)); + } + for (int i = theIndex; i < listSize - 1; i++) { + element[i] = element[i + 1]; + } + listSize--; + element[listSize] = T(); +} + +template +void arrayList::insert(int theIndex, const T& theElement) { + //Insert theElement to place Arr[theIndex] + if (theIndex < 0 || theIndex > listSize) { + throw std::out_of_range("Index out of bounds: " + std::to_string(theIndex)); + } + if (listSize == arrayLength) { + element = changeLength(element, arrayLength, arrayLength + 1); + + arrayLength++; + } + for (int i = listSize - 1; i >= theIndex; --i) { + element[i + 1] = element[i]; + } + element[theIndex] = theElement; + listSize++; +} + +template +void arrayList::output() const { + // output into ostream + for (int i = 0; i < listSize; i++) { + std::cout << element[i] << " "; + } + std::cout << std::endl; +} + +template class arrayList; +template class arrayList; +template class arrayList; + diff --git a/LinearList/arrayList.h b/LinearList/arrayList.h new file mode 100644 index 0000000..630a5c0 --- /dev/null +++ b/LinearList/arrayList.h @@ -0,0 +1,28 @@ +#ifndef ARRAY_LIST +#define ARRAY_LIST + +#include +#include "linearList.h" + +template +class arrayList : public linearList { +public: + arrayList(int initialCapacity = 10); + arrayList(const arrayList& x); + ~arrayList(); + int capacity(); + bool empty() const;//If empty, return 0; + int size() const; //Return Array Size + virtual T& get(int theIndex) const override; // Return Arr[theIndex] + virtual int indexOf(const T& theElement) const override; //Return the index of "theElement" first appeared + virtual void erase(int theIndex) override; //Erase Arr[theIndex] + virtual T* changeLength(T* x, int oldlength, int newlength) override; + virtual void insert(int theIndex, const T& theElement) override; //Insert theElement to place Arr[theIndex] + virtual void output() const override; //cout +private: + T* element; + int arrayLength; + int listSize; +}; +#endif + diff --git a/LinearList/linearList.h b/LinearList/linearList.h new file mode 100644 index 0000000..bb4c726 --- /dev/null +++ b/LinearList/linearList.h @@ -0,0 +1,20 @@ +#ifndef LINEAR_LIST +#define LINEAR_LIST +#include + +template +class linearList { +public: + virtual ~linearList() {}; + virtual bool empty() const = 0; //If empty, return 0; + virtual int size() const = 0; //Return Array Size + virtual T& get(int theIndex) const = 0; // Return Arr[theIndex] + virtual int indexOf(const T& theElement) const = 0; //Return the index of "theElement" first appeared + virtual void erase(int theIndex) = 0; //Erase Arr[theIndex] + virtual T* changeLength(T* x, int oldlength, int newlength) = 0; + virtual void insert(int theIndex, const T& theElement) = 0; //Insert theElement to place Arr[theIndex] + virtual void output() const = 0; //cout +}; + + +#endif \ No newline at end of file diff --git a/LinearList/main.cpp b/LinearList/main.cpp new file mode 100644 index 0000000..6976fac --- /dev/null +++ b/LinearList/main.cpp @@ -0,0 +1,49 @@ +#include +#include "linearList.h" +#include "arrayList.h" +/* + NEEDED TEST: +ok int capacity(); +ok bool empty() const;//If empty, return 0; +ok int size() const; //Return Array Size +ok virtual T& get(int theIndex) const override; // Return Arr[theIndex] + virtual int indexOf(const T & theElement) const override; //Return the index of "theElement" first appeared +ok virtual void erase(int theIndex) override; //Erase Arr[theIndex] +ok virtual T* changeLength(T * x, int oldlength, int newlength) override; +ok virtual void insert(int theIndex, const T & theElement) override; //Insert theElement to place Arr[theIndex] +ok virtual void output(std::ostream & out) const override; //cout +*/ +int main() { + arrayList test(10); + std::cout << "ArrayCapacity: " << test.capacity() << std::endl; + std::cout << "Is Empty: " << test.empty() << std::endl; + for (int i = 0; i < 10; i++) { + test.insert(i, i + 1); + } + test.output(); + std::cout << test.get(4) << std::endl ; //output 5 + test.insert(10, 11); // 1 2 3 4 5 6 7 8 9 10 11 + test.output(); + std::cout << test.size() << std::endl; + test.erase(0); // 2 3 4 5 6 7 8 9 10 11 + test.output(); + + test.insert(5, 3); + test.output(); + std::cout << test.indexOf(3) << std::endl; + return 0; +} + +/* +Output is down below + +ArrayCapacity: 0 +Is Empty: 1 +1 2 3 4 5 6 7 8 9 10 +5 +1 2 3 4 5 6 7 8 9 10 11 +11 +2 3 4 5 6 7 8 9 10 11 +2 3 4 5 6 3 7 8 9 10 11 +1 +*/ \ No newline at end of file