-
Notifications
You must be signed in to change notification settings - Fork 2
/
circular_linked_list.c
201 lines (193 loc) · 3.8 KB
/
circular_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
196
197
198
199
200
201
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
struct node
{
int data;
struct node *next;
} *head = 0;
int count = 0, deletedvalue;
void display()
{
struct node *temp;
int i = 1;
temp = head;
do
{
printf("the value of %d node is %d\n", i, temp->data);
i++;
temp = temp->next;
} while (temp != head);
}
void delete_firstnode()
{
struct node *temp, *last;
temp = last = head;
while (last->next != head)
{
last = last->next;
}
head = temp->next;
last->next = temp->next;
deletedvalue = temp->data;
free(temp);
}
void delete_lastnode()
{
struct node *temp, *last;
temp = last = head;
while (last->next != head)
{
temp = last;
last = last->next;
}
temp->next = head;
deletedvalue = last->data;
free(last);
}
void deletion()
{
if (head == 0)
{
printf("underflow");
return;
}
int pos;
printf("enter the position of node you want to delete");
scanf("%d", &pos);
if (pos > count)
{
printf("deletion is not possible");
return;
}
if (pos == 1)
{
delete_firstnode();
}
else if (pos == count)
{
delete_lastnode();
}
else if (pos < count)
{
int i = 1;
struct node *temp, *prev;
temp = prev = head;
while (i < pos)
{
prev = temp;
temp = temp->next;
i++;
}
prev->next = temp->next;
deletedvalue = temp->data;
free(temp);
}
count--;
printf("deleted value is %d", deletedvalue);
display();
}
void insertatend(int value)
{
struct node *temp, *new;
temp = head;
new = (struct node *)malloc(sizeof(struct node));
while (temp->next != head)
{
temp = temp->next;
}
new->data = value;
new->next = head;
temp->next = new;
printf("insert at end ");
}
void insertatbeg(int value)
{
struct node *temp, *new;
temp = head;
new = (struct node *)malloc(sizeof(struct node));
while (temp->next != head)
{
temp = temp->next;
}
new->data = value;
new->next = head;
head = new;
temp->next = new;
}
void insertion()
{
int pos, i = 2, value;
struct node *temp, *new;
printf("enter the value you want to insert");
scanf("%d", &value);
printf("enter the position where you want to insert");
scanf("%d", &pos);
if (pos == 1)
{
insertatbeg(value);
}
else if (pos == count + 1)
{
insertatend(value);
}
else
{
temp = head;
new = (struct node *)malloc(sizeof(struct node));
while (i < pos)
{
temp = temp->next;
}
new->data = value;
new->next = temp->next;
temp->next = new;
}
count++;
display();
}
void list()
{
int choice = 1;
struct node *new, *temp;
while (choice)
{
new = (struct node *)malloc(sizeof(struct node));
printf("enter the value of %d node", count + 1);
scanf("%d", &new->data);
if (head == 0)
{
head = temp = new;
new->next = head;
}
else
{
temp->next = new;
new->next = head;
temp = temp->next;
}
printf("press 0 for exit");
scanf("%d", &choice);
count++;
}
}
int main()
{
int temp = 1, value;
list();
while (temp)
{
printf("Enter the choice\n 1: for INSERTION\n 2: for DELETION");
scanf("%d", &value);
if(value==1)
insertion();
else if (value==2)
deletion();
else
printf("wrong input");
printf("PRESS 1 TO CONTIUE/0 FOR EXIT");
scanf("%d", &temp);
}
return 0;
}