-
Notifications
You must be signed in to change notification settings - Fork 28
/
menudriven_singlylinkedlist.cpp
330 lines (323 loc) · 6.52 KB
/
menudriven_singlylinkedlist.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//program of Singly linked list
#include<iostream>
using namespace std;
class Node
{
public:
int info;//data part of node
Node *next;//address part of node, which is used to store address of next Node
Node(int value)
{
info=value;
next=NULL;
}
};
class LinkedList
{
public:
Node *start;//this is used to store address of first node always
LinkedList()
{
start=NULL;
}
void create_LinkedList();
void displayList();//thi is used for traversing the linked list
void add_In_Begining();
void add_AtTheEnd();
void add_AtTheSpecifiedPosition(int val, int pos);
void delete_FromBegining();
void delete_FromEnd();
void delete_SpecifiedNode(int nodeNumer);
int countNode();
};
void LinkedList::create_LinkedList()
{
Node *temp;
temp=start;
int value;
cout<<"\n enter value of Newnode: ";
cin>>value;
Node *Newnode=new Node(value);//this instr. will create new node
if(start==NULL)
{
start=Newnode;//address of first Node stored in the start
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=Newnode;
}
cout<<"\n Newnode has been added at the end of the list";
}
void LinkedList::displayList()
{
Node *temp=start;
if(start==NULL)
{
cout<<"\n Linked list is empty, there is no Node";
}
else
{
cout<<"\n elements in the list are as follows:\n";
while(temp!=NULL)
{
cout<<temp->info<<"\t";
temp=temp->next;
}
}
}
int LinkedList::countNode()
{
Node *temp=start;
int count=0;
while(temp!=NULL)
{
temp=temp->next;
count++;
}
return count;
}
void LinkedList::add_In_Begining()
{
int value;
cout<<"\n enter value of new node :";
cin>>value;
Node *Newnode= new Node(value);
Newnode->next=start;
start=Newnode;
cout<<"\n Newnode has been added at the begining of the list";
}
void LinkedList::add_AtTheEnd()
{
Node *temp;
temp=start;
int value;
cout<<"\n enter value of Newnode: ";
cin>>value;
Node *Newnode=new Node(value);//this instr. will create new node
if(start==NULL)
{
start=Newnode;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=Newnode;
}
cout<<"\n Newnode has been added at end of the list";
}
void LinkedList::add_AtTheSpecifiedPosition(int val, int pos)
{
Node *temp=start;
Node *Newnode=new Node(val);
int count=countNode();
if(pos>count)
{
cout<<"\n this is the invalid position to insert the node";
}
else
{
if(start==NULL)
{
start=Newnode;
}
else if(pos==0)
{
Newnode->next=start;
start=Newnode;
}
else
{
for(int i=1;i<pos;i++)
{
temp=temp->next;
}
Newnode->next=temp->next;
temp->next=Newnode;
}
if(pos==0)
{
cout<<"\n Newnode has been added in the begining of list as 1st node ";
}
else
{
cout<<"\n Newnode has been added after node"<<pos;
}
}
}
void LinkedList::delete_FromBegining()
{
Node *temp=start;
int data;
if(start==NULL)
{
cout<<"\n list is empty, there is no Node to delete";
}
else
{
start=start->next;//or start=temp->next;
data=temp->info;
delete(temp);
cout<<"\n"<<data<<" has been deleted,";
cout<<"\n after deletion";
displayList();
}
}
void LinkedList::delete_FromEnd()
{
Node *temp;
Node *temp1;
int data;
if(start==NULL)
{
cout<<"\n list is empty, there is no Node to delete";
}
else if(start->next==NULL)//this is the first node as well as last
{
temp=start;
start=NULL;
data=temp->info;
delete(temp);//the only node in the list has been deleted.
cout<<"\n"<<data<<" has been deleted";
cout<<"\n after deletion";
displayList();
}
else
{
temp=start;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp1=temp->next;//address of last node stored in temp1.
data=temp1->info;
delete(temp1); // last node has been deleted.
temp->next=NULL;//In address part of last node NULL value has been stored.
cout<<"\n"<<data<<" has been deleted,";
cout<<"\n after deletion";
displayList();
}
}
void LinkedList::delete_SpecifiedNode(int nodeNumer)
{
Node *temp;
Node *temp1;
int data;
int count=countNode();
if(nodeNumer>count)
{
cout<<"\n illegal request or list is empty,so request is not accepted";
}
else if(start==NULL)
{
cout<<"\n list is empty, there is no NODE to delete";
}
else
{
if(nodeNumer==1)//deletion of first node
{
temp=start;
start=start->next;
data=temp->info;
delete(temp);
cout<<"\n"<<data<<" has been deleted ";
}
else if(nodeNumer>=2&&nodeNumer==count)//deletion of last node
{
temp=start;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp1=temp->next;//address of last node stored in temp1.
temp->next=NULL;//In address part of last node NULL value has been stored.
data=temp1->info;
delete(temp1); // last node has been deleted.
cout<<"\n"<<data<<"\t haa been deleted ";
}
else if(nodeNumer>=2&&nodeNumer<count)
{
temp=start;
for(int i=1;i<nodeNumer-1;i++)
{
temp=temp->next;
}
temp1=temp->next;
temp->next=temp1->next;
data=temp1->info;
delete(temp1);
cout<<"\n"<<data<<" haa been deleted ";
}
}
}
int main()
{
int choice,n;
int val,pos;
int nodeNumer;
LinkedList L1;
do
{
cout<<"\n\n-------Menu options--------"<<endl;
cout<<"press 1. to create linked list \n";
cout<<"press 2. to display linked list \n";
cout<<"press 3. to add node in the Begining of the list\n";
cout<<"press 4. to add node at the End of the list\n";
cout<<"press 5. to add node at the specified position in the list\n";
cout<<"press 6. to Delete the node from the Begining of the list\n";
cout<<"press 7. to Delete the node from the End of the list\n";
cout<<"press 8. to Delete the node from the specified position in list\n";
cout<<"press 9. to exit\n";
cout<<"\n enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n how many nodes you want to create and add in the List:";
cin>>n;
for(int i=1;i<=n;i++)
{
L1.create_LinkedList();
}
break;
case 2:
L1.displayList();
break;
case 3:
L1.add_In_Begining();
break;
case 4:
L1.add_AtTheEnd();
break;
case 5:
cout<<"\n enter value of node to be inserted: ";
cin>>val;
cout<<"\n enter position after which node should be inserted: ";
cin>>pos;
L1.add_AtTheSpecifiedPosition(val,pos);
break;
case 6:
L1.delete_FromBegining();
break;
case 7:
L1.delete_FromEnd();
break;
case 8:
cout<<"\n enter the Node number which you want to delete: ";
cin>>nodeNumer;
L1.delete_SpecifiedNode(nodeNumer);
break;
case 9:
exit(0);
break;
default:
cout<<"you have enterd wrong choice \n";
}
}while(choice!=9);
}