-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublyinkedlist.cpp
227 lines (204 loc) · 3.92 KB
/
doublyinkedlist.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
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *previous,*next;
}node;
typedef struct doublylinkedlist
{
struct node *head;
struct node *tail;
}doublylist;
void init(doublylist *l)
{
l->head = l->tail = NULL;
}
void addfirst(doublylist *l,int data)
{
node *p=(node*)malloc(sizeof(node));
p->data=data;
p->previous='\0';
p->next='\0';
if(l->head == '\0') //if no elemnt exists
{
l->head=p;
l->tail=p;
}
else //1 or more than that exists
{
p->next=l->head;
l->head->previous=p;
l->head=p;
}
}
void addlast(doublylist *l,int data)
{
node *p=(node*)malloc(sizeof(node));
p->data=data;
p->next='\0';
if(l->head== NULL)
{
l->head=p;
l->tail=p;
}
else
p->previous=l->tail;
l->tail->next=p;
l->tail=p;
}
void insat(doublylist *l,int data)
{
node *p=(node*)malloc(sizeof(node));
node *current=l->head;
p->data=data;
doublylist list;
if(l->head==NULL)
{
l->head=p;
l->tail=p;
}
if(p->data < l->head->data)
{ addfirst(&list,data); }
else if(p->data > l->tail->data)
{ addlast(&list,data); }
else
{
while( current->next->data < p->data )
{
current=current->next;
}
p->previous=current;
p->next=current->next;
current->next=p;
p->next->previous=p;
}
}
void display(doublylist *l)
{
node *current;
current=l->head;
int i=1;
while(current!=NULL)
{
printf("%d. %p [ %p | %d | %p ] \n",i,current,current->previous,current->data,current->next);
current=current->next;
i++;
}
}
int deletefirst(doublylist *l)
{
int x;
node *current;
current=l->head;
if(l->head==NULL)
{
printf("LINKED LIST IS EMPTY \n");
return 0;
}
else if(l->head == l->tail) //only 1 elemnt exists
{
x=current->data;
l->head=l->tail=NULL;
free(current);
return x;
}
else{ // mre than 1 element exists
x=l->head->data;
l->head=l->head->next;
l->head->previous=NULL;
free (current);
return x;
}
}
/*int deletefirst(doublylist *l)
{
int x;
node *current;
current=l->head;
if(l->head==NULL)
{
printf("LINKED LIST IS EMPTY \n");
return 0;
}
else
{
x=l->head->data;
l->head=l->head->next;
l->head->previous=NULL;
free(current);
return x;
}
}
*/
int deletelast(doublylist *l)
{
node *current=l->head;
int x;
if(l->head==NULL)
{
printf("LINKED LIST IS EMPTY \n");
return 0;
}
else if(l->head==l->tail) // 1 element
{
x=current->data;
l->head=l->tail=NULL;
free(current);
return x;
}
else
{
x=current->data; //YAHA PE BHI SAME YE LINE NAHI LIKHI THI
l->tail=l->tail->previous;
free(current);
return x;
}
}
int main()
{
doublylist list;
init(&list);
label:
int choice,num1,num2,num3;
printf("-------- Doubly LINKED LIST MENU-------\n");
printf("\n 1.INSERT-insert at first \n 2.INSEND-insert at last \n 3.INSAT-insert in middle \n 4.DELETE FIRST \n 5.DELETE LAST \n 6.DISPLAY \n 7.EXIT \n");
printf("enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Insert a value at first: ");
scanf("%d",&num1);
addfirst(&list,num1);
goto label;
case 2:
printf("Insert a value at last: ");
scanf("%d",&num2);
addlast(&list,num2);
goto label;
case 3:
printf("Insert a value at middle: ");
scanf("%d",&num3);
insat(&list,num3);
goto label;
case 4:
printf("\n deleted value is %d",deletefirst(&list));
goto label;
case 5:
printf("\n deleted value is %d",deletelast(&list));
goto label;
case 6:
printf("displaying value........\n");
display(&list);
goto label;
case 7:
printf("GOODBYE SHITHEAD.....");
exit(0);
goto label;
default:
printf("Invalid input \n");
goto label;
}
return 0;
}