#include<stdio.h>
#include<stdlib.h>
void push(int s[],int item);
int pop(int s[]);
void display();
# define stacksize 4
int s[stacksize];
int top=-1;
int main()
{
int ch,item;
while(1)
{
printf("\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : scanf("%d",&item);
push(s,item);break;
case 2 : pop(s);break;
case 3 : display();break;
case 4 : if(top==-1)
{
printf("Empty stack- No peek\n");
}
else
{
printf("The top most element in stack is :%d\n",s[top]);
};break;
case 5 : exit(0);
default : printf("Invalid Choice");
//exit(0);
}
}
return 0;
}
void push(int s[],int item)
{
if(top==stacksize-1)
{
printf("Stack Overflow-Cannot Push\n");
return;
}
top = top+1;
s[top]=item;
}
int pop(int s[])
{
if(top==-1)
{
printf("Stack Underflow-Cannot Pop\n");
return 0;
}
printf("The popped element is :%d\n",s[top--]);
}
void display()
{
if(top==-1)
{
printf("Stack is Empty-No elements to Display\n");
return;
}
for(int i=top;i>=0;i--)
{
printf("%d\n",s[i]);
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: