5.c
· 6.4 KiB · C
Raw
/*
* Ex. No. 5: IMPLEMENTATION OF LINKED LIST
* Student Record Management System using a singly linked list
* Supports: INSERT (beginning/end/position), DELETE (beginning/end/position),
* SEARCH, DISPLAY
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LEN 50
struct Node {
int regNo;
char name[NAME_LEN];
struct Node *next;
};
struct Node *head = NULL;
/* Count nodes in list */
int countNodes(void) {
int count = 0;
struct Node *cur = head;
while (cur != NULL) {
count++;
cur = cur->next;
}
return count;
}
/* Allocate and fill a new node */
struct Node *makeNode(int regNo, const char *name) {
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
node->regNo = regNo;
strncpy(node->name, name, NAME_LEN - 1);
node->name[NAME_LEN - 1] = '\0';
node->next = NULL;
return node;
}
/* i) Insert at Beginning */
void insertBeginning(int regNo, const char *name) {
struct Node *node = makeNode(regNo, name);
node->next = head;
head = node;
printf("Inserted at beginning.\n");
}
/* ii) Insert at End */
void insertEnd(int regNo, const char *name) {
struct Node *node = makeNode(regNo, name);
if (head == NULL) {
head = node;
} else {
struct Node *cur = head;
while (cur->next != NULL)
cur = cur->next;
cur->next = node;
}
printf("Inserted at end.\n");
}
/* iii) Insert at a Given Position (1-based) */
void insertAtPosition(int pos, int regNo, const char *name) {
int size = countNodes();
if (pos < 1 || pos > size + 1) {
printf("Invalid position.\n");
return;
}
if (pos == 1) {
insertBeginning(regNo, name);
return;
}
struct Node *prev = head;
for (int i = 1; i < pos - 1; i++)
prev = prev->next;
struct Node *node = makeNode(regNo, name);
node->next = prev->next;
prev->next = node;
printf("Inserted at position %d.\n", pos);
}
/* iv) Delete from Beginning */
void deleteBeginning(void) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node *doomed = head;
head = head->next;
free(doomed);
printf("Deleted from beginning.\n");
}
/* v) Delete from End */
void deleteEnd(void) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
if (head->next == NULL) {
free(head);
head = NULL;
printf("Deleted from end.\n");
return;
}
struct Node *cur = head;
while (cur->next->next != NULL)
cur = cur->next;
free(cur->next);
cur->next = NULL;
printf("Deleted from end.\n");
}
/* vi) Delete from a Given Position (1-based) */
void deleteAtPosition(int pos) {
int size = countNodes();
if (size == 0) {
printf("List is empty.\n");
return;
}
if (pos < 1 || pos > size) {
printf("Invalid position.\n");
return;
}
if (pos == 1) {
deleteBeginning();
return;
}
struct Node *prev = head;
for (int i = 1; i < pos - 1; i++)
prev = prev->next;
struct Node *doomed = prev->next;
prev->next = doomed->next;
free(doomed);
printf("Deleted position %d.\n", pos);
}
/* vii) Search by register number */
void search(int regNo) {
struct Node *cur = head;
int pos = 1;
while (cur != NULL) {
if (cur->regNo == regNo) {
printf("Found: RegNo=%d, Name=%s, Position=%d\n", cur->regNo, cur->name, pos);
return;
}
cur = cur->next;
pos++;
}
printf("Register number %d not found.\n", regNo);
}
/* viii) Display all students */
void display(void) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node *cur = head;
int pos = 1;
printf("--- Student Records ---\n");
while (cur != NULL) {
printf("%d) RegNo=%d, Name=%s\n", pos, cur->regNo, cur->name);
cur = cur->next;
pos++;
}
}
/* Free all remaining nodes before exit */
void freeList(void) {
struct Node *cur = head;
while (cur != NULL) {
struct Node *next = cur->next;
free(cur);
cur = next;
}
head = NULL;
}
void readName(char *name) {
scanf(" %49[^\n]", name);
}
int main(void) {
int choice, regNo, pos;
char name[NAME_LEN];
do {
printf("\n===== Student Record Management System =====\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at a Given Position\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from End\n");
printf("6. Delete from a Given Position\n");
printf("7. Search\n");
printf("8. Display\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter register number: ");
scanf("%d", ®No);
printf("Enter name: ");
readName(name);
insertBeginning(regNo, name);
break;
case 2:
printf("Enter register number: ");
scanf("%d", ®No);
printf("Enter name: ");
readName(name);
insertEnd(regNo, name);
break;
case 3:
printf("Enter position: ");
scanf("%d", &pos);
printf("Enter register number: ");
scanf("%d", ®No);
printf("Enter name: ");
readName(name);
insertAtPosition(pos, regNo, name);
break;
case 4:
deleteBeginning();
break;
case 5:
deleteEnd();
break;
case 6:
printf("Enter position: ");
scanf("%d", &pos);
deleteAtPosition(pos);
break;
case 7:
printf("Enter register number to search: ");
scanf("%d", ®No);
search(regNo);
break;
case 8:
display();
break;
case 9:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 9);
freeList();
return 0;
}
| 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 | |
| 13 | struct Node { |
| 14 | int regNo; |
| 15 | char name[NAME_LEN]; |
| 16 | struct Node *next; |
| 17 | }; |
| 18 | |
| 19 | struct Node *head = NULL; |
| 20 | |
| 21 | /* Count nodes in list */ |
| 22 | int 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 */ |
| 33 | struct 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 */ |
| 43 | void 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 */ |
| 51 | void 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) */ |
| 65 | void 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 */ |
| 85 | void 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 */ |
| 97 | void 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) */ |
| 117 | void 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 */ |
| 141 | void 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 */ |
| 156 | void 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 */ |
| 172 | void 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 | |
| 182 | void readName(char *name) { |
| 183 | scanf(" %49[^\n]", name); |
| 184 | } |
| 185 | |
| 186 | int 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", ®No); |
| 208 | printf("Enter name: "); |
| 209 | readName(name); |
| 210 | insertBeginning(regNo, name); |
| 211 | break; |
| 212 | case 2: |
| 213 | printf("Enter register number: "); |
| 214 | scanf("%d", ®No); |
| 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", ®No); |
| 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", ®No); |
| 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 |