Last active 1 week ago

Revision 8a8a8dcef24bc5f88eedbd068d639019e40dad1d

5.c Raw
1/*
2 * Ex. No. 5: IMPLEMENTATION OF LINKED LIST
3 * Student Record Management System using a singly linked list
4 * Supports: INSERT (beginning/end/position), DELETE (beginning/end/position),
5 * SEARCH, DISPLAY
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#define NAME_LEN 50
12
13struct Node {
14 int regNo;
15 char name[NAME_LEN];
16 struct Node *next;
17};
18
19struct Node *head = NULL;
20
21/* Count nodes in list */
22int countNodes(void) {
23 int count = 0;
24 struct Node *cur = head;
25 while (cur != NULL) {
26 count++;
27 cur = cur->next;
28 }
29 return count;
30}
31
32/* Allocate and fill a new node */
33struct Node *makeNode(int regNo, const char *name) {
34 struct Node *node = (struct Node *)malloc(sizeof(struct Node));
35 node->regNo = regNo;
36 strncpy(node->name, name, NAME_LEN - 1);
37 node->name[NAME_LEN - 1] = '\0';
38 node->next = NULL;
39 return node;
40}
41
42/* i) Insert at Beginning */
43void insertBeginning(int regNo, const char *name) {
44 struct Node *node = makeNode(regNo, name);
45 node->next = head;
46 head = node;
47 printf("Inserted at beginning.\n");
48}
49
50/* ii) Insert at End */
51void insertEnd(int regNo, const char *name) {
52 struct Node *node = makeNode(regNo, name);
53 if (head == NULL) {
54 head = node;
55 } else {
56 struct Node *cur = head;
57 while (cur->next != NULL)
58 cur = cur->next;
59 cur->next = node;
60 }
61 printf("Inserted at end.\n");
62}
63
64/* iii) Insert at a Given Position (1-based) */
65void insertAtPosition(int pos, int regNo, const char *name) {
66 int size = countNodes();
67 if (pos < 1 || pos > size + 1) {
68 printf("Invalid position.\n");
69 return;
70 }
71 if (pos == 1) {
72 insertBeginning(regNo, name);
73 return;
74 }
75 struct Node *prev = head;
76 for (int i = 1; i < pos - 1; i++)
77 prev = prev->next;
78 struct Node *node = makeNode(regNo, name);
79 node->next = prev->next;
80 prev->next = node;
81 printf("Inserted at position %d.\n", pos);
82}
83
84/* iv) Delete from Beginning */
85void deleteBeginning(void) {
86 if (head == NULL) {
87 printf("List is empty.\n");
88 return;
89 }
90 struct Node *doomed = head;
91 head = head->next;
92 free(doomed);
93 printf("Deleted from beginning.\n");
94}
95
96/* v) Delete from End */
97void deleteEnd(void) {
98 if (head == NULL) {
99 printf("List is empty.\n");
100 return;
101 }
102 if (head->next == NULL) {
103 free(head);
104 head = NULL;
105 printf("Deleted from end.\n");
106 return;
107 }
108 struct Node *cur = head;
109 while (cur->next->next != NULL)
110 cur = cur->next;
111 free(cur->next);
112 cur->next = NULL;
113 printf("Deleted from end.\n");
114}
115
116/* vi) Delete from a Given Position (1-based) */
117void deleteAtPosition(int pos) {
118 int size = countNodes();
119 if (size == 0) {
120 printf("List is empty.\n");
121 return;
122 }
123 if (pos < 1 || pos > size) {
124 printf("Invalid position.\n");
125 return;
126 }
127 if (pos == 1) {
128 deleteBeginning();
129 return;
130 }
131 struct Node *prev = head;
132 for (int i = 1; i < pos - 1; i++)
133 prev = prev->next;
134 struct Node *doomed = prev->next;
135 prev->next = doomed->next;
136 free(doomed);
137 printf("Deleted position %d.\n", pos);
138}
139
140/* vii) Search by register number */
141void search(int regNo) {
142 struct Node *cur = head;
143 int pos = 1;
144 while (cur != NULL) {
145 if (cur->regNo == regNo) {
146 printf("Found: RegNo=%d, Name=%s, Position=%d\n", cur->regNo, cur->name, pos);
147 return;
148 }
149 cur = cur->next;
150 pos++;
151 }
152 printf("Register number %d not found.\n", regNo);
153}
154
155/* viii) Display all students */
156void display(void) {
157 if (head == NULL) {
158 printf("List is empty.\n");
159 return;
160 }
161 struct Node *cur = head;
162 int pos = 1;
163 printf("--- Student Records ---\n");
164 while (cur != NULL) {
165 printf("%d) RegNo=%d, Name=%s\n", pos, cur->regNo, cur->name);
166 cur = cur->next;
167 pos++;
168 }
169}
170
171/* Free all remaining nodes before exit */
172void freeList(void) {
173 struct Node *cur = head;
174 while (cur != NULL) {
175 struct Node *next = cur->next;
176 free(cur);
177 cur = next;
178 }
179 head = NULL;
180}
181
182void readName(char *name) {
183 scanf(" %49[^\n]", name);
184}
185
186int main(void) {
187 int choice, regNo, pos;
188 char name[NAME_LEN];
189
190 do {
191 printf("\n===== Student Record Management System =====\n");
192 printf("1. Insert at Beginning\n");
193 printf("2. Insert at End\n");
194 printf("3. Insert at a Given Position\n");
195 printf("4. Delete from Beginning\n");
196 printf("5. Delete from End\n");
197 printf("6. Delete from a Given Position\n");
198 printf("7. Search\n");
199 printf("8. Display\n");
200 printf("9. Exit\n");
201 printf("Enter your choice: ");
202 scanf("%d", &choice);
203
204 switch (choice) {
205 case 1:
206 printf("Enter register number: ");
207 scanf("%d", &regNo);
208 printf("Enter name: ");
209 readName(name);
210 insertBeginning(regNo, name);
211 break;
212 case 2:
213 printf("Enter register number: ");
214 scanf("%d", &regNo);
215 printf("Enter name: ");
216 readName(name);
217 insertEnd(regNo, name);
218 break;
219 case 3:
220 printf("Enter position: ");
221 scanf("%d", &pos);
222 printf("Enter register number: ");
223 scanf("%d", &regNo);
224 printf("Enter name: ");
225 readName(name);
226 insertAtPosition(pos, regNo, name);
227 break;
228 case 4:
229 deleteBeginning();
230 break;
231 case 5:
232 deleteEnd();
233 break;
234 case 6:
235 printf("Enter position: ");
236 scanf("%d", &pos);
237 deleteAtPosition(pos);
238 break;
239 case 7:
240 printf("Enter register number to search: ");
241 scanf("%d", &regNo);
242 search(regNo);
243 break;
244 case 8:
245 display();
246 break;
247 case 9:
248 printf("Exiting...\n");
249 break;
250 default:
251 printf("Invalid choice.\n");
252 }
253 } while (choice != 9);
254
255 freeList();
256 return 0;
257}
258