/* Stack Implementation (without graphical display) */
#include<stdio.h>
#include<conio.h>
#define SIZE_OF_STACK 5
struct stack
{
int TOP;
int slot[SIZE_OF_STACK];
};
void push(s,item)
struct stack *s;
int item;
{
if(s->TOP==SIZE_OF_STACK-1)
{
printf("\nERROR : Stack Overflow !");
}
else
{
(s->TOP)++;
s->slot[s->TOP]=item;
printf("\nPush Operation Successful");
}
}
void pop(s)
struct stack *s;
{
if(s->TOP==-1)
{
printf("\nERROR : Stack Underflow !");
}
else
{
printf("%d",s->slot[s->TOP]);
(s->TOP)--;
}
}
void peep(s,slot_number)
struct stack s;
int slot_number;
{
slot_number--;
if(slot_number<0)
{
printf("\nERROR : Invalid Slot Number !");
}
else if(slot_number<SIZE_OF_STACK&&slot_number>s.TOP)
{
printf("\nERROR : Item does not exist. Topmost element is at %d !",s.TOP+1);
}
else if(slot_number>SIZE_OF_STACK-1)
{
printf("\nERROR : Slot Number beyond bounds of Stack. Stack Size is %d !",SIZE_OF_STACK);
}
else
{
printf("%d",s.slot[slot_number]);
}
}
void main()
{
struct stack s1;
int choice,item,slot_number;
s1.TOP=-1;
clrscr();
while(choice!=5)
{
clrscr();
item=0;
printf("\n ÉÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃû");
printf("\n º MAIN MENU º");
printf("\n ÃŒÃÃÃÃÃËÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃù");
printf("\n º º º");
printf("\n º 1 º Push º");
printf("\n º 2 º Pop º");
printf("\n º 3 º Peep º");
printf("\n º 4 º Display all elements º");
printf("\n º 5 º Exit º");
printf("\n º º º");
printf("\n ÈÃÃÃÃÃÊÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃü");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter Item to be pushed : ");
scanf("%d",&item);
push(&s1,item);
break;
case 2:
if(s1.TOP!=-1)
{
printf("\nPopped Value is ");
}
pop(&s1);
break;
case 3:
printf("\nEnter Slot Number : ");
scanf("%d",&slot_number);
if(slot_number>0&&slot_number<=s1.TOP+1)
{
printf("\nPeeped value is ");
}
peep(s1,slot_number);
break;
case 4:
for(slot_number=1,printf("\n");slot_number<=s1.TOP+1;slot_number++)
{
printf(" ");
peep(s1,slot_number);
}
break;
case 5:
printf("\nBye Bye !");
break;
default:
printf("\nEnter a valid choice number (1-5) !");
}
getch();
}
}
Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.
