STACK OPERATION USING ARRAYS


Program

#include <>
#include <>
int n,i,ch,top=-1,s[20],value;
void main()
{
int ch;
clrscr();
printf(“\n\t\t STACK OPERATION USING ARRAYS”);
printf("\n\t\t---------------------------------------------------- ");
printf("\n Enter the stack size:");
scanf("%d",&n);
do
{
printf("\n 1.PUSH\n 2.POP\n 3.PEEK\n 4.DISPLAY\n 5.EXIT");
printf("\n Enter Your Choice:");
scanf("%d", &ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
}
}while(ch!=5);
getch();
}


push()
{
if(top==n-1)
printf("\n Stack is full");
else
{
printf("\n Enter The Data :");
scanf("%d",&value);
top++;
s[top]=value;
printf("\n The inserted data is:%d",value);
}
}

pop()
{
if(top==-1)
printf("\n Stack is empty");
else
{
value=s[top];
top--;
printf("\n The deleted data is:%d",value);
}
}

peek()
{
if(top==-1)
printf("\n Stack is empty");
else
{
value=s[top];
printf("\n The peeked value is:%d",value);
}
}


display()
{
if(top==-1)
printf("\n Stack is empty");
else
{
printf("\n Stack Values are:");
for(i=top;i >=0;i--)
printf("\n%d",s[i]);
}
}