forked from 0xali3n/Hactoberfest-Beginner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular Doubly Linked List.c
195 lines (192 loc) · 4.19 KB
/
Circular Doubly Linked List.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
#include <stdio.h>
struct Node
{
int value;
struct Node *next,*prev;
}*start;
void insertbeg(int v)
{
struct Node *n;
n=(struct Node*)malloc(sizeof(struct Node));
n->value=v;
n->next=NULL;
n->prev=NULL;
if(start==NULL){
start=n;
start->next=start;
start->prev=start;
}
else{
n->prev=start->prev;
n->next=start;
start->prev=n;
start=n;
(n->prev)->next=start;
}
}
void deletebeg()
{
struct Node* n;
if(start==NULL)printf("List is empty\n");
else
{
n=start;
start=n->next;
start->prev=n->prev;
(start->prev)->next=start;
free(n);
printf("Value deleted successfully\n");
}
}
void insertend(int v){
struct Node* n;
struct Node* k;
n=(struct Node*)malloc(sizeof(struct Node));
n->value=v;
n->next=NULL;
n->prev=NULL;
if(start==NULL){
start=n;
start->next=start;
start->prev=start;
}
else
{
k=start->prev;
k->next=n;
n->prev=k;
n->next=start;
start->prev=n;
}
}
void deleteend()
{
struct Node* n;
struct Node* k;
if(start==NULL)printf("Invalid operation, list is empty\n");
else
{
n=start->prev;
k=n->prev;
start->prev=k;
k->next=start;
free(n);
printf("Value deleted successfully\n");
}
}
void insertbypos(int v,int p)
{
struct Node* n;
struct Node* k;
n=(struct Node*)malloc(sizeof(struct Node));
n->value=v;
n->next=NULL;
n->prev=NULL;
if(p==1 && start==NULL)
{
start=n;
start->next=start;
start->prev=start;
}
else if(p!=1 && start==NULL)printf("Invalid,List is empty\n");
else
{
k=start;
int p1=1;
while(p1<p-1 && k->next!=start)
{
k=k->next;
p1++;
}
if(p1==p-1){
n->next=k->next;
(n->next)->prev=n;
n->prev=k;
k->next=n;
}
else printf("Not available\n");
}
}
void deletebypos(int p)
{
struct Node* n;
struct Node* k;
if(start!=NULL && p==1)deletebeg();
else if(start==NULL)printf("List is empty\n");
else
{
k=start;
int p1=1;
while(p1<p-1 && (k->next)->next!=start)
{
k=k->next;
p1++;
}
n=k->next;
k->next=n->next;
(n->next)->prev=k;
free(n);
printf("Value deleted successfully\n");
}
}
void display()
{
struct Node *n;
if(start==NULL)printf("List is empty\n");
else if(start->next==start)printf("%d",n->value);
else{
n=start;
while(n->next!=start){
printf("%d ",n->value);
n=n->next;
}
printf("%d",n->value);
}
printf("\n");
}
void main()
{ int choice,n,k=1,pos;
while(k==1){
printf("Press 1 for insertion from beginning\nPress 2 for deletion from beginning\nPress 3 for insertion from end\nPress 4 for deletion from end\nPress 5 for insertion by position\nPress 6 for deletion by position\nPress 7 for display\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter value to be inserted ");
scanf("%d",&n);
insertbeg(n);
break;
case 2:
deletebeg();
break;
case 3:
printf("enter value to be inserted ");
scanf("%d",&n);
insertend(n);
break;
case 4:
deleteend();
break;
case 5:
printf("Enter value to be inserted ");
scanf("%d",&n);
printf("Enter position ");
scanf("%d",&pos);
insertbypos(n,pos);
break;
case 6:
printf("Enter position to be deleted ");
scanf("%d",&pos);
deletebypos(pos);
break;
case 7:
display();
break;
default:
printf("Invalid choice entered\n");
break;
}
printf("Press 1 for repeatition ");
scanf("%d",&k);
}
}