You are on page 1of 2

#include<stdio.

h>
#include<stdlib.h>
#define max_size 10
int stack_arr[max_size];
int top=-1;
void push(int item);
int pop();
int isFull();
int isEmpty();
int peek();
void display();
void main()
{
int choice,item;
while(1)
{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display all stack elements\n");
printf("4.Quit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the item to be pushed:");
scanf("%d",&item);
push(item);
break;
case 2: item=pop();
if(item!=0)
printf("Popped item is: %d\n",item);
break;
case 3: display();
break;
case 4:exit(1);
default:
printf("Wrong choice");
}
}
}
void push(int item)
{
if(isFull())
{
printf("Stack Overflow");
return;
}
top=top+1;
stack_arr[top]=item;
}
int pop()
{
int item;
if(isEmpty())
{
printf("Stack Underflow");
return 0;
}
item=stack_arr[top];
top=top-1;
return item;
}
int isFull()
{
if(top==max_size-1)
return 1;
else
return 0;
}
int isEmpty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
int peek()
{
if(!isEmpty())
return stack_arr[top];
}
void display()
{
if(isEmpty())
{
printf("Stack is empty");
return;
}
printf("Stack elements: \n\n");
for(int i=top;i>=0;i--)
printf(" %d\n",stack_arr[i]);
}

You might also like