-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_Singly_linked_list_operations.cpp
288 lines (271 loc) · 5.74 KB
/
2_Singly_linked_list_operations.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
/* AARTI RATHI
Topic: Singly linked list operations
My website - https://shinchancode.github.io/3d-react-portfolio/
*/
/* Problem Statement :
Create Singly Linked List and perform the following operation
1. Insertion
2. Deletion
3. Display Records.
4. Search a record form linked list.
(Hint: Create class Linked list, define node structure using class or structure.
Create menu driven program in which options are
1. Insert at start
2. Insert at end
3. Insert at position
4. Delete at start
5. Delete at end
6. Delete at position
7. Display record
8. Search a record) */
#include<iostream>
using namespace std;
//Structure of node of linked list
struct node
{
int data;
node *next;
};
//Class for operation on Linked List
class linklist
{
node *next,*tmp,*ptr,*head,*prev,*end;
public:
linklist()
{
head = NULL;
prev = NULL;
end =NULL;
}
void insert_beg(int x); //Insertion in the beginning
void insert_end(int x); //Insertion at the end
void insert_pos(int x,int pos); //Insertion at any position
void del_beg(); //Deletion from the beginning
void del_end(); //Deletion from the end
void del_pos(int pos); //Deletion from any position
void display(); //Display content of linked list
void search(int x); //Searching element in linked list
node *create(int x) //Node creation
{
node *temp;
temp=new node;
temp->data=x;
temp->next=NULL;
return temp;
}
};
void linklist::insert_beg(int x)
{
tmp=create(x);
if(head==NULL)
{
head=tmp;
end=head;
}
else
{
tmp->next=head;
head=tmp;
}
}
void linklist::insert_end(int x)
{
tmp=create(x);
if(head==NULL)
{
head=tmp;
end=head;
}
end->next=tmp;
end=tmp;
}
void linklist::insert_pos(int x,int pos)
{
tmp=create(x);
ptr=head;
for(int i=1;i<pos;i++)
{
prev=ptr;
ptr=ptr->next;
}
tmp->next=ptr;
prev->next=tmp;
}
void linklist::del_beg()
{
if(head==NULL)
{
cout<<"LIST IS EMPTY!! \n";
return;
}
ptr=head;
head=head->next;
ptr->next=NULL;
delete ptr;
}
void linklist::del_end()
{
if(head==NULL)
{
cout<<"LIST IS EMPTY!! \n";
return;
}
ptr=head;
while(ptr!=end)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=NULL;
end=prev;
delete ptr;
}
void linklist::del_pos(int pos)
{
if(head==NULL)
{
cout<<"LIST IS EMPTY!! \n";
return;
}
ptr=head;
for(int i=1;i<pos;i++)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=ptr->next;
ptr->next=NULL;
delete ptr;
}
void linklist::display()
{
ptr=head;
int i=1;
while(ptr!=end)
{
cout<<i<<"."<<ptr->data<<"\n";
ptr=ptr->next;
i++;
}
cout<<i<<"."<<ptr->data<<"\n";
}
void linklist::search(int x)
{
ptr=head;
int i=1,check=0;
while(1)
{
if(ptr->data==x)
{
cout<<"MATCH FOUND AT POSITION: "<<i<<"\n";
check=1;
}
if(check==1 || ptr==end)
{
break;
}
ptr=ptr->next;
i++;
}
if(check==0)
{
cout<<"MATCH NOT FOUND!! \n";
}
}
int main() //main function
{
linklist a1;
int n,i,x,pos,p=1;
do
{
cout<<"------ENTER YOU CHOICE------\n";
cout<<"1. Insert At Start\n";
cout<<"2. Insert At End\n";
cout<<"3. Insert At Position\n";
cout<<"4. Delete At Start\n";
cout<<"5. Delete At End\n";
cout<<"6. Delete At Position\n";
cout<<"7. Display Record\n";
cout<<"8. Search a Record \n";
cin>>n;
if(n==1)
{
cout<<"ENTER DATA FOR INSERTION:\n"<<p<<".";
cin>>x;
a1.insert_beg(x);
p++;
}
else if(n==2)
{
cout<<"ENTER DATA FOR INSERTION:\n"<<p<<".";
cin>>x;
a1.insert_end(x);
p++;
}
else if(n==3)
{
cout<<"ENTER DATA FOR INSERTION:\n"<<p<<".";
cin>>x;
cout<<"Enter position for insertion \n";
cin>>pos;
if(pos==1)
{
a1.insert_beg(x);
}
else if(pos==p+1)
{
a1.insert_end(x);
}
else
{
a1.insert_pos(x,pos);
}
p++;
}
else if(n==4)
{
a1.del_beg();
p--;
}
else if(n==5)
{
a1.del_end();
p--;
}
else if(n==6)
{
cout<<"Enter position for deletion\n";
cin>>pos;
if(pos==1)
{
a1.del_beg();
}
else if(pos==p)
{
a1.del_end();
}
else
a1.del_pos(pos);
p--;
}
else if(n==7)
{
a1.display();
}
else if(n==8)
{
cout<<"Record which you want to search?\n";
cin>>x;
a1.search(x);
}
else
{
cout<<"INVALID INPUT!!\n";
}
cout<<"DO YOU WANT TO CONTINUE?(Enter 1 if yes):\n";
cin>>i;
}
while(i==1);
cout<<"OPERATION COMPLETED!! THANK YOU";
return 0;
}