#include <stdio.h>
#include <string.h> #define MAX 5 // Maximum number of books in the
library (stack size)
// Structure to represent a book
struct Book {
int book_id;
char title[50];
char author[50];
};
// Stack to hold books
struct Book stack[MAX];
int top = -1; // Stack initialization
// Function to add a book to the stack
void add_book(struct Book book) {
if (top >= MAX - 1) {
printf("Stack Overflow! Cannot add more books.\n");
} else {
top++;
stack[top] = book;
printf("Book '%s' added to the library.\n", book.title);
}
}
// Function to issue a book (pop from stack)
void issue_book() {
if (top == -1) {
printf("No books available to issue.\n");
} else {
printf("Book '%s' has been issued.\n", stack[top].title);
top--; // Remove the book from the stack
}
}
// Function to return a book (push to stack)
void return_book(struct Book book) {
if (top >= MAX - 1) {
printf("Stack Overflow! Cannot return more books.\n");
} else {
top++;
stack[top] = book;
printf("Book '%s' returned successfully.\n", book.title);
}
}
// Function to display all books in the library (stack)
void view_books() {
if (top == -1) {
printf("No books available in the library.\n");
} else {
printf("Books available in the library:\n");
for (int i = top; i >= 0; i--) {
printf("ID: %d, Title: %s, Author: %s\n", stack[i].book_id, stack[i].title,
stack[i].author);
}
}
}
// Main function to drive the program
int main() {
struct Book book1 = {1, "C Programming", "Dennis Ritchie"};
struct Book book2 = {2, "Data Structures", "Robert Lafore"};
struct Book book3 = {3, "Algorithms", "Thomas H. Cormen"};
int choice;
do {
printf("\nLibrary Management System\n");
printf("1. Add Book\n");
printf("2. Issue Book\n");
printf("3. Return Book\n");
printf("4. View Books\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
add_book(book1);
break;
case 2:
issue_book();
break;
case 3:
return_book(book2);
break;
case 4:
view_books();
break;
case 5:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice!
Please try again.\n");
!= 5);
return 0;
}