-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly_linked_list.cpp
More file actions
274 lines (237 loc) · 9.2 KB
/
singly_linked_list.cpp
File metadata and controls
274 lines (237 loc) · 9.2 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
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
#include<iostream>
using namespace std;
// A class to create Nodes
class Node{
public:
int key;
int data;
Node* next; // same class pointers
//default constructor
Node(){
key = 0;
data = 0;
next = NULL;
}
//parametrized constructor
Node(int k, int d){
key = k;
data = d;
}
};
// Class to perform operations on the node
class SinglyLinkedList{
public:
Node* head;
SinglyLinkedList(){
head = NULL;
}
// passing the argument by address
SinglyLinkedList(Node *n){
head=n;
}
//method to check whether a node is present or not (using key value)
Node* nodeExist(int k){
//iterating through the list from head to NULL (using ptr) (key should be unique)
Node* temp = NULL;
Node* ptr = head;
while (ptr!=NULL){
// only if the key value of the node which the current pointer
// is pointing is equal to the key value we passed. (Node exists in that case)
if(ptr->key==k){
temp = ptr;
}
ptr = ptr->next;
}
return temp; // it is either NULL or points to the Node whose key value is passed
}
// append a Node to the list (append is done at the end)
void appendNode(Node *n){
//we must traverse to the end of the List,
// but first we check if the key value of the entered Node is unique
if(nodeExist(n->key)!=NULL){
cout << "Node already exist with key value, " << n->key << ". Try appending a different node with a different key value" << endl;
}
// Actual appending of the Node
else{
// In case this is the first append (head is pointing to NULL in that case)
// No node initially in the List
if(head==NULL){
head = n;
cout << "Node is appended." << endl;
}
// When the head is pointing to some node. (The case where there is atleast one node present)
//Appending a Node is done at the end
else{
//traversing to the end of the list(last Node) (using pointer ptr)
Node* ptr = head;
while(ptr->next!=NULL){
ptr = ptr->next;
}
//ptr has reached the last Node
ptr->next = n;
cout << "Node appended succesfully." << endl;
}
}
}
// prepend Node (attaching Node at the beginning)
void prependNode(Node* n){
// Ensure first that we donot have duplicate key values of multiple nodes
if(nodeExist(n->key)!=NULL){
cout << "Node already exist with key value, " << n->key << ". Try appending a different node with a different key value" << endl;
}
else{
n->next=head;
head = n;
cout << "Node prepended succesfully." << endl;
}
}
// Insert a Node after a particular node in the List.
// Give key value of the node after which we want to insert node (n)
void insertNode(int k, Node* n){
Node* ptr = nodeExist(k);
if (ptr==NULL){
cout << "No node exist with the key value " << k << "." << endl;
}
else{
if(nodeExist(n->key)!=NULL){
cout << "Node already exist with key value, " << n->key << ". Try appending a different node with a different key value" << endl;
}
else{
n->next = ptr->next;
ptr->next = n;
cout << "Node inserted successfully after key " << k << "." << endl;
}
}
}
//Delete node by specifying the unique key (tricky!!)
void deleteNode(int k){
if(head==NULL){
cout << "List is empty. Cannot delete any node!" << endl;
}
else if(head!=NULL){
// if the head node is required to delete
if(head->key==k){
// the second Node (head->next) will become the head node
head = head->next;
cout << "Node with key value " << k << " is unlinked." << endl;
}
// deleting a node which is not the head node
else{
// traverse to that node (two pointers required)
Node* temp = NULL;
Node* previous_ptr = head;
Node* current_ptr = head->next;
while(current_ptr!=NULL){
if(current_ptr->key==k){
temp = current_ptr;
current_ptr = NULL;
}
else{
previous_ptr = previous_ptr->next;
current_ptr = current_ptr->next;
}
if(temp!=NULL){
previous_ptr->next = temp->next;
cout << "Node with key value " << k << " is unlinked." << endl;
}
else{
cout << "Node with key value " << k << " does not exist." << endl;
}
}
}
}
}
// Update node: Access node with key value and update its data
void updateNode(int k, int data){
Node* ptr = nodeExist(k);
if(ptr!=NULL){
ptr->data = data;
cout << "Node data updated successfully." << endl;
}
else{
cout << "Node with key value " << k << " does not exist." << endl;
}
}
//Printing the List. Traversing and printing
void printList(){
if(head==NULL){
cout << "No nodes present." << endl;
}
else{
Node* temp = head;
cout << endl << "Singly Linked List:" << endl;
cout << "key, data" << endl;
while(temp!=NULL){
cout << temp->key << ", " << temp->data << endl;
temp = temp->next;
}
}
}
};
int main(){
// Menu driven program
SinglyLinkedList s;
int option;
int key_user, key_user_, data_user;
do{
cout << endl << endl << "Enter your option, select a number: " << endl;
cout << "0: Exit" << endl;
cout << "1: Add a node at the end (Appending)" << endl;
cout << "2: Add a node at the beginning (Prepending)" << endl;
cout << "3: Insert a node after a certain key node" << endl;
cout << "4: Delete node of specific key" << endl;
cout << "5: Update the data of a certain key node" << endl;
cout << "6: Print the List" << endl;
cout << "7: Clear screen" << endl;
cin >> option;
Node* n_user = new Node(); // dynamic memory allocation on heap
switch(option){
case 0:
break;
case 1:
cout << "Enter (key,data) for a node, which you want to append." << endl;
cin >> key_user;
cin >> data_user;
n_user->key = key_user;
n_user->data = data_user;
s.appendNode(n_user);
break;
case 2:
cout << "Enter (key, data) for a node, which you want to prepend." << endl;
cin >> key_user;
cin >> data_user;
n_user->key = key_user;
n_user->data = data_user;
s.prependNode(n_user);
break;
case 3:
cout << "Enter key of an existing node, after which you want to insert." << endl;
cin >> key_user_;
cout << "Enter (key, data) for a node, which you want to insert." << endl;
n_user->key = key_user;
n_user->data = data_user;
s.insertNode(key_user_, n_user);
break;
case 4:
cout << "Enter the key value of the node you wish to delete." << endl;
cin >> key_user_;
s.deleteNode(key_user_);
break;
case 5:
cout << "Enter key of an existing node, after which you want to update." << endl;
cin >> key_user;
cin >> data_user;
s.updateNode(key_user, data_user);
break;
case 6:
s.printList();
break;
case 7:
system("clc");
break;
default:
cout << "Enter a valid number." << endl;
}
}while(option!=0);
return 0;
}