0% found this document useful (0 votes)
19 views7 pages

DSC Lab Project j7, d6, k2

Uploaded by

chary01229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

DSC Lab Project j7, d6, k2

Uploaded by

chary01229
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

*LAB PROJECT*

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>

#define MAX_SIZE 100


struct Book {
int book_id;
char title[100];
char author[100];
};
struct Stack {
int top;
struct Book items[MAX_SIZE];
};
void initialize(struct Stack *s) {
s->top = -1;
}
int isFull(struct Stack *s) {
return s->top == MAX_SIZE – 1;
}
int isEmpty(struct Stack *s) {
return s->top == -1;
}
void push(struct Stack *s, struct Book
new_book) {
if (isFull(s)) {
printf(“Stack Overflow\n”);

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

You might also like