-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1
More file actions
56 lines (55 loc) · 1.34 KB
/
Copy path1
File metadata and controls
56 lines (55 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;
template <class T> class chain;
template <class T>
class ChainNode {
friend class Chain<T>;
private:
T data; ChainNode<T> *link;
public:
ChainNode(const T& e, ChainNode *next=NULL):data(e), link(next) {};
};
template <class T>
class Chain {
private:
ChainNode<T> *first; ChainNode<T> *last;
public:
Chain(ChainNode<T> *fst=NULL, ChainNode<T> *lst=NULL) : first(fst), last(lst) {};
void Insert(const T & istData);
void Delete(T delData);
void Print();
int Length();
void Reverse();
};
template <class T>
void Chain<T>::Insert(const T & istData) {
if(first == NULL) {
first = istData;
last = first;
}
else {
last->link=istData;
last=last->link;
}
}
template <class T>
void Chain<T>::Delete(T delData) {
ChainNode *current = first;
ChainNode *beforeNode = NULL;
if (current == NULL || current->data == delData) {
if (current == NULL) { cout << "빈 리스트입니다." << endl; return; }
first = current->link;
delete current; return;
}
else {
beforeNode = current;
for (current = first->link; current != NULL; beforeNode = current, current = current->link) {
if (current->data == delData) {
if (current->link == NULL) last = beforeNode;
beforeNode->link = current->link;
delete current; return;
}
}
cout << "찾고자 하는 값이 리스트에 없습니다." << endl;
}
}