-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.h
More file actions
205 lines (165 loc) · 4.72 KB
/
LinkedList.h
File metadata and controls
205 lines (165 loc) · 4.72 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef __LINKEDLIST__
#define __LINKEDLIST__
#include <iostream>
using namespace std;
//LinkedList Node
template <class T> class Node{
public:
//데이터를 저장할 변수
T data;
//노드구조체 이용; 다음노드의 주소를 저장할 포인터
Node *link;
Node(T element){
data = element;
link = 0;
}
};
//LinkedList Class
template <class T> class LinkedList{
protected:
//첫번째 노드의 주소를 저장할 포인터
Node<T> *first;
int current_size;
public:
//생성자, 초기화
LinkedList(){
first = 0;
current_size = 0;
};
//노드 개수를 리턴
int GetSize(){
return current_size;
};
//맨 앞에 원소를 삽입, LinkedList와 Stack 둘다 같다
void Insert(T element);
//맨 뒤의 원소를 삭제, 제일 나중에 들어온 원소 삭제 - LinkedList
virtual bool Delete(T &element);
//리스트 출력
void Print();
};
//새 노드를 맨 앞에 붙이고 값을 넣음
template <class T> void LinkedList<T>::Insert(T element){
//노드 생성
Node<T> *newnode = new Node<T>(element);
//새 노드가 첫번째 노드를 가리킴
//newnode는 포인터이기 때문에 -> 를 사용해 함수, 변수를 불러옴 newnode.link와 뜻은 같다
newnode -> link = first;
first = newnode;
current_size++;
}
//마지막 노드의 값을 리턴하면서 메모리에서 할당 해제
template <class T> bool LinkedList<T>::Delete(T &element){
if (first == 0)
return false;
Node<T> *current = first;
Node<T> *previous = 0;
//마지막 노드까지 찾아가는 반복문
while(1){
if (current->link == 0){ //마지막 노드를 찾는것
if (previous)
previous -> link = current -> link;
else
first = first -> link;
break;
}
previous = current;
current = current -> link;
}
element = current -> data;
delete current;
current_size--;
return true;
}
//리스트를 출력하는 Print 함수
template <class T> void LinkedList<T>::Print(){
Node<T> *i;
int index = 1;
if (current_size != 0){
for( i = first; i != NULL; i=i->link){
if (index == current_size){
cout << "[" << index << "|";
cout << i -> data <<"]->";
}
else {
cout << "[" << index << "|";
cout << i -> data << "]->";
index++;
}
}
cout << endl;
}
}
#endif
// #ifndef __LINKEDLIST__
// #define __LINKEDLIST__
// #include <iostream>
// using namespace std;
// //LinkedList Node
// class Node{
// public:
// //데이터를 저장할 변수
// int data;
// //노드구조체 이용; 다음노드의 주소를 저장할 포인터
// Node *link;
// Node(int element){
// data = element;
// link = 0;
// }
// };
// //LinkedList Class
// class LinkedList{
// protected:
// //첫번째 노드의 주소를 저장할 포인터
// Node *first;
// int current_size;
// public:
// //생성자, 초기화
// LinkedList(){
// first = 0;
// current_size = 0;
// };
// //노드 개수를 리턴
// int GetSize(){
// return current_size;
// };
// //맨 앞에 원소를 삽입, LinkedList와 Stack 둘다 같다
// void Insert(int element);
// //맨 뒤의 원소를 삭제, 제일 나중에 들어온 원소 삭제 - LinkedList
// virtual bool Delete(int &element);
// //리스트 출력
// void Print();
// };
// //새 노드를 맨 앞에 붙이고 값을 넣음
// void LinkedList::Insert(int element){
// //노드 생성
// Node *newnode = new Node(element);
// //새 노드가 첫번째 노드를 가리킴
// //newnode는 포인터이기 때문에 -> 를 사용해 함수, 변수를 불러옴 newnode.link와 뜻은 같다
// newnode -> link = first;
// first = newnode;
// current_size++;
// }
// //마지막 노드의 값을 리턴하면서 메모리에서 할당 해제
// bool LinkedList::Delete(int &element){
// if (first == 0)
// return false;
// Node *current = first;
// Node *previous = 0;
// //마지막 노드까지 찾아가는 반복문
// while(1){
// if (current->link == 0){ //마지막 노드를 찾는것
// if (previous)
// previous -> link = current -> link;
// else
// first = first -> link;
// break;
// }
// previous = current;
// current = current -> link;
// }
// element = current -> data;
// delete current;
// current_size--;
// return true;
// }
// #endif