-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcircular_linked_list.c
More file actions
228 lines (191 loc) · 5.41 KB
/
Copy pathcircular_linked_list.c
File metadata and controls
228 lines (191 loc) · 5.41 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Program: Circular Linked List Operations
* Description: Implementation of circular linked list with insert, delete, and display
* Author: Amey Thakur
* Reference: https://github.com/Amey-Thakur/DATA-STRUCTURES-AND-DATA-STRUCTURES-LAB
*/
#include <stdio.h>
#include <stdlib.h>
// Node structure for circular linked list
struct Node {
int data;
struct Node* next;
};
// Global pointers
struct Node* head = NULL;
// Function prototypes
struct Node* createNode(int value);
void insertNode(int value);
void deleteNode(int value);
void displayList();
void freeList();
int main() {
int choice, value;
printf("=== Circular Linked List Operations ===\n");
while (1) {
printf("\n--- Menu ---\n");
printf("1. Insert Node\n");
printf("2. Delete Node\n");
printf("3. Display List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter node value: ");
scanf("%d", &value);
insertNode(value);
printf("Node inserted successfully!\n");
break;
case 2:
if (head == NULL) {
printf("List is empty!\n");
} else {
printf("Enter node value to delete: ");
scanf("%d", &value);
deleteNode(value);
}
break;
case 3:
displayList();
break;
case 4:
freeList();
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid choice! Please enter 1-4.\n");
}
}
return 0;
}
/*
* Function: createNode
* Description: Creates a new node with given value
* Parameters: value - Data to store in node
* Returns: Pointer to newly created node
*/
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
/*
* Function: insertNode
* Description: Inserts a new node at the end of circular list
* Parameters: value - Data to insert
* Note: In circular list, last node's next points to head
*/
void insertNode(int value) {
struct Node* newNode = createNode(value);
// If list is empty, create first node pointing to itself
if (head == NULL) {
head = newNode;
newNode->next = head; // Circular link
return;
}
// Traverse to last node
struct Node* current = head;
while (current->next != head) {
current = current->next;
}
// Insert new node at end and maintain circular link
current->next = newNode;
newNode->next = head;
}
/*
* Function: deleteNode
* Description: Deletes first occurrence of node with given value
* Parameters: value - Value of node to delete
*/
void deleteNode(int value) {
if (head == NULL) {
printf("List is empty!\n");
return;
}
struct Node* current = head;
struct Node* previous = NULL;
// Find last node (to maintain circular link)
struct Node* last = head;
while (last->next != head) {
last = last->next;
}
// If head node contains the value
if (current->data == value) {
// Only one node in list
if (current->next == head) {
free(current);
head = NULL;
} else {
// Update last node to point to new head
last->next = current->next;
head = current->next;
free(current);
}
printf("Node with value %d deleted successfully!\n", value);
return;
}
//Search for node to delete
previous = current;
current = current->next;
while (current != head) {
if (current->data == value) {
previous->next = current->next;
free(current);
printf("Node with value %d deleted successfully!\n", value);
return;
}
previous = current;
current = current->next;
}
printf("Node with value %d not found!\n", value);
}
/*
* Function: displayList
* Description: Displays all nodes in the circular linked list
*/
void displayList() {
if (head == NULL) {
printf("\nList is empty!\n");
return;
}
printf("\nCircular Linked List: ");
struct Node* current = head;
do {
printf("%d", current->data);
if (current->next != head) {
printf(" --> ");
}
current = current->next;
} while (current != head);
printf(" --> (back to head: %d)\n", head->data);
}
/*
* Function: freeList
* Description: Frees all allocated memory in the list
*/
void freeList() {
if (head == NULL) {
return;
}
struct Node* current = head;
struct Node* next;
// Break circular link
struct Node* last = head;
while (last->next != head) {
last = last->next;
}
last->next = NULL;
// Free all nodes
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
head = NULL;
}