-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
232 lines (196 loc) · 3.93 KB
/
LinkedList.cpp
File metadata and controls
232 lines (196 loc) · 3.93 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <iostream>
#include <stack>
using namespace std;
struct Node{
int data;
struct Node *next;
};
class List{
private:
Node *head;
public:
List() {
head =0;
}
void insertNode(int data);
void deleteNode(int num);
bool isEmpty();
void traverseList(int num);
void invertList();
void printLast();
void printNth(int N);
// void serchList(int num);
void display();
};
bool List::isEmpty(){
if (head == 0){
return true;
}
else{
return false;
}
}
void List::display()
{
Node *ptr = head;
while(ptr){
cout<< ptr->data <<endl;
ptr = ptr->next;
}
}
void List::insertNode(int data){
Node *temp = new Node();
Node *p,*q;
temp->data = data;
if (head == 0 ){
cout << "Head is null" << endl;
head = temp;
}
else if(temp->data < head->data){ // 앞에다 삽입
temp->next = head;
head = temp;
}
else{
p = head;
while((p!=0)&&(p->data < temp ->data)){
q = p;
p = p->next;
}
if (p!=0){
temp ->next =p;
q ->next =temp;
}
else
{
q->next = temp;
}
}
}
void List::deleteNode(int num){
Node *p,*q;
if(head->data == num){
p = head;
head = head -> next;
delete p;
//지워야하는게 해드면
}
else
{
p = head;
while(p!=0 && p->data != num){
q = p;
p = p->next;
} // 숫자 찾기
if(p!=0){
q->next = p->next;
delete p;
} // 찾으면 지우기
else
{
cout<< num << "is not in the list"<<endl;
} // 없으면 리스트에 없는수
}
}
// void List::serchList(int num){
// Node *p;
// if (!isEmpty()){
// p = head;
// while(p){
// cout<<p->data;
// p = p -> next;
// }
// cout<< endl;
// }
// else
// {
// cout<<"List is empty"<<endl;
// }
// }
// List::~List(){
// Node*p;
// while (head !=0){
// p = head;
// head = head -> next;
// delete p;
// }
// }
void List::traverseList(int num){
Node *p;
if (head != 0){
p = head;
while( p!= 0 && p->data != num){
p = p ->next;
}
if (p){
cout<< p ->data <<" is found"<<endl;
}
else
{
cout<< num << " is not in the list"<<endl;
}
}
else
{
cout<< "List is empty"<<endl;
}
}
void List::invertList(){
stack<int> s;
Node *p = head;
if(head == 0){
cout << "List is empty" << endl;
}
else{
while(p != 0){
s.push(p->data);
p = p->next;
}
while(!s.empty()){
cout << s.top() << " ";
s.pop();
}
}
}
void List::printLast(){
Node *p = head;
if(head == 0){
cout << "List is empty" << endl;
}
else{
while(p->next != 0){
p = p-> next;
}
cout << p->data << endl;
}
}
void List::printNth(int N){
Node *p = head;
if(head == 0) {
cout << "List is empty" << endl;
return;
}
for(int i =0 ; i < N-1 ; i++){
p = p-> next;
if(p == 0){
cout << "List error" << endl;
return;
}
}
cout << p->data << endl;
}
int main(){
List l;
// l.deleteNode(10);
l.insertNode(10);
l.insertNode(5);
l.display();
l.insertNode(30);
l.traverseList(5);
l.deleteNode(5);
l.display();
l.traverseList(3);
l.printLast();
l.insertNode(40);
l.invertList();
return 0;
}