1.Push pop
a. Push
b. Pop
c. Display
The program should print appropriate messages for stack overflow, stack underflow and stack
empty.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX_SIZE 4
int s[MAX_SIZE],top=-1;
void main()
{
void PUSH(int);
void POP();
void DISPLAY();
int ch,x;
while(1)
{
clrscr();
printf("\n enter your choice \n\n 1.PUSH \n 2.POP \n 3.DISPLAY \n 4.EXIT \n\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n enter the element to be inserted \n");
scanf("%d",&x);
PUSH(x);
break;
case 2: POP();
break;
case 3: DISPLAY();
break;
case 4: printf("\n Bye! See you next time.... \n");
getch();
exit(0);
default: printf("\n Illegal choice, try again \n");
}
printf("\n\n press any key to continue........\n");
getch();
}
}
void PUSH(int x)
{
if(top>=MAX_SIZE-1)
printf("\n stack overflow, insertion not possible \n");
else
{
top++;
s[top]=x;
printf("\n element %d has been inserted \n",x);
}
}
void POP()
{
int temp;
if(top==-1)
printf("\n stack underflow, deletion not possible \n");
else
{
temp=s[top];
top--;
printf("\n deleted element is %d \n",temp);
}
}
void DISPLAY()
{
int i;
if(top==-1)
printf("\n stack is empty, no elements to display \n");
else
{
printf("\n\n the elements on the stack are \n\n");
for(i=0;i<=top;i++)
printf("%d\t",s[i]);
}
}
Comments
Post a Comment