-
Notifications
You must be signed in to change notification settings - Fork 0
/
cll.c
192 lines (192 loc) · 3.98 KB
/
cll.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
/*Implement the following operations on a circular linked list using C.
Create
traverse
Insert
Delete
reverse
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} * first;
void create(int);
void display();
void insert(int, int);
void delete (int,int);
void reverse();
int main()
{
first = NULL;
int n, a, pos;
printf("Enter the number of nodes \n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter data \n");
scanf("%d", &a);
create(a);
}
printf("Circular linked list: \n");
display();
printf("\nEnter position to insert \n");
scanf("%d",&pos);
insert(pos, n);
printf("Circular linked list after insertion: \n");
display();
printf("\nEnter position to delete \n");
scanf("%d",&pos);
delete(pos,n+1);
printf("Circular linked list after deletion: \n");
display();
reverse();
printf("\nReversed Circular linked list: \n");
display();
return 0;
}
void create(int a)
{
struct node *temp, *ptr;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = a;
temp->next = NULL;
if (first == NULL)
{
first = temp;
}
else
{
ptr = first;
while (ptr->next != first)
{
ptr = ptr->next;
}
ptr->next = temp;
}
temp->next = first;
}
void display()
{
struct node *ptr;
ptr = first;
while (ptr->next != first)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("%d", ptr->data);
}
void insert(int pos, int n)
{
struct node *temp, *ptr,*ptr1;
int b;
printf("Enter data of the node to be inserted \n");
scanf("%d",&b);
if (pos == 1)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->data = b;
temp->next = first;
first = temp;
ptr = first;
ptr=ptr->next;
while (ptr->next != first->next)
{
ptr = ptr->next;
}
ptr->next = first;
}
else if (pos == n)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->data = b;
temp->next = first;
ptr = first;
while (ptr->next != first)
{
ptr = ptr->next;
}
ptr->next = temp;
}
else
{
temp = (struct node *)malloc(sizeof(struct node));
temp->data = b;
ptr = first;
ptr1 = first;
for (int i = 0; i < (pos - 1); i++)
{
ptr = ptr->next;
if (i < (pos - 2))
{
ptr1 = ptr1->next;
}
}
temp->next = ptr;
ptr1->next = temp;
}
}
void delete(int pos,int n)
{
struct node * ptr,*ptr1;
ptr=first;
ptr1=first;
if(pos==1)
{
while(ptr1->next!=NULL)
ptr1=ptr1->next;
first=first->next;
ptr=NULL;
free(ptr);
ptr1->next=first;
}
else if(pos==n)
{
for(int i=1;i<(n-1);i++)
{
ptr=ptr->next;
ptr1=ptr1->next;
}
ptr1=ptr1->next;
ptr->next=ptr1->next;
ptr=NULL;
free(ptr);
}
else
{
for(int i=1;i<=(pos-2);i++)
{
ptr=ptr->next;
ptr1=ptr1->next;
}
ptr1=ptr1->next;
ptr->next=ptr1->next;
ptr=NULL;
free(ptr);
}
}
/* 100-->1|101-->2|102-->3|103-->4|104-->5|100
1|104<--2|100<--3|101<--4|102<--5|103<--104
*/
void reverse()
{
struct node *ptr,*ptr1,*ptr3;
ptr=first;
ptr1=first;
ptr1=ptr1->next;
ptr3=first;
ptr3=ptr3->next;
ptr3=ptr3->next;
while(ptr3!=first)
{
ptr1->next=ptr;
ptr=ptr1;
ptr1=ptr3;
ptr3=ptr3->next;
}
ptr1->next=ptr;
first->next=ptr1;
first=ptr1;
}