-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLL.cpp
298 lines (256 loc) · 6.56 KB
/
SLL.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// added comments to explain steps
// and for debugging, you may better know then me
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int d) {
data = d;
next = NULL;
}
};
class SLL {
Node* head;
public:
SLL() {
head = NULL;
}
void add_at_start(int d) {
cout << "adding at start: " << d << endl; // *
// step 1. make a newnode and add data
// step 2. always make newnode->next = head,
// { if head is NULL, so newnode->next = NULL otherwise newnode->next will be head }
// finally make the newnode the head of s-linked list
Node* newnode = new Node(d);
newnode->next = head;
head = newnode;
}
void add_at_end (int d) {
cout << "adding at end: " << d << endl; // *
// step 1. create a newnode and add data
// step 2. always make next ptr = NULL
// step 3. if list is empty, make newnode head of list
// else find last node and make its next = newnode
Node* newnode = new Node(d);
newnode->next = NULL;
if(!head) {
head = newnode;
} else {
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newnode;
}
}
void add_after (int p, int d) {
cout << "adding {" << d << "} after position " << p << endl; // *
// step 1. create a newnode and add data
// step 2. find node after given position p
// step 3. if position is greater then no. of nodes present
// - return or show error
// step 4. set newNode->next = temp->next
// step 5. if the position is valid, insert the new node
// step 6. set temp's next to newNode
Node* newnode = new Node(d);
int c = 1; // shows current position
Node* temp = head;
while (temp && c < p) {
temp = temp->next;
c++;
}
if (p > c) {
// add_at_end(d);
cout << "cant find position " << p << endl;
cout << "---" << endl;
return;
}
newnode->next = temp->next;
temp->next = newnode;
}
void sort () {
cout << "sorting SLL" << endl; // *
// I used Bubble sort algorithm, you choose your favr
// step 1. Check if the SLL is empty or has only one node
// step 2. Use a do-while loop for sort
// step 3. compare adjacent nodes and swap data if needed
// stpe 4. continue the loop until the entire SLL is sorted
if (!head || head->next == NULL) {
cout << "empty or only one node" << endl;
} else {
bool swaped;
Node* curr;
Node* temp = NULL;
do {
swaped = false;
curr = head;
while (curr->next != temp) {
if (curr->data > curr->next->data) {
int t = curr->data;
curr->data = curr->next->data;
curr->next->data = t;
swaped = true;
}
curr = curr->next;
}
temp = curr;
} while (swaped);
}
cout << "sorted" << endl; // *
}
void print() {
cout << "Printing SLL" << endl; // *
// step 1. Check if SLL is empty { return if it is }
// step 2. initialize a pointer to traverse the list
// step 3. traverse the list and print each node's data
if (!head) {
cout << "empty SLL" << endl;
} else {
Node* temp = head;
while (temp) {
cout << temp->data << "\t";
temp = temp->next;
}
cout << endl;
}
cout << "---" << endl; // *
}
// ---- delete functions
void del_from_start () {
cout << "deleting from start: "; // *
// 1. Check if the SLL is empty
// 2. Store the node to be deleted in a temporary pointer (temp = head)
// 3. Update the head (head = head->next)
// 4. Deallocate the memory (delete temp)
// 5. Return the data of the deleted node (optional)
if(!head) {
cout << "empty already" << endl;
} else {
Node* temp = head;
head = head->next;
cout << temp->data << endl; // *
cout << "deleted " << temp->data << endl;
delete temp;
}
}
void del_from_end () {
cout << "deleting from end: "; // *
// 1. Check if SLL is empty
// 2. Find the second last node
// 3. Store the last node
// 4. Remove the last node from SLL
// 5. Free the memory of the last node
if (!head) {
cout << "empty already" << endl;
} else {
if(!head->next) {
cout << head->data << endl; // *
cout << "deleted " << head->data << endl;
delete head;
head = NULL;
} else {
Node* temp = head;
Node* prev = NULL;
while (temp->next) {
prev = temp;
temp = temp->next;
}
cout << temp->data << endl; // *
cout << "deleted " << temp->data << endl;
delete temp;
prev->next = NULL;
}
}
}
void del_after (int p) {
// 1. Check if SLL is empty
// 2. Go to the node at the given position
// 3. Store the next node (to be deleted)
// 4. Connect the node at the given position to the node after the next node
// 5. DEL the next node
// 6. Free the memory of removed node
if (!head) {
cout << "empty already " << endl;
} else {
Node* temp = head;
int c = 1;
while (temp && c<p) {
temp = temp->next;
c++;
}
if (!temp || !temp->next) {
cout << "invlid position " << p << endl;
return;
}
Node* temp2 = temp->next;
temp->next = temp2->next;
cout << "deleted after " << p << ":::" << temp->data << endl;
delete temp2;
}
}
void reverse () {
cout << "reversing SLL" << endl; // *
// 1. Check if the list is empty
// 2. Start at the beginning of the SLL
// 3. Move through the SLL, reversing the direction of each node's pointer
// 4. Keep going until you reach the end of the SLL
// 5. Update the head of SLL
if (!head) {
cout << "empty" << endl;
} else {
Node* current = head;
Node* next = NULL;
Node* prev = NULL;
while (current) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
cout << "reversed\n---" << endl;
}
};
int main() {
SLL s;
s.print();
s.add_after(2, 34); // cant find position
s.print();
s.add_at_start(2);
s.add_at_start(4);
s.add_at_start(5);
s.print();
s.add_at_end(45);
s.add_at_end(46);
s.print();
s.add_at_start(10);
s.add_at_end(20);
s.add_after(2, 34);
s.add_after(6, 64);
s.add_after(5, 74);
s.print();
cout << "\noriginal SLL" << endl;
s.print();
s.sort();
s.print();
cout << "\n---deleting functions---\n" << endl; // *
s.del_from_start();
s.print();
s.del_from_end();
s.print();
s.del_after(2);
s.del_after(99); // invalid postion
s.print();
cout << "\noriginal SLL" << endl;
s.print();
s.reverse();
// s.add_at_start(23);
s.print();
// QUIZ:
// Without running what should be the SLL at the end
system("pause");
return 0;
}