1// Aim: Program to perform Circular Linked List operations (Insertion, Deletion).
2#include<stdio.h>
3#include<stdlib.h>
4
5struct node {
6 int data;
7 struct node *link;
8};
9
10struct node *head = NULL;
11
12void insertfront(int x){
13 struct node *new = (struct node *)malloc(sizeof(struct node));
14 new->data = x;
15 if(head == NULL){
16 head = new;
17 new->link = head;
18 }else{
19 struct node *ptr = head;
20 while(ptr->link != head){
21 ptr = ptr->link;
22 }
23 ptr->link = new;
24 new->link = head;
25 head = new;
26 }
27}
28
29void insertend(int x){
30 struct node *new = (struct node *)malloc(sizeof(struct node));
31 new->data = x;
32 if(head == NULL){
33 head = new;
34 new->link = head;
35 }else{
36 struct node *ptr = head;
37 while(ptr->link != head){
38 ptr = ptr->link;
39 }
40 ptr->link = new;
41 new->link = head;
42 }
43}
44
45void insertafter(int key, int x){
46 if(head == NULL){
47 printf("List is empty\n");
48 }
49 else{
50 struct node *ptr = head;
51 while(ptr->link != head && ptr->data != key){
52 ptr = ptr->link;
53 if(ptr == head){
54 break;
55 }
56 }
57 if(ptr->data == key){
58 struct node *new = (struct node *)malloc(sizeof(struct node));
59 new->data = x;
60 new->link = ptr->link;
61 ptr->link = new;
62 }else{
63 printf("Key not found\n");
64 }
65 }
66}
67
68void deletefront(){
69 if(head == NULL){
70 printf("List is empty\n");
71 }else if(head->link == head){
72 struct node *temp = head;
73 head = NULL;
74 free(temp);
75 }else{
76 struct node *ptr = head;
77 struct node *temp = head;
78 while(ptr->link != head){
79 ptr = ptr->link;
80 }
81 head = head->link;
82 ptr->link = head;
83 free(temp);
84 }
85}
86
87void deleteend(){
88 if(head == NULL){
89 printf("List is empty\n");
90 }else if(head->link == head){
91 struct node *temp = head;
92 head = NULL;
93 free(temp);
94 }else{
95 struct node *prev = head;
96 struct node *curr = head->link;
97 while(curr->link != head){
98 prev = curr;
99 curr = curr->link;
100 }
101 prev->link = head;
102 free(curr);
103 }
104}
105
106void deletekey(int key){
107 if(head == NULL){
108 printf("List is empty\n");
109 }else if(head->link == head){
110 if(head->data == key){
111 struct node *temp = head;
112 head = NULL;
113 free(temp);
114 }else{
115 printf("Key not found\n");
116 }
117 }else if(head->data == key){
118 struct node *ptr = head;
119 struct node *temp = head;
120 while(ptr->link != head){
121 ptr = ptr->link;
122 }
123 head = head->link;
124 ptr->link = head;
125 free(temp);
126 }else{
127 struct node *prev = head;
128 struct node *curr = head->link;
129 while(curr != head && curr->data != key){
130 prev = curr;
131 curr = curr->link;
132 }
133 if(curr->data == key){
134 prev->link = curr->link;
135 free(curr);
136 }else{
137 printf("Key not found\n");
138 }
139 }
140}
141
142void display(){
143 if(head == NULL){
144 printf("List is empty\n");
145 }else{
146 struct node *ptr = head;
147 do{
148 printf("%d ", ptr->data);
149 ptr = ptr->link;
150 }while(ptr != head);
151 printf("\n");
152 }
153}
154
155void main(){
156 int ch, x, key;
157 while(1){
158 printf("1. Insert front\n");
159 printf("2. Insert end\n");
160 printf("3. Insert after\n");
161 printf("4. Delete front\n");
162 printf("5. Delete end\n");
163 printf("6. Delete key\n");
164 printf("7. Display\n");
165 printf("8. Exit\n");
166 printf("Enter your choice: ");
167 scanf("%d", &ch);
168 switch(ch){
169 case 1:
170 printf("Enter the element to insert: ");
171 scanf("%d", &x);
172 insertfront(x);
173 break;
174 case 2:
175 printf("Enter the element to insert: ");
176 scanf("%d", &x);
177 insertend(x);
178 break;
179 case 3:
180 printf("Enter the key: ");
181 scanf("%d", &key);
182 printf("Enter the element to insert: ");
183 scanf("%d", &x);
184 insertafter(key, x);
185 break;
186 case 4:
187 deletefront();
188 break;
189 case 5:
190 deleteend();
191 break;
192 case 6:
193 printf("Enter the key: ");
194 scanf("%d", &key);
195 deletekey(key);
196 break;
197 case 7:
198 display();
199 break;
200 case 8:
201 exit(0);
202 default:
203 printf("Invalid choice\n");
204 }
205 }
206}
207