forked from anish2210/hacktober2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circuler Linked List.c
181 lines (154 loc) · 3.77 KB
/
Circuler 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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node* create(int value)
{
struct node* newNode = (struct node*)malloc(sizeof(struct node));
if (newNode)
{
newNode->data = value;
newNode->next = newNode;
}
return newNode;
}
struct node* inBegin(struct node* head, int value)
{
struct node* newNode = create(value);
if (!newNode)
return head;
if (head == NULL)
return newNode;
newNode->next = head;
struct node* temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = newNode;
return newNode;
}
struct node* inEnd(struct node* head, int value)
{
struct node* newNode = create(value);
if (!newNode)
return head;
if (head == NULL)
return newNode;
struct node* temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = newNode;
newNode->next = head;
return head;
}
void display(struct node* head)
{
if (head == NULL)
{
printf("List is empty.\n");
return;
}
struct node* temp = head;
do
{
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n");
}
struct node* deleteNode(struct node* head, int value)
{
if (head == NULL)
{
printf("\nList is empty.\n");
return NULL;
}
struct node* curr = head;
struct node* prev = NULL;
while (curr->data != value)
{
if (curr->next == head)
{
printf("\nValue not present.\n");
return head;
}
prev = curr;
curr = curr->next;
}
if (prev == NULL)
{ // If the first node is to be deleted
struct node* temp = head;
while (temp->next != head)
temp = temp->next;
temp->next = head->next;
free(head);
head = temp->next;
}
else
{
prev->next = curr->next;
free(curr);
}
return head;
}
void freeList(struct node* head)
{
if (head == NULL)
return;
struct node* temp = head;
struct node* nextNode;
do
{
nextNode = temp->next;
free(temp);
temp = nextNode;
} while (temp != head);
}
int main()
{
struct node* head = NULL;
int choice, value;
while(1)
{
printf("\n\nCircular Linked List Operations:\n");
printf("1. Insert at Begin\n");
printf("2. Insert at End\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter value to insert at begin: ");
scanf("%d", &value);
head = inBegin(head, value);
break;
case 2:
printf("\nEnter value to insert at end: ");
scanf("%d", &value);
head = inEnd(head, value);
break;
case 3:
printf("\nEnter value to delete: ");
scanf("%d", &value);
head = deleteNode(head, value);
break;
case 4:
printf("\nCircular Linked List: ");
display(head);
break;
case 5:
freeList(head);
printf("\nExiting..\n");
exit(0);
break;
default:
printf("Invalid choice.\n");
}
}
return 0;
}