Implement the stack operations for the stack of books, for the followings:(i.e.: Also check the underflow & overflow conditions) Note : 1.Implement using array of structure only. 2.Each book contains book_name,book_id and book_price. 1. Push 5 book informations. 2. Pop 2 book information. 3. Peep the book.4. Display all books from stack.
#include<stdio.h>
#include<stdlib.h>
#define max_size 5
//defining a library of books
struct book{
char book_name[50];
int book_id;
float book_price;
};
int top=-1;
void push(struct book *);//function for inserting value at the top
void pop(struct book *);//function for deleting the value from the top
void peep(struct book *);//function for accessing top element
void display(struct book *);//function for displaying all books.
void main()
{
struct book stack[max_size];
int choice;
while(1){
printf("\tEnter your choice :\n");
printf("\t\t1.Push\n");
printf("\t\t2.Pop\n");
printf("\t\t3.Top element\n");
printf("\t\t4.Display all books\n");
printf("\t\t5.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(top==max_size-1)
printf("Error : Stack is full \n");
else
{
printf("\tEnter book \n\t1.Name\n\t2.Book id\n\t3.Book price\n");
push(stack);
}break;
case 2:
if(top==-1)
printf("Error : The library is empty\n");
else{
pop(stack);
}break;
case 3:
if(top==-1)
printf("Error : The library is empty\n");
else{
peep(stack);
}break;
case 4:
display(stack);break;
case 5:
printf("\nThank you !\n");
return ;
}
}
}
void push(struct book *s)
{
top++;
scanf("%s",s[top].book_name);scanf("%d",&s[top].book_id);scanf("%f",&s[top].book_price);
}
void pop(struct book *s)
{
printf("\nthe book poped is");
printf("\nname:%s",s[top].book_name);
printf("\nid:%d",s[top].book_id);
printf("\nprice:%.2f\n",s[top].book_price);
top--;
}
void peep(struct book *s)
{
printf("\nBook at top:");
printf("\nname:%s",s[top].book_name);
printf("\nid:%d",s[top].book_id);
printf("\nprice:%.2f\n",s[top].book_price);
}
void display(struct book *s)
{
int i;
if(top==-1)
printf("\nStack is empty \n");
else{
for(i=0;i<=top;i++)
{
printf("\nname:%s",s[i].book_name);
printf("\nid:%d",s[i].book_id);
printf("\nprice:%.2f\n",s[i].book_price);
}
}
}
Thanking you !
No comments :
Post a Comment