linked list 자료구조 연습
(아직 미완)
#include <iostream>
using namespace std;
template<class T> class Chain;
template<class T> class ChainIterator;
template<class T> class ChainNode {
friend class Chain<T>;
friend class ChainIterator<T>;
public:
ChainNode(T element = 0);
private:
T data;
ChainNode<T>* link;
};
template<class T> class Chain {
friend class ChainIterator<T>;
public:
Chain() {first = 0;};
void Delete(void);
int Length();
void Add(const T& element);
void Invert();
void Concatenate(Chain<T> b);
//template <class T>
friend ostream& operator<<(ostream&, Chain<T>);
//int Minimum();
private:
ChainNode<T>* first;
};
template<class T>
class ChainIterator {
public:
ChainIterator(const Chain<T>& I):list(I), current(I.first) {};
T& operator *() const {return current->data;};
T* operator ->() const {return ¤t->data;};
//increment
ChainIterator<T>& operator ++();
ChainIterator<T> operator ++(int);
bool NotNull();
bool NextNotNull();
T* First();
T* Next();
private:
const Chain<T>& list; //refers to an existing list
ChainNode<T>* current; // points to a node in list
};
template<class T>
ChainNode<T>::ChainNode(T element) {
data = element;
link = 0;
}
template<class T>
void Chain<T>::Delete(void) {
ChainNode<T>* current, *next;
if(first != nullptr) {
next = first->link;
ChainNode<T>* temp = first;
first = next;
delete temp;
}
else cout << "Error - Empty List. Not deleted" << "\n";
}
template<class T>
void Chain<T>::Add(const T& element) {
ChainNode<T>* newNode = new ChainNode<T>(element);
if(!first) {
first = newNode;
return;
}
newNode->link = first;
first = newNode;
}
template<class T>
void Chain<T>::Invert() {
ChainNode<T>* p =first, *q = 0; //q trails p
while(p) {
ChainNode<T>* r = q;
q = p;
p = p->link;
q->link = r;
}
first = q;
}
template<class T>
void Chain<T>::Concatenate(Chain<T> b) {
ChainNode<T>* p;
if(!first) {
first = b.first;
return;
}
if(b.first) {
for(p = first; p->link;p = p->link)
p->link = b.first;
}
}
template<class T>
ChainIterator<T>& ChainIterator<T>:: operator++() {
current = current->link;
return *this;
}
template<class T>
ChainIterator<T> ChainIterator<T>::operator++(int) {
ChainIterator<T> old = *this;
current = current->link;
return old;
}
template<class T>
bool ChainIterator<T>::NotNull() {
if(current) return 1;
else return 0;
}
template<class T>
bool ChainIterator<T>::NextNotNull() {
if(current && current->link) return 1;
else return 0;
}
template<class T>
T* ChainIterator<T>::First() {
if(list.first) return &list.first->data;
else return 0;
}
template<class T>
T* ChainIterator<T>::Next() {
if(current) {
current = current->link;
return ¤t->data;
}
else return 0;
}
template<class T>
int Show(const Chain<T>& I) {
ChainIterator<T> li(I);
if(!li.NextNotNull()) return 0;
T retvalue = *li.First();
cout << retvalue;
while(li.NextNotNull()) {
retvalue = *li.Next();
cout << " <- " << retvalue;
}
return retvalue;
}
template<class T>
int sum(const Chain<T>& I) {
ChainIterator<T> li(I);
if(!li.NotNull()) return 0;
T retvalue = *li.First();
while(li.NextNotNull()) {
retvalue += *li.Next();
}
return retvalue;
}
template<class T>
int SumProductFifth(const Chain<T>& I) {
// Sum(Xi * Xi+5)
return 0;
}
template<class T>
int Length(const Chain<T>& I) {
//list 갯수
return 0;
}
template<class T>
int Maximum(const Chain<T>& I) {
//list에서 최솟값
return 0;
}
template<class T>
int Minimun(const Chain<T>& I) {
//list에서 최솟값
return 0;
}
template<class T>
int MakeArray(const Chain<T>& I, int []) {
//list 원소를 읽어 배열에 저장
return 0;
}
int main(void) {
int select;
//ChainNode<int> nd;
Chain<int> a, b;
ChainIterator<int> cit(a);
int value;
do{
cout << "\n1. Add new value to chain 'A\n2. Add new value to chain 'B'";
cout << "\n3. Print all chains\n4. Invert Chain\n5. Concatenate A and B\n6. Delete first node in Chain";
cout << "\n7. Get Sum of the chain\n8. Get length of the chain\n9. Get Max val in Chain\n10. Quit\n";
cout << "Selet command(1~10) : ";
cin >> select;
switch (select)
{
case 1:
cout << "Add a new value: ";
cin >> value;
a.Add(value);
break;
case 2:
cout << "Add a new value: ";
cin >> value;
b.Add(value);
break;
case 3:
cout << "Chain A: ";
Show(a);
cout << "\n";
cout << "Chain B: ";
Show(b);
cout << "\n";
case 4:
cout << "\nWhich chain will you invert?\n1. Chain A\n2.Chain B\n\nSelect command(1~2) : ";
cin >> value;
if(value == 1) {
a.Invert();
} else if(value == 2) {
b.Invert();
} else {
cout << "Invalid value\n";
}
break;
case 5:
cout << "\nConcatenate chain\n";
a.Concatenate(b);
Show(a);
cout << "\n";
break;
case 6:
cout << "\nWhich chain will you delete?\n[1] Chain A\n[2]Chain B\n\nSelect Command(1~2) : ";
cin >> value;
if(value == 1)
a.Delete();
else if(value == 2)
b.Delete();
else
cout << "Invalid value\n";
break;
case 7:
cout << "\nWhich chain will you sum?\n[1]Chain A\n[2]Chain B\n\nSelect Command(1~2) : ";
cin >> value;
if(value == 1)
sum(a);
else if(value == 2)
sum(b);
else
cout << "Invalid value\n";
break;
case 8:
cout << "\nWhich chain's length do you want?\n[1]Chain A\n[2]Chain B\n\nSelect Command(1~2) : ";
cin >> value;
if(value == 1)
cout << Length(a) << "\n";
else if(value == 2)
cout << Length(b) << "\n";
else
cout << "Invalid value\n";
break;
case 9:
cout << "\nWhich chain's max-value do you want?\n[1]Chain A\n[2]Chain B\n\nSelect Command(1~2) : ";
cin >> value;
if(value == 1)
cout << Maximum(a) << "\n";
else if(value == 2)
cout << Maximum(b) << "\n";
else
cout << "Invalid value\n";
break;
case 10:
cout << "Quit the program...";
break;
default:
cout << "WRONG INPUT\n";
break;
}
} while(select != 10);
system("pause");
return 0;
}