as-Node
This commit is contained in:
167
LinearList/as-List/chain.h
Normal file
167
LinearList/as-List/chain.h
Normal file
@@ -0,0 +1,167 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "chainNode.h"
|
||||
#include "linearList.h"
|
||||
|
||||
template <typename T>
|
||||
class chain : public linearList<T> {
|
||||
public:
|
||||
chain(int initialCapacity = 10);
|
||||
chain(const chain<T>& 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<T>* first;
|
||||
int listSize;
|
||||
int capacity;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
chain<T>::chain(int initialCapacity) {
|
||||
capacity = initialCapacity;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
chain<T>::chain(const chain<T>& 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<T>* source = x.first;
|
||||
first = new chainNode<T>(source->element);//equals to chainNode<T>(source->element, nullptr)
|
||||
chainNode<T>* target = first;
|
||||
|
||||
source = source->next;
|
||||
|
||||
while (source != nullptr) {
|
||||
target->next = new chainNode<T>(source->element);
|
||||
target = target->next;
|
||||
source = source->next;
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
chain<T>::~chain() {
|
||||
chainNode<T>* tmp = first;
|
||||
while (tmp != nullptr) {
|
||||
tmp = tmp->next;
|
||||
delete first;
|
||||
first = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool chain<T>::empty() const {
|
||||
//If empty, return 0;
|
||||
return listSize == 0;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int chain<T>::size() const
|
||||
{
|
||||
//Return Array Size
|
||||
return listSize;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T& chain<T>::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<T>* tmp = first;
|
||||
for (int i = 1; i <= theIndex; i++) {
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return tmp->element;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int chain<T>::indexOf(const T& theElement) const
|
||||
{
|
||||
//Return the index of "theElement" first appeared
|
||||
chainNode<T>* tmp = first;
|
||||
for (int i = 0; i < listSize; i++) {
|
||||
if (tmp->element == theElement) {
|
||||
return i;//i = arr - 1
|
||||
}
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void chain<T>::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<T>* deleteNode;
|
||||
|
||||
if (theIndex == 0) {
|
||||
deleteNode = first;
|
||||
first = first->next;
|
||||
}
|
||||
else {
|
||||
chainNode<T>* prev = first;
|
||||
for (int i = 0; i < theIndex - 1; i++) {
|
||||
prev = prev->next;
|
||||
}
|
||||
deleteNode = prev->next;
|
||||
prev->next = deleteNode->next;
|
||||
}
|
||||
delete deleteNode;
|
||||
listSize--;
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
void chain<T>::insert(int theIndex, const T& theElement)
|
||||
{
|
||||
//Insert theElement to place Arr[theIndex]
|
||||
chainNode<T>* arr = first;
|
||||
if (theIndex < 0) throw std::out_of_range("index out of range");
|
||||
|
||||
if (theIndex == 0) {
|
||||
// <20><><EFBFBD>뵽ͷ<EBB5BD><CDB7>
|
||||
first = new chainNode<T>(theElement, first);
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < theIndex - 1; i++) {
|
||||
arr = arr->next;
|
||||
}
|
||||
chainNode<T>* insert = new chainNode<T>(theElement);
|
||||
insert->next = arr->next;
|
||||
arr->next = insert;
|
||||
listSize++;
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void chain<T>::output() const
|
||||
{
|
||||
//cout
|
||||
chainNode<T>* tmp = first;
|
||||
for (int i = 0; i < listSize; i++) {
|
||||
std::cout << tmp->element << " ";
|
||||
tmp = tmp->next;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
22
LinearList/as-List/chainNode.h
Normal file
22
LinearList/as-List/chainNode.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
template <typename T>
|
||||
class chainNode {
|
||||
public:
|
||||
T element;
|
||||
chainNode<T>* next;
|
||||
//C11 can write like : std::unique_ptr<chainNode<T>> next;
|
||||
|
||||
public:
|
||||
chainNode();
|
||||
chainNode(const T& x, chainNode<T>* next = nullptr);
|
||||
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
chainNode<T>::chainNode() {
|
||||
this->next = nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
chainNode<T>::chainNode(const T& x, chainNode<T>* next) : element(x), next(next) {
|
||||
}
|
@@ -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
|
||||
};
|
||||
|
50
LinearList/as-List/main.cpp
Normal file
50
LinearList/as-List/main.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#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<int> 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
|
||||
*/
|
Reference in New Issue
Block a user