-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIrcDLL.C
227 lines (227 loc) · 5.27 KB
/
CIrcDLL.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
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <stdio.h>
#include <stdlib.h>
struct NODE
{
int data;
struct NODE *next;
struct NODE *prev;
};
struct NODE *addnode(struct NODE *head, int num, int pos)
{
struct NODE *new_node = (struct NODE *)malloc(sizeof(struct NODE));
if (new_node == NULL)
{
printf("FAIL");
}
new_node->data = num;
// IF HEAD==NULL
if (head == NULL)
{
new_node->next = new_node;
new_node->prev = new_node;
head = new_node;
return head;
}
// ADD AT BEGINING
if (pos == 1)
{
struct NODE *temp = head;
while (temp->next != head)
{
temp = temp->next;
}
new_node->prev = temp;
new_node->next = head;
head->prev = new_node;
temp->next = new_node;
head = new_node;
return head;
}
// ADD AT END
else if (pos == -1)
{
struct NODE *temp = head;
while (temp->next != head)
{
temp = temp->next;
}
new_node->prev = temp;
temp->next = new_node;
new_node->next = head;
head->prev = new_node;
return head;
}
// ADD AT NTH POSITION
else
{
struct NODE *temp = head;
struct NODE *temp2;
int i;
for (i = 1; i < pos; i++)
{
temp2 = temp;
temp = temp->next;
}
temp->prev = new_node;
new_node->next = temp;
temp2->next = new_node;
new_node->prev = temp2;
return head;
}
}
struct NODE *delnode(struct NODE *head, int opt)
{
struct NODE *g = head;
struct NODE *f;
if (head == NULL)
{
printf("\nList empty!\n");
return head;
}
// DEL AT BEG
if (opt == 1)
{
f = head;
while (f->next != head)
{
f = f->next;
}
f->next = head->next;
head = head->next;
head->prev = f;
g->prev = NULL;
g->next = NULL;
free(g);
g = NULL;
return head;
}
// DEL AT END
else if (opt == -1)
{
while (g->next != head)
{
f = g;
g = g->next;
}
head->prev = f;
f->next = head;
g->prev = NULL;
g->next = NULL;
free(g);
g = NULL;
return head;
}
else
{
int i;
for (i = 1; i < opt; i++)
{
f = g;
g = g->next;
}
struct NODE *u;
u = g->next;
u->prev = f;
f->next = g->next;
g->next = NULL;
g->prev = NULL;
free(g);
g = NULL;
return head;
}
}
void printlist(struct NODE *n)
{
printf("\n");
struct NODE *j = n;
printf("%d\t", j->data);
j = j->next;
while (j != n)
{
printf("%d\t", j->data);
j = j->next;
}
}
void revprint(struct NODE *n)
{
printf("\n");
struct NODE *p = n;
while (p->next != n)
{
p = p->next;
}
struct NODE *y = p;
printf("%d\t", p->data);
p = p->prev;
while (p != y)
{
printf("%d\t", p->data);
p = p->prev;
}
}
int main()
{
struct NODE *head = NULL;
int num, opt, pos, opt2;
printf("------------- Lets create a circular linked list -------------\n");
do
{
printf("\nEnter appropriate option:\n1. Add node\t2.Delete node\t0. Exit\t");
scanf("%d", &opt);
switch (opt)
{
case 1:
{
printf("\nEnter element you wish to add to node:\t");
scanf("%d", &num);
printf("\n1. Add at beginning\t2. Add at end\t3. Add at nth position\t");
scanf("%d", &opt2);
if (opt2 == 1)
{
head = addnode(head, num, 1);
}
else if (opt2 == 2)
{
head = addnode(head, num, -1);
}
else if (opt2 == 3)
{
printlist(head);
printf("\nEnter position:");
scanf("%d", &pos);
head = addnode(head, num, pos);
}
break;
}
case 2:
{
printf("\nWhere do you wish to delete the node:\n1.At beginning\t2. From end\t3. At nth Position\t");
scanf("%d", &opt2);
if (opt2 == 1)
{
head = delnode(head, 1);
}
else if (opt2 == 2)
{
head = delnode(head, -1);
}
else if (opt2 == 3)
{
printf("\nThe existing list is:");
printlist(head);
printf("\nEnter the position which you wish to delete:\t");
scanf("%d", &pos);
head = delnode(head, pos);
}
break;
}
default:
break;
}
} while (opt != 0);
printf("\n-------The elements in the list are-------");
printlist(head);
printf("\n---------The elements of list in reverse order are:----------");
revprint(head);
return 0;
}