#include<stdio.h>
int stack[100];
int opt,q,top,p,j;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter stack :");
scanf("%d",&q);
printf("\n\t Stack using array:");
printf("\n\t 1.Push\n\t 2.Pop\n\t 3.Display\n\t 4.Exit");
do
{
printf("\n Input the option:");
scanf("%d",&opt);
switch(opt)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t Exit ");
break;
}
default:
{
printf ("\n\t Invalid option");
}
}
}
while(opt!=4);
return 0;
}
void push()
{
if(top>=q-1)
{
printf("\n\tStack is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&p);
top++;
stack[top]=p;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in Stack \n");
for(j=top; j>=0; j--)
printf("\n%d",stack[j]);
printf("\n Press Next Choice");
}
else
{
printf("\n Stack is empty");
}
}
Comments
Leave a comment