-
Notifications
You must be signed in to change notification settings - Fork 93
/
DoublyLinkedList.c
194 lines (178 loc) · 3.63 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
//doubly linked list
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
} *head = NULL;
int size()
{
int c = 1;
struct node *p = head;
if (head == NULL)
{
printf("Linked List is empty.");
return 0;
}
while (p->next != NULL)
{
c++;
p = p->next;
}
return c;
}
void display()
{
struct node *p = head;
while (p->next != NULL)
{
printf("%d <-> ", p->data);
p = p->next;
}
printf("%d", p->data);
}
void insertHead(int data)
{
struct node *new = (struct node *)malloc(sizeof(struct node));
struct node *p = head;
new->data = data;
new->prev = NULL;
new->next = head;
head = new;
}
void insertTail(int data)
{
struct node *new = (struct node *)malloc(sizeof(struct node));
if (head == NULL)
{
insertHead(data);
return;
}
struct node *p = head;
while (p->next != NULL)
p = p->next;
new->data = data;
p->next = new;
new->prev = p;
new->next = NULL;
}
void insertPos(int pos, int data)
{
struct node *new = (struct node *)malloc(sizeof(struct node));
if (pos == 1)
{
insertHead(data);
return;
}
new->data = data;
struct node *p = head;
struct node *q = head;
for (int i = 1; i < pos - 1; i++)
{
p = p->next;
}
for (int i = 1; i < pos; i++)
{
q = q->next;
}
p->next = new;
new->prev = p;
new->next = q;
q->prev = new;
}
void delHead()
{
struct node *p = head;
struct node *q = head->next;
free(p);
head = q;
q->prev = NULL;
}
void delTail()
{
struct node *p = head;
struct node *q = head;
while (p->next != NULL)
p = p->next;
for (int i = 1; i < size() - 1; i++)
q = q->next;
q->prev = p->prev;
free(p);
q->next = NULL;
}
void delPos(int pos)
{
if (pos == 1)
{
delHead();
return;
}
if (pos == size())
{
delTail();
return;
}
struct node *p = head;
struct node *q = head;
for (int i = 1; i < pos - 1; i++)
{
p = p->next;
}
for (int i = 1; i <= pos; i++)
{
q = q->next;
}
free(p->next);
p->next = q;
q->prev = p;
}
int main()
{
printf("a. Display the elements of the linked list\n");
printf("b. Insert an element at the beginning of the list\n");
printf("c. Insert an element at the end of the list\n");
printf("d. Insert an element at a specified position in the list\n");
printf("e. Deletion of an element from the beginning of the list\n");
printf("f. Deletion of an element from the end of the list\n");
printf("g. Deletion of an element from a specified position in the list\n");
while (1)
{
int data,pos;
char choice;
scanf(" %c", &choice);
switch (choice)
{
case 'a':
display();
break;
case 'b':
scanf("%d", &data);
insertHead(data);
break;
case 'c':
scanf("%d", &data);
insertTail(data);
break;
case 'd':
scanf("%d %d", &pos, &data);
insertPos(pos, data);
break;
case 'e':
delHead();
break;
case 'f':
delTail();
break;
case 'g':
scanf("%d", &pos);
delPos(pos);
break;
default:
printf("!!!ERROR!!!");
return 0;
}
}
return 0;
}