#include
#include
#define MAX 20
#define TRUE 1
#define FALSE 0
void push();
void display();
struct stack /* stack declarations */
{
int top,items[MAX];
}s;
main()
{
int i,n;
int choice,x;
clrscr();
s.top=-1;
while(1)
{
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.TOPTEST\n");
printf("\n enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nenter the values of n:");
scanf("%d",&n);
printf("\n enter the items");
for(i=0;i
{
scanf("%d",&x);
push(&s,x);
}
printf(" the stack items are:\n");
display(&s);
break;
case 2:
x=pop(&s);
printf("\n the value of x=%d\n",x);
break;
case 3:
display(&s);
break;
case 4:
x=toptest(&s);
printf("\n The top most item is %d\n",x);
break;
default:
printf("\n Invalid Entry");
getch();
exit(1);
}
}
getch();
}
/* function to push items to stack */
void push(struct stack *ps,int y)
{
if(ps->top==MAX-1)
{
printf("\n stack overflow");
getch();
exit(1);
}
else
ps->items[++(ps->top)]=y;
}
/* function to pop items from stack */
int pop(struct stack *ps)
{
int x;
if(empty(ps))
{
printf("\n stack underflow");
getch();
exit(1);
}
else
x=ps->items[ps->top--];
return(x);
}
/* function to check whether stack is empty */
int empty(struct stack *p)
{
if(p->top==-1)
return(TRUE);
else
return(FALSE);
}
/* function to display stack items */
void display(struct stack *ps)
{
int i,x;
for(i=ps->top;i>=0;i--)
{
x=ps->items[i];
printf("\n %d",x);
}
printf("\n");
}
/* fuction to check the top of stack */
int toptest(struct stack *ps)
{
int x;
if (empty(ps))
{
printf("\n Underflow");
getch();
exit(1);
}
else
{
x=ps->items[ps->top];
return(x);
}
}
No comments:
Post a Comment