DSA Project – Library Book Management System
Learn DSA & Get Ready for MAANG Companies Start Now!!
Program 1
// Project Library Books Management System using doubly linked list
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book {
int id;
char title[100];
char author[100];
struct Book *prev;
struct Book *next;
};
struct Book *head = NULL;
// Function to create a new book node
struct Book* createBook(int id, const char* title, const char* author)
{
struct Book* newBook = (struct Book*)malloc(sizeof(struct Book));
newBook->id = id;
strcpy(newBook->title, title);
strcpy(newBook->author, author);
newBook->prev = NULL;
newBook->next = NULL;
return newBook;
}
// Add book to the end of the list
void addBook(int id, const char* title, const char* author) {
struct Book* newBook = createBook(id, title, author);
if (head == NULL) {
head = newBook;
return;
}
struct Book* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newBook;
newBook->prev = temp;
printf("Book added successfully.\n");
}
// Display all books
void displayBooks() {
if (head == NULL) {
printf("No books in the library.\n");
return;
}
struct Book* temp = head;
printf("\nBooks in Library:\n");
printf("ID\tTitle\t\tAuthor\n");
printf("\n---------------------------------------------\n");
while (temp != NULL) {
printf("%d\t%s\t\t%s\n", temp->id, temp->title, temp->author);
temp = temp->next;
}
printf("\n--------------------------------------------");
}
// Search for a book by ID
void searchBook(int id) {
struct Book* temp = head;
while (temp != NULL) {
if (temp->id == id) {
printf("Book found:\n");
printf("ID: %d\nTitle: %s\nAuthor: %s\n", temp->id, temp->title, temp->author);
return;
}
temp = temp->next;
}
printf("Book with ID %d not found.\n", id);
}
// Delete a book by ID
void deleteBook(int id) {
struct Book* temp = head;
while (temp != NULL && temp->id != id)
temp = temp->next;
if (temp == NULL)
{
printf("Book not found.\n");
return;
}
int choice;
printf("ID: %d\nTitle: %s\nAuthor: %s\n", temp->id, temp->title, temp->author);
printf("\n Are you sure want to delete(1-Yes): ");
scanf("%d",&choice);
if(choice==1)
{
if (temp->prev != NULL)
temp->prev->next = temp->next;
else
head = temp->next; // deleting head
if (temp->next != NULL)
temp->next->prev = temp->prev;
free(temp);
printf("Book deleted successfully.\n");
}
}
// Main menu
int main() {
int choice, id;
char title[100], author[100];
while (1) {
printf("\n===== Library Menu =====\n");
printf("1. Add Book\n");
printf("2. Delete Book\n");
printf("3. Display All Books\n");
printf("4. Search Book by ID\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // to consume newline
switch (choice) {
case 1:
printf("Enter Book ID: ");
scanf("%d", &id);
getchar();
printf("Enter Book Title: ");
fgets(title, sizeof(title), stdin);
title[strcspn(title, "\n")] = '\0'; // remove newline // gets(title)
printf("Enter Book Author: ");
fgets(author, sizeof(author), stdin);
author[strcspn(author, "\n")] = '\0';
addBook(id, title, author);
break;
case 2:
printf("Enter Book ID to delete: ");
scanf("%d", &id);
deleteBook(id);
break;
case 3:
displayBooks();
break;
case 4:
printf("Enter Book ID to search: ");
scanf("%d", &id);
searchBook(id);
break;
case 5:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}
return 0;
} Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

