6.c
· 8.7 KiB · C
Raw
/*
* Ex. No. 6: IMPLEMENTATION OF DOUBLY LINKED LIST
* Library Borrowed Book Record System using a doubly linked list
* Supports: INSERT (beginning/end/position), DELETE (beginning/end/position/by ID),
* SEARCH by Book ID, DISPLAY (forward/reverse)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TITLE_LEN 60
#define AUTHOR_LEN 50
struct Node {
int bookId;
char title[TITLE_LEN];
char author[AUTHOR_LEN];
struct Node *prev;
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;
}
/* Return the last node, or NULL if the list is empty */
struct Node *lastNode(void) {
struct Node *cur = head;
if (cur == NULL) {
return NULL;
}
while (cur->next != NULL) {
cur = cur->next;
}
return cur;
}
struct Node *makeNode(int bookId, const char *title, const char *author) {
struct Node *node = (struct Node *)malloc(sizeof(struct Node));
if (node == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
node->bookId = bookId;
strcpy(node->title, title);
strcpy(node->author, author);
node->prev = NULL;
node->next = NULL;
return node;
}
/* i) Insert at beginning */
void insertBeginning(int bookId, const char *title, const char *author) {
struct Node *node = makeNode(bookId, title, author);
node->next = head;
if (head != NULL) {
head->prev = node;
}
head = node;
printf("Record inserted at beginning.\n");
}
/* ii) Insert at end */
void insertEnd(int bookId, const char *title, const char *author) {
struct Node *node = makeNode(bookId, title, author);
struct Node *last = lastNode();
if (last == NULL) {
head = node;
} else {
last->next = node;
node->prev = last;
}
printf("Record inserted at end.\n");
}
/* iii) Insert at a given position (1-based) */
void insertAtPosition(int pos, int bookId, const char *title, const char *author) {
int size = countNodes();
if (pos < 1 || pos > size + 1) {
printf("Invalid position. Valid range is 1 to %d.\n", size + 1);
return;
}
if (pos == 1) {
insertBeginning(bookId, title, author);
return;
}
struct Node *prev = head;
int i;
for (i = 1; i < pos - 1; i++) {
prev = prev->next;
}
struct Node *node = makeNode(bookId, title, author);
node->next = prev->next;
node->prev = prev;
if (prev->next != NULL) {
prev->next->prev = node;
}
prev->next = node;
printf("Record inserted at position %d.\n", pos);
}
/* iv) Delete from beginning */
void deleteBeginning(void) {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
struct Node *doomed = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
printf("Deleted record: BookID=%d, Title=%s\n", doomed->bookId, doomed->title);
free(doomed);
}
/* v) Delete from end */
void deleteEnd(void) {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
struct Node *doomed = lastNode();
if (doomed->prev == NULL) {
head = NULL;
} else {
doomed->prev->next = NULL;
}
printf("Deleted record: BookID=%d, Title=%s\n", doomed->bookId, doomed->title);
free(doomed);
}
/* vi) Delete from a given position (1-based) */
void deleteAtPosition(int pos) {
int size = countNodes();
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
if (pos < 1 || pos > size) {
printf("Invalid position. Valid range is 1 to %d.\n", size);
return;
}
if (pos == 1) {
deleteBeginning();
return;
}
struct Node *doomed = head;
int i;
for (i = 1; i < pos; i++) {
doomed = doomed->next;
}
doomed->prev->next = doomed->next;
if (doomed->next != NULL) {
doomed->next->prev = doomed->prev;
}
printf("Deleted record: BookID=%d, Title=%s\n", doomed->bookId, doomed->title);
free(doomed);
}
/* vii) Delete a record by its Book ID */
void deleteById(int bookId) {
struct Node *cur = head;
while (cur != NULL && cur->bookId != bookId) {
cur = cur->next;
}
if (cur == NULL) {
printf("Book ID %d not found.\n", bookId);
return;
}
if (cur->prev == NULL) {
head = cur->next;
} else {
cur->prev->next = cur->next;
}
if (cur->next != NULL) {
cur->next->prev = cur->prev;
}
printf("Deleted record: BookID=%d, Title=%s\n", cur->bookId, cur->title);
free(cur);
}
/* viii) Search by Book ID */
void search(int bookId) {
struct Node *cur = head;
int pos = 1;
while (cur != NULL) {
if (cur->bookId == bookId) {
printf("Found at position %d: BookID=%d, Title=%s, Author=%s\n",
pos, cur->bookId, cur->title, cur->author);
return;
}
cur = cur->next;
pos++;
}
printf("Book ID %d not found.\n", bookId);
}
/* ix) Display all records in forward order */
void displayForward(void) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node *cur = head;
int pos = 1;
printf("--- Borrowed Book Records (Forward) ---\n");
while (cur != NULL) {
printf("%d) BookID=%d, Title=%s, Author=%s\n", pos, cur->bookId, cur->title, cur->author);
cur = cur->next;
pos++;
}
}
/* x) Display all records in reverse order */
void displayReverse(void) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node *cur = lastNode();
int pos = countNodes();
printf("--- Borrowed Book Records (Reverse) ---\n");
while (cur != NULL) {
printf("%d) BookID=%d, Title=%s, Author=%s\n", pos, cur->bookId, cur->title, cur->author);
cur = cur->prev;
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 readBook(int *bookId, char *title, char *author) {
printf("Enter book ID: ");
scanf("%d", bookId);
printf("Enter book title: ");
scanf(" %59[^\n]", title);
printf("Enter author name: ");
scanf(" %49[^\n]", author);
}
int main(void) {
int choice, bookId, pos;
char title[TITLE_LEN];
char author[AUTHOR_LEN];
do {
printf("\n===== Library Borrowed Book Record 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. Delete by Book ID\n");
printf("8. Search by Book ID\n");
printf("9. Display Forward\n");
printf("10. Display Reverse\n");
printf("11. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
readBook(&bookId, title, author);
insertBeginning(bookId, title, author);
break;
case 2:
readBook(&bookId, title, author);
insertEnd(bookId, title, author);
break;
case 3:
printf("Enter position: ");
scanf("%d", &pos);
readBook(&bookId, title, author);
insertAtPosition(pos, bookId, title, author);
break;
case 4:
deleteBeginning();
break;
case 5:
deleteEnd();
break;
case 6:
printf("Enter position: ");
scanf("%d", &pos);
deleteAtPosition(pos);
break;
case 7:
printf("Enter book ID to delete: ");
scanf("%d", &bookId);
deleteById(bookId);
break;
case 8:
printf("Enter book ID to search: ");
scanf("%d", &bookId);
search(bookId);
break;
case 9:
displayForward();
break;
case 10:
displayReverse();
break;
case 11:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 11);
freeList();
return 0;
}
| 1 | /* |
| 2 | * Ex. No. 6: IMPLEMENTATION OF DOUBLY LINKED LIST |
| 3 | * Library Borrowed Book Record System using a doubly linked list |
| 4 | * Supports: INSERT (beginning/end/position), DELETE (beginning/end/position/by ID), |
| 5 | * SEARCH by Book ID, DISPLAY (forward/reverse) |
| 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #define TITLE_LEN 60 |
| 12 | #define AUTHOR_LEN 50 |
| 13 | |
| 14 | struct Node { |
| 15 | int bookId; |
| 16 | char title[TITLE_LEN]; |
| 17 | char author[AUTHOR_LEN]; |
| 18 | struct Node *prev; |
| 19 | struct Node *next; |
| 20 | }; |
| 21 | |
| 22 | struct Node *head = NULL; |
| 23 | |
| 24 | /* Count nodes in list */ |
| 25 | int countNodes(void) { |
| 26 | int count = 0; |
| 27 | struct Node *cur = head; |
| 28 | while (cur != NULL) { |
| 29 | count++; |
| 30 | cur = cur->next; |
| 31 | } |
| 32 | return count; |
| 33 | } |
| 34 | |
| 35 | /* Return the last node, or NULL if the list is empty */ |
| 36 | struct Node *lastNode(void) { |
| 37 | struct Node *cur = head; |
| 38 | if (cur == NULL) { |
| 39 | return NULL; |
| 40 | } |
| 41 | while (cur->next != NULL) { |
| 42 | cur = cur->next; |
| 43 | } |
| 44 | return cur; |
| 45 | } |
| 46 | |
| 47 | struct Node *makeNode(int bookId, const char *title, const char *author) { |
| 48 | struct Node *node = (struct Node *)malloc(sizeof(struct Node)); |
| 49 | if (node == NULL) { |
| 50 | printf("Memory allocation failed.\n"); |
| 51 | exit(1); |
| 52 | } |
| 53 | node->bookId = bookId; |
| 54 | strcpy(node->title, title); |
| 55 | strcpy(node->author, author); |
| 56 | node->prev = NULL; |
| 57 | node->next = NULL; |
| 58 | return node; |
| 59 | } |
| 60 | |
| 61 | /* i) Insert at beginning */ |
| 62 | void insertBeginning(int bookId, const char *title, const char *author) { |
| 63 | struct Node *node = makeNode(bookId, title, author); |
| 64 | node->next = head; |
| 65 | if (head != NULL) { |
| 66 | head->prev = node; |
| 67 | } |
| 68 | head = node; |
| 69 | printf("Record inserted at beginning.\n"); |
| 70 | } |
| 71 | |
| 72 | /* ii) Insert at end */ |
| 73 | void insertEnd(int bookId, const char *title, const char *author) { |
| 74 | struct Node *node = makeNode(bookId, title, author); |
| 75 | struct Node *last = lastNode(); |
| 76 | if (last == NULL) { |
| 77 | head = node; |
| 78 | } else { |
| 79 | last->next = node; |
| 80 | node->prev = last; |
| 81 | } |
| 82 | printf("Record inserted at end.\n"); |
| 83 | } |
| 84 | |
| 85 | /* iii) Insert at a given position (1-based) */ |
| 86 | void insertAtPosition(int pos, int bookId, const char *title, const char *author) { |
| 87 | int size = countNodes(); |
| 88 | if (pos < 1 || pos > size + 1) { |
| 89 | printf("Invalid position. Valid range is 1 to %d.\n", size + 1); |
| 90 | return; |
| 91 | } |
| 92 | if (pos == 1) { |
| 93 | insertBeginning(bookId, title, author); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | struct Node *prev = head; |
| 98 | int i; |
| 99 | for (i = 1; i < pos - 1; i++) { |
| 100 | prev = prev->next; |
| 101 | } |
| 102 | |
| 103 | struct Node *node = makeNode(bookId, title, author); |
| 104 | node->next = prev->next; |
| 105 | node->prev = prev; |
| 106 | if (prev->next != NULL) { |
| 107 | prev->next->prev = node; |
| 108 | } |
| 109 | prev->next = node; |
| 110 | printf("Record inserted at position %d.\n", pos); |
| 111 | } |
| 112 | |
| 113 | /* iv) Delete from beginning */ |
| 114 | void deleteBeginning(void) { |
| 115 | if (head == NULL) { |
| 116 | printf("List is empty. Nothing to delete.\n"); |
| 117 | return; |
| 118 | } |
| 119 | struct Node *doomed = head; |
| 120 | head = head->next; |
| 121 | if (head != NULL) { |
| 122 | head->prev = NULL; |
| 123 | } |
| 124 | printf("Deleted record: BookID=%d, Title=%s\n", doomed->bookId, doomed->title); |
| 125 | free(doomed); |
| 126 | } |
| 127 | |
| 128 | /* v) Delete from end */ |
| 129 | void deleteEnd(void) { |
| 130 | if (head == NULL) { |
| 131 | printf("List is empty. Nothing to delete.\n"); |
| 132 | return; |
| 133 | } |
| 134 | struct Node *doomed = lastNode(); |
| 135 | if (doomed->prev == NULL) { |
| 136 | head = NULL; |
| 137 | } else { |
| 138 | doomed->prev->next = NULL; |
| 139 | } |
| 140 | printf("Deleted record: BookID=%d, Title=%s\n", doomed->bookId, doomed->title); |
| 141 | free(doomed); |
| 142 | } |
| 143 | |
| 144 | /* vi) Delete from a given position (1-based) */ |
| 145 | void deleteAtPosition(int pos) { |
| 146 | int size = countNodes(); |
| 147 | if (head == NULL) { |
| 148 | printf("List is empty. Nothing to delete.\n"); |
| 149 | return; |
| 150 | } |
| 151 | if (pos < 1 || pos > size) { |
| 152 | printf("Invalid position. Valid range is 1 to %d.\n", size); |
| 153 | return; |
| 154 | } |
| 155 | if (pos == 1) { |
| 156 | deleteBeginning(); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | struct Node *doomed = head; |
| 161 | int i; |
| 162 | for (i = 1; i < pos; i++) { |
| 163 | doomed = doomed->next; |
| 164 | } |
| 165 | |
| 166 | doomed->prev->next = doomed->next; |
| 167 | if (doomed->next != NULL) { |
| 168 | doomed->next->prev = doomed->prev; |
| 169 | } |
| 170 | printf("Deleted record: BookID=%d, Title=%s\n", doomed->bookId, doomed->title); |
| 171 | free(doomed); |
| 172 | } |
| 173 | |
| 174 | /* vii) Delete a record by its Book ID */ |
| 175 | void deleteById(int bookId) { |
| 176 | struct Node *cur = head; |
| 177 | while (cur != NULL && cur->bookId != bookId) { |
| 178 | cur = cur->next; |
| 179 | } |
| 180 | if (cur == NULL) { |
| 181 | printf("Book ID %d not found.\n", bookId); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | if (cur->prev == NULL) { |
| 186 | head = cur->next; |
| 187 | } else { |
| 188 | cur->prev->next = cur->next; |
| 189 | } |
| 190 | if (cur->next != NULL) { |
| 191 | cur->next->prev = cur->prev; |
| 192 | } |
| 193 | printf("Deleted record: BookID=%d, Title=%s\n", cur->bookId, cur->title); |
| 194 | free(cur); |
| 195 | } |
| 196 | |
| 197 | /* viii) Search by Book ID */ |
| 198 | void search(int bookId) { |
| 199 | struct Node *cur = head; |
| 200 | int pos = 1; |
| 201 | while (cur != NULL) { |
| 202 | if (cur->bookId == bookId) { |
| 203 | printf("Found at position %d: BookID=%d, Title=%s, Author=%s\n", |
| 204 | pos, cur->bookId, cur->title, cur->author); |
| 205 | return; |
| 206 | } |
| 207 | cur = cur->next; |
| 208 | pos++; |
| 209 | } |
| 210 | printf("Book ID %d not found.\n", bookId); |
| 211 | } |
| 212 | |
| 213 | /* ix) Display all records in forward order */ |
| 214 | void displayForward(void) { |
| 215 | if (head == NULL) { |
| 216 | printf("List is empty.\n"); |
| 217 | return; |
| 218 | } |
| 219 | struct Node *cur = head; |
| 220 | int pos = 1; |
| 221 | printf("--- Borrowed Book Records (Forward) ---\n"); |
| 222 | while (cur != NULL) { |
| 223 | printf("%d) BookID=%d, Title=%s, Author=%s\n", pos, cur->bookId, cur->title, cur->author); |
| 224 | cur = cur->next; |
| 225 | pos++; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* x) Display all records in reverse order */ |
| 230 | void displayReverse(void) { |
| 231 | if (head == NULL) { |
| 232 | printf("List is empty.\n"); |
| 233 | return; |
| 234 | } |
| 235 | struct Node *cur = lastNode(); |
| 236 | int pos = countNodes(); |
| 237 | printf("--- Borrowed Book Records (Reverse) ---\n"); |
| 238 | while (cur != NULL) { |
| 239 | printf("%d) BookID=%d, Title=%s, Author=%s\n", pos, cur->bookId, cur->title, cur->author); |
| 240 | cur = cur->prev; |
| 241 | pos--; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* Free all remaining nodes before exit */ |
| 246 | void freeList(void) { |
| 247 | struct Node *cur = head; |
| 248 | while (cur != NULL) { |
| 249 | struct Node *next = cur->next; |
| 250 | free(cur); |
| 251 | cur = next; |
| 252 | } |
| 253 | head = NULL; |
| 254 | } |
| 255 | |
| 256 | void readBook(int *bookId, char *title, char *author) { |
| 257 | printf("Enter book ID: "); |
| 258 | scanf("%d", bookId); |
| 259 | printf("Enter book title: "); |
| 260 | scanf(" %59[^\n]", title); |
| 261 | printf("Enter author name: "); |
| 262 | scanf(" %49[^\n]", author); |
| 263 | } |
| 264 | |
| 265 | int main(void) { |
| 266 | int choice, bookId, pos; |
| 267 | char title[TITLE_LEN]; |
| 268 | char author[AUTHOR_LEN]; |
| 269 | |
| 270 | do { |
| 271 | printf("\n===== Library Borrowed Book Record System =====\n"); |
| 272 | printf("1. Insert at Beginning\n"); |
| 273 | printf("2. Insert at End\n"); |
| 274 | printf("3. Insert at a Given Position\n"); |
| 275 | printf("4. Delete from Beginning\n"); |
| 276 | printf("5. Delete from End\n"); |
| 277 | printf("6. Delete from a Given Position\n"); |
| 278 | printf("7. Delete by Book ID\n"); |
| 279 | printf("8. Search by Book ID\n"); |
| 280 | printf("9. Display Forward\n"); |
| 281 | printf("10. Display Reverse\n"); |
| 282 | printf("11. Exit\n"); |
| 283 | printf("Enter your choice: "); |
| 284 | scanf("%d", &choice); |
| 285 | |
| 286 | switch (choice) { |
| 287 | case 1: |
| 288 | readBook(&bookId, title, author); |
| 289 | insertBeginning(bookId, title, author); |
| 290 | break; |
| 291 | case 2: |
| 292 | readBook(&bookId, title, author); |
| 293 | insertEnd(bookId, title, author); |
| 294 | break; |
| 295 | case 3: |
| 296 | printf("Enter position: "); |
| 297 | scanf("%d", &pos); |
| 298 | readBook(&bookId, title, author); |
| 299 | insertAtPosition(pos, bookId, title, author); |
| 300 | break; |
| 301 | case 4: |
| 302 | deleteBeginning(); |
| 303 | break; |
| 304 | case 5: |
| 305 | deleteEnd(); |
| 306 | break; |
| 307 | case 6: |
| 308 | printf("Enter position: "); |
| 309 | scanf("%d", &pos); |
| 310 | deleteAtPosition(pos); |
| 311 | break; |
| 312 | case 7: |
| 313 | printf("Enter book ID to delete: "); |
| 314 | scanf("%d", &bookId); |
| 315 | deleteById(bookId); |
| 316 | break; |
| 317 | case 8: |
| 318 | printf("Enter book ID to search: "); |
| 319 | scanf("%d", &bookId); |
| 320 | search(bookId); |
| 321 | break; |
| 322 | case 9: |
| 323 | displayForward(); |
| 324 | break; |
| 325 | case 10: |
| 326 | displayReverse(); |
| 327 | break; |
| 328 | case 11: |
| 329 | printf("Exiting...\n"); |
| 330 | break; |
| 331 | default: |
| 332 | printf("Invalid choice.\n"); |
| 333 | } |
| 334 | } while (choice != 11); |
| 335 | |
| 336 | freeList(); |
| 337 | return 0; |
| 338 | } |
| 339 |