-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdoublylinkedlist.c
214 lines (198 loc) · 3.17 KB
/
doublylinkedlist.c
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start=0,*newnode,*temp,*ptr,*prev,*nextnode;
void create()
{ int choice=1,x;
while(choice)
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nenter the data :");
scanf("%d",&x);
newnode->data=x;
newnode->next=0;
if(start==0)
start=temp=newnode;
else
{
temp->next=newnode;
temp=newnode;
}
temp->next=start;
printf("enter 1 to add ,0 to exit:");
scanf("%d",&choice);
}
}
void display()
{
temp=start;
if(start==0)
printf("the list is empty");
else
{ printf("\n Displaying list:");
while(temp->next!=start)
{
printf("%d ",temp->data);
temp=temp->next;
60
}printf("%d",temp->data);}
}
void delbeg()
{
temp=start;
if(start==0)
printf("\n The list is empty");
else{
while(temp->next!=start)
{
temp=temp->next;
}
temp->next=start->next;
free(start);
start=temp->next;}
}
void delend() {
prev=ptr=start;
if(start==0)
printf("\n The list is empty");
else {
while(ptr->next!=start)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=start;
free(ptr);} }
void delpos()
{
int pos,i=1,l;
printf("enter the position:");
scanf("%d",&pos);
l=count();
if(pos<0||pos>l)
printf("
\n invalid position");
else
{
ptr=start;
while(i<pos-1)
{
ptr=ptr->next;
i++;
}
nextnode=ptr->next;
ptr->next=nextnode->next;
free(nextnode);
}
}
int count()
{
temp=start;
int count=0;
if(start==0)
printf("count%d:",count);
else
{
while(temp->next!=start)
{
count++;
temp=temp->next;
}
count=count+1;
return count;
}}
void insertbeg()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter the data:");
scanf("%d",&newnode->data);
newnode->next=start;
ptr=start;
while(ptr->next!=start)
{
ptr=ptr->next;
}
ptr->next=newnode;
start=newnode;
return;
}
void insertpos()
{ int pos,i=1,l;
printf("enter the position:");
scanf("%d",&pos);
l=count();
if(pos<0||pos>l)
printf("\n invalid position");
else
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter the data:");
scanf("%d",&newnode->data);
ptr=start;
while(i<pos-1)
{
ptr=ptr->next;
i++;
}
newnode->next=ptr->next;
ptr->next=newnode;
}
}
void insertend()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter the data:");
scanf("%d",&newnode->data);
newnode->next=start;
ptr=start;
while(ptr->next!=start)
{
ptr=ptr->next;
}
ptr->next=newnode;
return;
}
int main()
{ int choice;
create();
do
{
printf("\nMenu\n1.Insert at beginning\n2.Insert at position\n3.Insert at end\n4.Deletion
at beginning\n5.Deletion at position\n6.Delete from End \n7.Display\n8.Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insertbeg();
break;
case 2:
insertpos();
break;
case 3:
insertend();
break;
case 4:
delbeg();
break;
case 5:
delpos();
break;
case 6:
delend();
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("Invalid choice");
}
}
while(choice!=8);
return 0;
}