-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_linked_list_manager.c
More file actions
211 lines (192 loc) · 5.6 KB
/
dynamic_linked_list_manager.c
File metadata and controls
211 lines (192 loc) · 5.6 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
//Developed by JIMIT PANDYA
//Developed a C program to manage various operations on Dynamic link list.
//You could display the list, modify it as well as search within the database.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
// Function to create a new node
Node *createNode(int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the beginning of the linked list
Node *insertAtBeginning(Node *head, int data) {
Node *newNode = createNode(data);
newNode->next = head;
return newNode;
}
// Function to insert a node at the end of the linked list
void insertAtEnd(Node *head, int data) {
Node *newNode = createNode(data);
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
// Function to insert a node at a specific position in the linked list
void insertAtPosition(Node *head, int data, int position) {
Node *newNode = createNode(data);
Node *current = head;
for (int i = 1; i < position - 1 && current != NULL; i++) {
current = current->next;
}
if (current == NULL) {
printf("Invalid position.\n");
return;
}
newNode->next = current->next;
current->next = newNode;
}
// Function to delete the first node of the linked list
Node *deleteFirstNode(Node *head) {
if (head == NULL) {
printf("List is empty.\n");
return NULL;
}
Node *temp = head;
head = head->next;
free(temp);
return head;
}
// Function to delete the last node of the linked list
void deleteLastNode(Node *head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
if (head->next == NULL) {
free(head);
head = NULL;
return;
}
Node *current = head;
Node *previous = NULL;
while (current->next != NULL) {
previous = current;
current = current->next;
}
free(current);
previous->next = NULL;
}
// Function to delete a node at a specific position in the linked list
void deleteAtPosition(Node *head, int position) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
Node *current = head;
Node *previous = NULL;
for (int i = 1; i < position && current != NULL; i++) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("Invalid position.\n");
return;
}
previous->next = current->next;
free(current);
}
// Function to display the linked list
void display(Node *head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
Node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Function to search for a key in the linked list
void search(Node *head, int key) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
Node *current = head;
while (current != NULL) {
if (current->data == key) {
printf("Element found.\n");
return;
}
current = current->next;
}
printf("Element not found.\n");
}
// Function to free the memory allocated for the linked list
void freeList(Node *head) {
Node *current = head;
while (current != NULL) {
Node *temp = current;
current = current->next;
free(temp);
}
}
int main() {
Node *head = NULL;
int choice, data, position, key;
while (1) {
printf("\n1. Insert at beginning\n2. Insert at end\n3. Insert at position\n");
printf("4. Delete first node\n5. Delete last node\n6. Delete at position\n");
printf("7. Display\n8. Search\n9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
head = insertAtBeginning(head, data);
break;
case 2:
printf("Enter data to insert: ");
scanf("%d", &data);
insertAtEnd(head, data);
break;
case 3:
printf("Enter data to insert: ");
scanf("%d", &data);
printf("Enter position to insert: ");
scanf("%d", &position);
insertAtPosition(head, data, position);
break;
case 4:
head = deleteFirstNode(head);
break;
case 5:
deleteLastNode(head);
break;
case 6:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteAtPosition(head, position);
break;
case 7:
display(head);
break;
case 8:
printf("Enter key to search: ");
scanf("%d", &key);
search(head, key);
break;
case 9:
freeList(head);
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}