From ce3f273487c65799dbc8be3ccb649d1c61ac3fbb Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Sat, 19 Jul 2025 21:34:14 +0800 Subject: [PATCH 1/2] New --- LinearList/{ => as-Array}/arrayList.cpp | 0 LinearList/{ => as-Array}/arrayList.h | 0 LinearList/{ => as-Array}/linearList.h | 0 LinearList/{ => as-Array}/main.cpp | 0 LinearList/as-List/linearList.h | 20 ++++++++++++++++++++ 5 files changed, 20 insertions(+) rename LinearList/{ => as-Array}/arrayList.cpp (100%) rename LinearList/{ => as-Array}/arrayList.h (100%) rename LinearList/{ => as-Array}/linearList.h (100%) rename LinearList/{ => as-Array}/main.cpp (100%) create mode 100644 LinearList/as-List/linearList.h diff --git a/LinearList/arrayList.cpp b/LinearList/as-Array/arrayList.cpp similarity index 100% rename from LinearList/arrayList.cpp rename to LinearList/as-Array/arrayList.cpp diff --git a/LinearList/arrayList.h b/LinearList/as-Array/arrayList.h similarity index 100% rename from LinearList/arrayList.h rename to LinearList/as-Array/arrayList.h diff --git a/LinearList/linearList.h b/LinearList/as-Array/linearList.h similarity index 100% rename from LinearList/linearList.h rename to LinearList/as-Array/linearList.h diff --git a/LinearList/main.cpp b/LinearList/as-Array/main.cpp similarity index 100% rename from LinearList/main.cpp rename to LinearList/as-Array/main.cpp diff --git a/LinearList/as-List/linearList.h b/LinearList/as-List/linearList.h new file mode 100644 index 0000000..bb4c726 --- /dev/null +++ b/LinearList/as-List/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 From b4d8a78c47cbcd6b3c0e7c9e91f182aafb9765ba Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Sun, 20 Jul 2025 11:22:32 +0800 Subject: [PATCH 2/2] as-Node --- LinearList/as-List/chain.h | 167 ++++++++++++++++++++++++++++++++ LinearList/as-List/chainNode.h | 22 +++++ LinearList/as-List/linearList.h | 1 - LinearList/as-List/main.cpp | 50 ++++++++++ 4 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 LinearList/as-List/chain.h create mode 100644 LinearList/as-List/chainNode.h create mode 100644 LinearList/as-List/main.cpp diff --git a/LinearList/as-List/chain.h b/LinearList/as-List/chain.h new file mode 100644 index 0000000..2101482 --- /dev/null +++ b/LinearList/as-List/chain.h @@ -0,0 +1,167 @@ +#pragma once +#include +#include +#include "chainNode.h" +#include "linearList.h" + +template +class chain : public linearList { +public: + chain(int initialCapacity = 10); + chain(const chain& x); + ~chain(); + virtual bool empty() const; //If empty, return 0; + virtual int size() const; //Return Array Size + virtual T& get(int theIndex) const; // Return Arr[theIndex] + virtual int indexOf(const T& theElement) const; //Return the index of "theElement" first appeared + virtual void erase(int theIndex); //Erase Arr[theIndex] + virtual void insert(int theIndex, const T& theElement); //Insert theElement to place Arr[theIndex] + virtual void output() const; //cout +protected: + chainNode* first; + int listSize; + int capacity; +}; + +template +chain::chain(int initialCapacity) { + capacity = initialCapacity; +} + +template +chain::chain(const chain& x) { + //**********Copy The WHOLE Chain********* + capacity = x.capacity; + listSize = x.listSize; + //**********Deep Copy: Copy Every Node with figures******** + + if (x.first == nullptr) { + first = nullptr; + return; + } + + chainNode* source = x.first; + first = new chainNode(source->element);//equals to chainNode(source->element, nullptr) + chainNode* target = first; + + source = source->next; + + while (source != nullptr) { + target->next = new chainNode(source->element); + target = target->next; + source = source->next; + } +} + +template +chain::~chain() { + chainNode* tmp = first; + while (tmp != nullptr) { + tmp = tmp->next; + delete first; + first = tmp; + } +} + +template +bool chain::empty() const { + //If empty, return 0; + return listSize == 0; +} + +template +int chain::size() const +{ + //Return Array Size + return listSize; +} + +template +T& chain::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)); + } + + chainNode* tmp = first; + for (int i = 1; i <= theIndex; i++) { + tmp = tmp->next; + } + return tmp->element; +} + +template +int chain::indexOf(const T& theElement) const +{ + //Return the index of "theElement" first appeared + chainNode* tmp = first; + for (int i = 0; i < listSize; i++) { + if (tmp->element == theElement) { + return i;//i = arr - 1 + } + tmp = tmp->next; + } + return -1; +} + +template +void chain::erase(int theIndex) +{ + //Erase Arr[theIndex] + if (theIndex < 0 || theIndex >= listSize) { + throw std::out_of_range("Index out of bounds: " + std::to_string(theIndex)); + } + + chainNode* deleteNode; + + if (theIndex == 0) { + deleteNode = first; + first = first->next; + } + else { + chainNode* prev = first; + for (int i = 0; i < theIndex - 1; i++) { + prev = prev->next; + } + deleteNode = prev->next; + prev->next = deleteNode->next; + } + delete deleteNode; + listSize--; +} + + +template +void chain::insert(int theIndex, const T& theElement) +{ + //Insert theElement to place Arr[theIndex] + chainNode* arr = first; + if (theIndex < 0) throw std::out_of_range("index out of range"); + + if (theIndex == 0) { + // ²åÈ뵽ͷ²¿ + first = new chainNode(theElement, first); + } + else { + for (int i = 0; i < theIndex - 1; i++) { + arr = arr->next; + } + chainNode* insert = new chainNode(theElement); + insert->next = arr->next; + arr->next = insert; + listSize++; + } +} + +template +void chain::output() const +{ + //cout + chainNode* tmp = first; + for (int i = 0; i < listSize; i++) { + std::cout << tmp->element << " "; + tmp = tmp->next; + } + std::cout << std::endl; +} diff --git a/LinearList/as-List/chainNode.h b/LinearList/as-List/chainNode.h new file mode 100644 index 0000000..001fcbe --- /dev/null +++ b/LinearList/as-List/chainNode.h @@ -0,0 +1,22 @@ +#pragma once +template +class chainNode { +public: + T element; + chainNode* next; + //C11 can write like : std::unique_ptr> next; + +public: + chainNode(); + chainNode(const T& x, chainNode* next = nullptr); + +}; + +template +chainNode::chainNode() { + this->next = nullptr; +} + +template +chainNode::chainNode(const T& x, chainNode* next) : element(x), next(next) { +} diff --git a/LinearList/as-List/linearList.h b/LinearList/as-List/linearList.h index bb4c726..2ab7bdd 100644 --- a/LinearList/as-List/linearList.h +++ b/LinearList/as-List/linearList.h @@ -11,7 +11,6 @@ public: 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 }; diff --git a/LinearList/as-List/main.cpp b/LinearList/as-List/main.cpp new file mode 100644 index 0000000..7e3b4fb --- /dev/null +++ b/LinearList/as-List/main.cpp @@ -0,0 +1,50 @@ +#include +#include +#include "linearList.h" +#include "chainNode.h" +#include "chain.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] +ok 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() { + chain test(10); + 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; +} + +/* +Result: +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