-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.cpp
More file actions
371 lines (309 loc) · 7.08 KB
/
Copy pathSinglyLinkedList.cpp
File metadata and controls
371 lines (309 loc) · 7.08 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* Practical Assignment 3 : Singly Linked List
Name : Annie Bhalla
Roll No. : 19HCS4009
Course : BSC (H) Computer Science
Semester : 3
*/
#include<iostream>
using namespace std;
class node
{
public:
int ele;
node *next;
friend class LinkedList;
};
class LinkedList
{
node *head,*tail;
public:
LinkedList(){ // constructor
head=NULL;
tail=NULL;
}
~LinkedList(){ // destrcutor
node* p= head;
while(head!=NULL)
{
p=head;
head=head->next;
delete p;
}
tail=NULL;
}
bool empty() const; //1
void insert_front(int x); //2
void remove_front(); //3
void insert_pos(int pos,int x); //4
void remove_pos(int pos); //5
node* search(int x); //6
int count(); //7
void concatenation(LinkedList& obj); //8
void insert_end(int x); //9
void remove_end(); //10
void reverse(); //11
friend ostream& operator<<(ostream&,const LinkedList&); //12
const int& front() const; // 13
};
// (1) function to determine whether the list in empty or not
bool LinkedList::empty() const
{
if(head==NULL)
return 1;
else
return 0;
}
// (2) function to insert an element in the beginning of the list
void LinkedList::insert_front(int x)
{
node *n=new node;
n->ele=x;
n->next=head;
head=n;
if(tail==NULL)
tail=n;
}
// (3) function to remove the head element or beginning element in the list
void LinkedList::remove_front()
{
if(empty()){
cout<<"\n Empty List . Deletion Not possible "<<endl<<endl;
return;
}
node *old=head;
head=old->next;
delete old;
if(head==NULL)
tail==NULL;
}
// (4) functiont to insert an element at a given position in the list
void LinkedList::insert_pos(int pos,int e)
{
node *n= new node;
n->ele=e;
if(pos==1)
{
n->next=head;
head=n;
tail=n;
return;
}
node *ptr= head;
int count=1;
while(ptr!=NULL && count<pos-1)
{
ptr=ptr->next;
count++;
}
if(ptr==NULL)
{
cout<< "\n Invalid position ";
return;
}
n->next=ptr->next;
ptr->next=n;
}
// (5) function to remove an element from given position in the list
void LinkedList::remove_pos(int pos)
{
node *n=head;
if(head==NULL)
{
cout<<"Not possible ";
return;
}
if(pos==1)
{
node *old=head;
head=old->next;
delete old;
tail=NULL;
return;
}
int count=1;
while(n!=NULL && count<pos-1){
n=n->next;
count++;
}
if(n==NULL)
cout<<"\n Invalid position ";
else
{
node *old=n->next;
n->next=old->next;
delete old;
}
}
// (6) function to search for an element in the list
node* LinkedList::search(int e){
node *n=head;
while(n!=NULL){
if(n->ele==e)
return n;
n=n->next;
}
return n;
}
// (7) function that returns the number of nodes in a list
int LinkedList::count()
{
int ctr=1;
node *ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
ctr++;
}
return ctr;
}
// (8) function to concatenate two singly linked lists
void LinkedList:: concatenation( LinkedList& n1)
{
tail->next= n1.head;
tail=n1.tail;
n1.head=NULL;
n1.tail=NULL;
}
// (9) function to add elements in the ned of the list
void LinkedList:: insert_end(int x)
{
if(empty())
{
node *p =new node;
p->ele=x;
p->next=NULL;
head=p;
tail=p;
return;
}
node *ptr,*arrow;
ptr= new node;
ptr->ele=x;
ptr->next=NULL;
arrow=head;
while(arrow->next!=NULL)
{
arrow=arrow->next;
}
arrow->next=ptr;
tail=ptr;
}
// (10) function to remove elemenets from the end
void LinkedList::remove_end()
{
if(empty())
{
cout<<"\n Empty list";
return;
}
if(count()==1)
{
node *i=head;
head= NULL;
tail= NULL;
delete i;
return;
}
node *ptr,*arrow;
ptr=head;
arrow=head->next;
while(arrow->next!=NULL)
{
ptr=ptr->next;
arrow=arrow->next;
}
ptr->next=NULL;
tail=ptr;
delete arrow;
}
//(11) function to reverse the singly linked list
void LinkedList::reverse()
{
if(empty())
{
cout<<"\n Empty List ";
return;
}
node *p=head;
node *p1=head->next;
if(p1==NULL)
return;
while(p1!=NULL)
{
node *p2 =p1->next;
p1->next=p;
p=p1;
p1=p2;
}
head->next=NULL; // to break link between original first two elements
tail=head;
head=p;
}
// (12) Overlaoding output operator <<
ostream& operator<<(ostream& out,const LinkedList& L)
{
if(L.head==NULL)
{
out<<"\n Empty list ";
return out;
}
else
{
node *n=L.head;
int a;
while(n!=NULL){
a=n->ele;
out<<a<<" ";
n=n->next;
}
return out;
}
}
// (13) function that returns the element at the head of list
const int& LinkedList::front() const
{
if(head==NULL) throw" Empty List";
return head->ele;
}
// driver code
int main()
{
LinkedList l1;
l1.remove_front();
l1.insert_front(10);
l1.insert_front(9);
l1.insert_front(7);
l1.insert_pos(2,8);
cout<<l1;
cout<<endl;
LinkedList l2;
l2.insert_front(1);
l2.insert_pos(2,2);
cout<<l2;
cout<<endl<<"\n Number of nodes in L2 "<<l2.count();
cout<<endl;
cout<<"\n Result after concatenation "<<endl;
l1.concatenation(l2);
cout<<l1;
cout<<endl;
cout<<l2;
cout<<endl;
cout<<"\n Removing elements from l1 "<<endl;
l1.remove_front();
l1.remove_pos(3);
cout<<l1;
cout<<endl;
cout<<"\n Searching element in L2 ";
node *ptr=l2.search(2);
if(ptr==NULL)
cout<<"\n Not found ";
else
cout<<"\n "<<"2 Found at location "<<ptr;
cout<<endl<<endl;
l1.insert_end(0);
cout<<l1;
cout<<"\n Displaying after reversing "<<endl;
l1.reverse();
cout<<l1;
return 0;
}