-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathSLL_Operations_using_Unique_Key.cpp
317 lines (288 loc) · 5.75 KB
/
SLL_Operations_using_Unique_Key.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
This is singly linked list implementation in which
a unique key has to be given while entering the new node to the linked list,
and you cannot give same key to the different node.
*/
#include <iostream>
using namespace std;
class Node
{
public:
int key; //key = index of particular node in linked list
int data;
Node *next;
Node()
{
key = 0;
data = 0;
next = NULL;
}
Node(int k, int d)
{
int key = k;
int data = d;
}
};
class sll
{
public:
Node *head;
sll()
{
head = NULL;
}
sll(Node *n)
{
head = n;
}
// nexist : checks wether node exist in the linked list or not
Node *nexist(int k) //k= key value
{
Node *temp = NULL;
Node *ptr = head;
while (ptr != NULL)
{
if (ptr->key == k)
{
temp = ptr;
}
ptr = ptr->next;
}
return temp;
}
// appendnode : adds the new node at the end of the linked list
void appendnode(Node *n) //n= new node
{
if (nexist(n->key) != NULL)
{
cout << "Entered node exist " << n->key << endl;
}
else
{
if (head == NULL) //assuming that node is empty
{
head = n;
cout << "Node appended" << endl;
}
else //assuming that node stores some data
{
Node *ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = n;
cout << "Node appended " << endl;
}
}
}
// prependnode : adds the node at the head of the linked list
void prependnode(Node *n)
{
if (nexist(n->key) != NULL)
{
cout << "Node already exist = " << n->key << endl;
}
else
{
n->next = head;
head = n;
cout << "Node prepended " << endl;
}
}
// insertnode : insert new node in the middle of the linked list.
void insertnode(int k, Node *n)
{
Node *ptr = nexist(k);
if (ptr == NULL) //checking wether the entered key node exist or not
{
cout << "No node exist with this key value = " << k << endl;
}
else
{
if (nexist(n->key) != NULL)
{
cout << "Node already exist with same key value = " << n->key << endl;
}
else
{
n->next = ptr->next;
ptr->next = n;
cout << "Node inserted " << endl;
}
}
}
// dnode : to delete the node from the linked list
void dnode(int k)
{
if (head == NULL)
{
cout << "Node is empty " << endl;
}
else if (head != NULL)
{
if (head->key == k) // assuming that the head is being deleted
{
head = head->next;
cout << "Node deleted " << endl;
}
else //assuming that any middle or last node is being is deleted
{
Node *temp = NULL;
Node *prevptr = head;
Node *currentptr = head->next;
while (currentptr != NULL)
{
if (currentptr->key == k)
{
temp = currentptr;
currentptr = NULL;
}
else
{
prevptr = prevptr->next;
currentptr = currentptr->next;
}
}
if (temp != NULL)
{
prevptr->next = temp->next;
cout << "Node deleted " << k << endl;
}
else
{
cout << "No node exist with " << k << " value " << endl;
}
}
}
}
// updatenode : by this you have to update the data entered in linked list
void updatenode(int k, int d)
{
Node *ptr = nexist(k);
if (ptr != NULL)
{
ptr->data = d;
cout << "Node data updated successfully " << endl;
}
else
{
cout << "Node doesn't exist " << k << " value " << endl;
}
}
// printlist : it prints the linked list
void printlist()
{
if (head == NULL)
{
cout << "Node is empty " << endl;
}
else
{
cout << "\nSingly linked list values are " << endl;
Node *temp = head;
while (temp != NULL)
{
cout << "(" << temp->key << "," << temp->data << ")-->"
<< " ";
temp = temp->next;
}
cout << "NULL";
}
}
};
int main()
{
sll s;
int option;
int key1, k1, data1;
do
{
cout << "\nWhat operation you want to perform " << endl;
cout << "1 Append the node or insert the node " << endl;
cout << "2 Add the node in the begining " << endl;
cout << "3 Insert the node in between " << endl;
cout << "4 Delete the node " << endl;
cout << "5 Update or make correction in the node " << endl;
cout << "6 Print the node " << endl;
cout << "7 Clear the screen\n"<< endl;
cin >> option;
Node *n1 = new Node();
switch (option)
{
case 0:
break;
case 1:
cout << "Append node operation choosed " << endl;
cout << "Enter the key " << endl;
cin >> key1;
cout << "Enter the data " << endl;
cin >> data1;
n1->key = key1;
n1->data = data1;
s.appendnode(n1);
break;
case 2:
cout << "Prepend operation choosed " << endl;
cout << "Enter the key " << endl;
cin >> key1;
cout << "Enter the data " << endl;
cin >> data1;
n1->key = key1;
n1->data = data1;
s.prependnode(n1);
break;
case 3:
cout << "Insert node operation selected " << endl;
cout << "Enter the key after which you want to insert " << endl;
cin >> k1;
cout << "Enter the key " << endl;
cin >> key1;
cout << "Enter the data " << endl;
cin >> data1;
n1->key = key1;
n1->data = data1;
s.insertnode(k1, n1);
break;
case 4:
cout << "Delete node operation selected " << endl;
s.printlist();
cout << "Enter the key to be deleted " << endl;
cin >> k1;
s.dnode(k1);
break;
case 5:
cout << "Update operation selected " << endl;
cout << "Enter the key " << endl;
cin >> key1;
cout << "Enter the NEW data " << endl;
cin >> data1;
s.updatenode(key1, data1);
break;
case 6:
s.printlist();
break;
case 7:
system("cls");
break;
default:
cout << "Enter the proper option : " << endl;
}
} while (option != 0);
{
cout << "Thank you !!" << endl;
}
return 0;
}
/*
Test Case 1 :
input : 1 1 12 2 2 34 3 1 3 24 6
outout: (2,34)--> (1,12)--> (3,24)--> NULL
Test case 2:
input: 1 1 100 1 2 101 4 2 6
outout: (1,100)--> NULL
*/
/*
Time complexity - O(n)
Space complexity - O(n)
*/