-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathGeneralizedLinkedlist.cpp
240 lines (214 loc) · 5.07 KB
/
GeneralizedLinkedlist.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
/* AUTHOR - Sayantan Banerjee (2018IMT-093)*/
#include <bits/stdc++.h>
using namespace std;
struct node
{
int value;
struct node *next;
}*head=NULL;
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
void insertBegin()
{
struct node* newNode=(struct node*)malloc(sizeof(struct node));
cout<<"Enter the value to be inserted in linked list : ";
cin>>newNode->value;
newNode->next=NULL;
if(head!=NULL)
newNode->next=head;
head=newNode;
}
void insertMiddle()
{
struct node* newNode=(struct node*)malloc(sizeof(struct node));
cout<<"Enter the value to be inserted in linked list : ";
cin>>newNode->value;
newNode->next=NULL;
if(head==NULL)
{
head=newNode;
return;
}
cout<<"\nEnter the element after which you want to enter the new node : ";
int index;
cin>>index;
struct node* currentNode=(struct node*)malloc(sizeof(struct node));
currentNode=head;
bool flag=true;
while(true)
{
if(currentNode->value==index)
break;
if(currentNode->next==NULL)
{
flag=false;
break;
}
currentNode=currentNode->next;
}
if(!flag)
{
cout<<"\nElement not found!!";
}
else
{
newNode->next=currentNode->next;
currentNode->next=newNode;
}
}
void insertEnd()
{
struct node* newNode=(struct node*)malloc(sizeof(struct node));
cout<<"Enter the value to be inserted in linked list : ";
cin>>newNode->value;
newNode->next=NULL;
struct node* currentNode=(struct node*)malloc(sizeof(struct node));
currentNode=head;
if(head==NULL)
{
head=newNode;
return;
}
while(currentNode->next!=NULL)
currentNode=currentNode->next;
newNode->next=currentNode->next;
currentNode->next=newNode;
}
void insertion()
{
cout<<"\n\nINSERTION in linked list\n";
cout<<"\nWhere would you like to do? ";
cout<<"\n 1. Beginning";
cout<<"\n 2. Middle";
cout<<"\n 3. End\n";
int choice;
cin>>choice;
switch(choice)
{
case 1:insertBegin();break;
case 2:insertMiddle();break;
case 3:insertEnd();break;
default: cout<<"Invalid choice...breaking";
}
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
void deleteBegin()
{
if(head==NULL)
{
cout<<"\nUNDERFLOW";
return;
}
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp=head;
head=head->next;
free(temp);
}
void deleteMiddle()
{
if(head==NULL)
{
cout<<"\nUNDERFLOW!";
return;
}
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp=head;
cout<<"Enter the Element to be deleted ";
int item;
cin>>item;
if(head->value==item)
{
head=head->next;
free(temp);
return;
}
while(temp->next!=NULL)
{
if(temp->next->value==item)
{
struct node* t=temp->next;
temp->next=temp->next->next;
free(t);
return;
}
temp=temp->next;
}
cout<<"\nElement not found";
}
void deleteEnd()
{
if(head==NULL)
{
cout<<"\nUnderflow!";
return;
}
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp=head;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
struct node* t=temp->next;
temp->next=NULL;
free(t);
}
void deletion()
{
cout<<"\n\nDELETION in linked list\n";
cout<<"\nWhere would you like to do? ";
cout<<"\n 1. Beginning";
cout<<"\n 2. Middle";
cout<<"\n 3. End\n";
int choice;
cin>>choice;
switch(choice)
{
case 1:deleteBegin();break;
case 2:deleteMiddle();break;
case 3:deleteEnd();break;
default: cout<<"Invalid choice...breaking";
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
void display()
{
struct node* currentNode=(struct node*)malloc(sizeof(struct node));
currentNode=head;
if(currentNode==NULL)
{
cout<<"\nEmpty list ";
return;
}
while(currentNode!=NULL)
{
cout<<currentNode->value<<" -> ";
currentNode=currentNode->next;
}
}
int main()
{
char ch;
do
{
cout<<"LINKED LIST\n";
cout<<"\nWhat would you like to do? ";
cout<<"\n 1. Insertion";
cout<<"\n 2. Deletion";
cout<<"\n 3. Display\n";
int choice;
cin>>choice;
switch(choice)
{
case 1:insertion();break;
case 2:deletion();break;
case 3:display();break;
default: cout<<"\nInvalid choice...breaking";
}
cout<<"\nWould you like to operate again?(y/n)";
fflush(stdin);
cin>>ch;
}while(ch=='y');
return 0;
}