-
Notifications
You must be signed in to change notification settings - Fork 1
/
circular_linked_list.c
174 lines (165 loc) · 3.47 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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void createClist(struct Node **head)
{
int c = 1;
while (c != -1)
{
struct Node *newnode, *temp;
newnode = (struct Node *)malloc(sizeof(struct Node));
printf("Enter the data:\n");
scanf("%d", &newnode->data);
newnode->next = NULL;
if (*head == NULL)
{
newnode->next = newnode;
*head = newnode;
}
else
{
temp = *head;
while (temp->next != *head)
{
temp = temp->next;
}
temp->next = newnode;
newnode->next = *head;
}
printf("Enter 1 to continue -1 to end\n");
scanf("%d", &c);
}
return;
}
void insertatbeg(struct Node **head)
{
struct Node *newnode, *temp;
if (*head == NULL)
{
createClist(head);
return;
}
newnode = (struct Node *)malloc(sizeof(struct Node));
printf("Enter the data:\n");
scanf("%d", &newnode->data);
temp = *head;
while (temp->next != *head)
{
temp = temp->next;
}
temp->next = newnode;
newnode->next = *head;
*head = newnode;
return;
}
void insert_end(struct Node **head)
{
struct Node *newnode, *temp;
newnode = (struct Node *)malloc(sizeof(struct Node));
printf("Enter the data:\n");
scanf("%d", &newnode->data);
temp = *head;
while (temp->next != *head)
{
temp = temp->next;
}
temp->next = newnode;
newnode->next = *head;
}
void delete_beg(struct Node **head)
{
struct Node *temp, *ptr;
if (*head == NULL)
{
printf("Underflow\n");
}
ptr = temp = *head;
while (temp->next != *head)
{
temp = temp->next;
}
temp->next = ptr->next;
*head = ptr->next;
free(ptr);
return;
}
void delete_end(struct Node **head)
{
struct Node *temp, *ptr;
temp = ptr = *head;
while (temp->next != *head)
{
ptr = temp;
temp = temp->next;
}
ptr->next = *head;
free(temp);
return;
}
void delete_after(struct Node **head)
{
struct Node *ptr, *preptr;
int val;
printf(" Enter the value after which the node has to deleted : \n");
scanf("%d", &val);
ptr = *head;
preptr = ptr;
while (preptr->data != val)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
if (ptr == *head)
*head = preptr->next;
free(ptr);
return;
}
void delete_list(struct Node **head)
{
struct Node *ptr;
ptr = *head;
while (ptr->next != *head)
{
delete_end(head);
}
free(*head);
return;
}
void printClist(struct Node *head)
{
struct Node *temp;
if (head == NULL)
{
printf("list is empty\n");
return;
}
temp = head;
printf("circular linked list:\n");
while (temp->next != head)
{
printf("%d\n", temp->data);
temp = temp->next;
}
printf("%d\n", temp->data);
return;
}
int main()
{
struct Node *head;
head = NULL;
createClist(&head);
printClist(head);
insert_end(&head);
insertatbeg(&head);
printClist(head);
delete_end(&head);
delete_beg(&head);
printClist(head);
delete_list(&head);
return 0;
}