DSC Lab Project j7, d6, k2
DSC Lab Project j7, d6, k2
DATA STRUCTURES
THROUGH ‘C’
DATA STRUCTURES
THROUGH ‘C’
AIM:-TO WRITE A C PROGRAM TO
IMPLEMENT LIBRARY MANAGEMENT
SYSTEM USING STACK ADT
SOUERCE CODE:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2
return;
}
s->items[++(s->top)] = new_book;
}
struct Book pop(struct Stack *s) {
if (isEmpty(s)) {
printf(“Stack Underflow\n”);
exit(1); // Handle underflow condition
}
return s->items[(s->top)--];
}
struct Book peek(struct Stack *s) {
if (isEmpty(s)) {
printf(“Stack is empty\n”);
exit(1); // Handle empty stack
}
return s->items[s->top];
}
3
void addBook(struct Stack *s, int id,
char *title, char *author) {
struct Book new_book;
new_book.book_id = id;
strcpy(new_book.title, title);
strcpy(new_book.author, author);
push(s, new_book);
printf(“Book added successfully.\n”);
}
void displayAllBooks(struct Stack *s)
{
if (isEmpty(s)) {
printf(“Library is empty\n”);
return;
}
printf(“Books in the library:\n”);
for (int i = s->top; i >= 0; i--) {
printf(“Book ID: %d, Title: %s, Author:
%s\n”,
4
s->items[i].book_id, s->items[i].title,
s->items[i].author);
}
}
void
removeBook(struct Stack *s) {
if (isEmpty(s)) {
printf(“Library is empty, no books to
remove\n”);
return;
}
struct Book removedBook = pop(s);
printf(“Removed Book: ID %d, Title:
%s, Author: %s\n”,
removedBook.book_id,
removedBook.title,
removedBook.author);
}
int main() {
5
struct Stack library;
initialize(&library);
// Adding books
addBook(&library, 101, “Book 1”,
“Author 1”);
addBook(&library, 102, “Book 2”,
“Author 2”);
addBook(&library, 103, “Book 3”,
“Author 3”);
// Display all books
displayAllBooks(&library);
// Removing a book
removeBook(&library);
// Display all books again
displayAllBooks(&library);
return 0;
}
6
OUTPUT:-
23B81A62J
7
23B81A62D
6
23B81A62K
2