-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDOUBLY-LINKED_LIST.cpp
269 lines (243 loc) · 3.93 KB
/
DOUBLY-LINKED_LIST.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
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* root=NULL;
int len;
void append(void);
void addatbegin(void);
void addatpos(void);
int length(void);
void display(void);
void deletion(void);
void search(void);
int main()
{
int ch;
while(1)
{
cout<<"---------------------------\n";
cout<<"1. Append\n";
cout<<"2. Insert at begin\n";
cout<<"3. Insert at postion\n";
cout<<"4. Length\n";
cout<<"5. Display\n";
cout<<"6. Delete\n";
cout<<"7. Search\n";
cout<<"8. Quit\n";
cout<<"Enter your choice : \n";
cin>>ch;
switch(ch)
{
case 1: append();
break;
case 2: addatbegin();
break;
case 3: addatpos();
break;
case 4: len=length();
cout<<"Length is : \n"<<len<<endl;
break;
case 5: display();
break;
case 6: deletion();
break;
case 7: search();
break;
case 8: exit(1);
default :
system("cls");
cout<<"---------------------------\n";
cout<<"Invalid input\n";
}
}
return 0;
}
void append()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
cout<<"Enter node data : \n";
cin>>temp->data;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
{
root=temp;
}
else
{
struct node *p;
p=root;
while(p->right!=NULL)
{
p=p->right;
}
p->right=temp;
temp->left=p;
}
}
void addatbegin()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
cout<<"Enter node data : \n";
cin>>temp->data;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
{
root=temp;
}
else
{
temp->right=root;
root->left=temp;
root=temp;
}
}
void addatpos()
{
struct node *temp,*p;
int loc,i=1;
cout<<"Enter location : \n";
cin>>loc;
if(loc>length())
{
system("cls");
cout<<"---------------------------\n";
cout<<"Invalid location... \n";
cout<<"Currently list has "<<length()<<" nodes\n";
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
cout<<"Enter node data : \n";
cin>>temp->data;
temp->left=NULL;
temp->right=NULL;
p=root;
while(i<loc-1)
{
p=p->right;
i++;
}
temp->right=p->right;
p->right->left=temp;
temp->left=p;
p->right=temp;
}
}
int length()
{
int c=0;
struct node* temp;
temp=root;
while(temp!=NULL)
{
c++;
temp=temp->right;
}
return c;
}
void display()
{
struct node* temp;
temp=root;
if(temp==NULL)
{
system("cls");
cout<<"---------------------------\n";
cout<<"List is empty... \n";
}
else
{
system("cls");
cout<<"Elements are : \n";
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->right;
}
cout<<"\n";
}
}
void deletion()
{
struct node* temp;
int loc;
cout<<"Enter the location to delete : \n";
cin>>loc;
if(loc>length())
cout<<"Invalid location...\n";
else if(loc==1)
{
temp=root;
root=temp->right;
temp->right->left=NULL;
temp->right=NULL;
free(temp);
}
else
{
struct node*p=root,*q;
int i=1;
while(i<loc-1)
{
p=p->right;
i++;
}
q=p->right;
if(q->right==NULL)
{
p->right=NULL;
q->left=NULL;
free(q);
}
else
{
q=p->right;
q->right->left=q->left;
p->right=q->right;
q->right=NULL;
q->left=NULL;
free(q);
}
}
}
void search()
{
int ele,k=1;
if(root==NULL)
{
cout<<"No elements to be searched...\n";
}
else
{
int count=0;
cout<<"Enter element to be searched : \n";
cin>>ele;
struct node* temp;
temp=root;
while(temp!=NULL)
{
if(temp->data==ele)
{
cout<<"Element found at position "<<k<<endl;
count++;
break;
}
k++;
temp=temp->right;
}
if(count==0)
{
cout<<"Element is not present...\n";
}
}
}