+1 (315) 557-6473 

Program To Represent a Library Management System Using Linked Lists in C Assignment Solutions.


Instructions

Objective
Write a program to represent a library management system using linked lists in C language.

Requirements and Specifications

Represent a library management system in C-language
Represent a library management system in C-language 1
Represent a library management system in C-language 2

Source Code

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "book.h"

Book *list;

void insertBook(Book **sPtr) {

 Book *curr = list;

 if (curr == NULL) {

  list = *sPtr;

  return;

 }

 if(strcmp(curr->ID, (*sPtr)->ID) == 0) {

  curr->numberCopies++;

  return;

 }

 while(curr != NULL) {

  if(strcmp(curr->ID, (*sPtr)->ID) == 0) {

   curr->numberCopies++;

   return;

  }

  if (curr->next == NULL) {

   break;

  }

  curr = curr->next;

 }

 curr->next = *sPtr;

}

void deleteBook(Book **sPtr) {

 Book *curr = list;

 if (curr == NULL) {

  return;

 }

 if(strcmp(curr->ID, (*sPtr)->ID) == 0) {

  if (curr->numberCopies == 1) {

   printf("Book %s checked out successfully! Book list is empty.\n", curr->ID);

   free(curr);

   list = list->next;

   if (list == NULL) {

    printf("Book list is empty\n");

   }

  }

  else {

   curr->numberCopies--;

   }

  return;

 }

 while(curr->next != NULL) {

  if(strcmp(curr->next->ID, (*sPtr)->ID) == 0) {

   if (curr->next->numberCopies == 1) {

    printf("Book %s checked out successfully!\n", curr->next->ID);

    free(curr->next);

    curr->next = curr->next->next;

   }

   else {

    printf("Book %s checked out successfully!\n", curr->next->ID);

    curr->next->numberCopies--;

   }

   return;;

  }

  curr = curr->next;

 }

 printf("Book %s not found.\n", (*sPtr)->ID);

}

void printBook(Book *currentPtr) {

 if(currentPtr == NULL) {

  return;

 }

 printf("%-30s%-20s%-20s%d\n", currentPtr->bookTitle, currentPtr->author, currentPtr->ID, currentPtr->numberCopies);

 printBook(currentPtr->next);

}

void displayID(Book *currentPtr) {

 if(currentPtr == NULL) {

  return;

 }

 printf("%s ", currentPtr->ID);

 displayID(currentPtr->next);

}

int main(void) {

 size_t len = 256;

  char *input = NULL;

 int isOver = 0;

 printf("Enter your choice:\n1 Add a book;\n2 Check out a book;\n3 Display books;\n4 Exit\n");

 while(isOver == 0) {

  printf("? ");

  getline(&input, &len, stdin);

  input[strlen(input)-1] = 0;

  if (strcmp("1", input) == 0) {

   Book *book = (Book*)malloc(sizeof(Book));

   printf("Book Title: ");

   getline(&input, &len, stdin);

   input[strlen(input)-1] = 0;

   strcpy(book->bookTitle, input);

   printf("Author: ");

   getline(&input, &len, stdin);

   input[strlen(input)-1] = 0;

   strcpy(book->author, input);

   printf("Book ID: ");

   getline(&input, &len, stdin);

   input[strlen(input)-1] = 0;

   strcpy(book->ID, input);

   book->numberCopies = 1;

   insertBook(&book);

  }

  else if (strcmp("2", input) == 0) {

   if (list == NULL) {

    printf("Book list is empty. No books to check out!\n");

   }

   else {

    printf("Available Book IDs to check out:\n");

    displayID(list);

    printf("\nEnter a book ID to check out: ");

    Book *book = (Book*)malloc(sizeof(Book));

    getline(&input, &len, stdin);

    input[strlen(input)-1] = 0;

    strcpy(book->ID, input);

    deleteBook(&book);

    free(book);

   }

  }

  else if (strcmp("3", input) == 0) {

   if(list == NULL) {

    printf("Book list is empty\n");

    }

   else{

    printf("%-30s%-20s%-20s%-20s\n", "Book Title", "Author", "ID", "Copies");

    printBook(list);

   }

  }

  else if (strcmp("4", input) == 0) {

   Book* curr = list;

   while(curr != NULL) {

    Book *tmp = curr;

    curr=curr->next;

    free(tmp);

   }

   isOver = 1;

  }

  else {

   printf("Invalid Input\n");

  }

  printf("\n");

 }

}