/*STACK operations */
#include<stdio.h>
#include<stdlib.h>
#define stack_size 5
int top;
int s[10];
int item;
void push()
{
if(top==stack_size-1)
{
printf("stack overflow\n");
return;
}
top=top+1;
s[top]=item;
}
int pop()
{
int item_deleted;
if(top==-1)return 0;
item_deleted=s[top--];
return item_deleted;
}
int display()
{
int i;
if(top==-1)
{
printf("stack is empty\n");
return 0;
}
printf("contents of the stack\n");
for(i=0;i<=top;i++)
{
printf("%d\n",s[i]);
}
}
int main()
{
int item_deleted;
int choice;
top=-1;
for(;;)
{
printf("1:push 2:pop\n");
printf("3:display 4:exit\n");
printf("enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter the item to be inserted\n");
scanf("%d",&item);
push();
break;
case 2:item_deleted=pop();
if(item_deleted==0)
printf("stack is empty\n");
else
printf("item deleted = %d\n",item_deleted);
break;
case 3:display();
break;
default:exit(0);
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define stack_size 5
int top;
int s[10];
int item;
void push()
{
if(top==stack_size-1)
{
printf("stack overflow\n");
return;
}
top=top+1;
s[top]=item;
}
int pop()
{
int item_deleted;
if(top==-1)return 0;
item_deleted=s[top--];
return item_deleted;
}
int display()
{
int i;
if(top==-1)
{
printf("stack is empty\n");
return 0;
}
printf("contents of the stack\n");
for(i=0;i<=top;i++)
{
printf("%d\n",s[i]);
}
}
int main()
{
int item_deleted;
int choice;
top=-1;
for(;;)
{
printf("1:push 2:pop\n");
printf("3:display 4:exit\n");
printf("enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter the item to be inserted\n");
scanf("%d",&item);
push();
break;
case 2:item_deleted=pop();
if(item_deleted==0)
printf("stack is empty\n");
else
printf("item deleted = %d\n",item_deleted);
break;
case 3:display();
break;
default:exit(0);
}
}
return 0;
}