-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOUBLY_LLIST.c
182 lines (164 loc) · 3.14 KB
/
DOUBLY_LLIST.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next, *prev;
}*newnd, *first=NULL, *last=NULL,*temp, *ptr;
struct node* create(int d)
{
newnd=((struct node*)malloc(sizeof(struct node)));
newnd->data=d;
newnd->next=NULL;
newnd->prev=NULL;
return newnd;
}
void firstt()
{
int v;
printf("\nEnter value: ");
scanf("%d", &v);
newnd=create(v);
if(first==NULL && last==NULL)
{
first=last=newnd;
first->next=first->prev=NULL;
last->next=last->prev=NULL;
}
else
{
temp=first;
first=newnd;
first->next=temp;
temp->prev=first;
}
}
void lastt()
{
int v;
printf("\nEnter value: ");
scanf("%d", &v);
newnd=create(v);
last=first;
if(first==NULL && last==NULL)
{
first=last=newnd;
first->next=first->prev=NULL;
last->next=last->prev=NULL;
}
else
{
while(last->next != NULL)
last=last->next;
ptr=last;
last->next=newnd;
last=newnd;
last->next=NULL;
last->prev=ptr;
}
}
void pos()
{
int p;
struct node *pre;
ptr=first;
printf("Enter position: ");
scanf("%d",&p);
int c;
for(c=1;c<p;c++)
{
pre=ptr;
ptr=ptr->next;
}
int v;
printf("\nEnter value: ");
scanf("%d", &v);
newnd=create(v);
pre->next=newnd;
newnd->prev=pre;
newnd->next=ptr;
ptr->prev=newnd;
}
void del()
{
int p;
struct node *pre;
ptr=first;
printf("Enter position: ");
scanf("%d",&p);
int c=1;
for(c=1;c<p;c++)
{
pre=ptr;
ptr=ptr->next;
}
if(ptr == first)
{
first=first->next;
first->prev=NULL;
free(ptr);
}
else if(ptr == last)
{
last=pre;
last->next=NULL;
free(ptr);
}
else
{
pre->next=ptr->next;
ptr->next->prev=pre;
free(ptr);
}
}
void display()
{
ptr=first;
printf("\nLinked List: ");
while(ptr!=NULL)
{
printf("%d ", ptr->data);
ptr=ptr->next;
}
}
void reverse()
{
struct node *current, *temp;
current = first;
while(current != NULL)
{
temp = current->next;
current->next = current->prev;
current->prev = temp;
current = temp;
}
temp = first;
first = last;
last = temp;
printf("LIST REVERSED SUCCESSFULLY.\n");
}
int main()
{
int ch;
while(1)
{
printf("\n\n1->INSERT FROM FRONT\n2-> INSERT FROM END\n3->INSERT AT POS\n4-> DELETE FORM POS\n5-> DISPLAY\n6-> REVERSE\n7-> EXIT\n\nEnter: ");
scanf("%d",&ch);
system("cls");
if(ch==1)
firstt();
else if(ch==2)
lastt();
else if(ch==3)
pos();
else if(ch==4)
del();
else if(ch==5)
display();
else if(ch==6)
reverse();
else
exit(0);
}
return 0;
}