stack using array
stack using array
h>
#define size 5
int stack[size];
int top=0;
int push(int n);
int pop();
int peek();
int display();
int main();
int push(int n)
{
stack[top]=n;
}
int pop()
{
if(top==0)
printf("Stack is empty");
else
top--;
}
int peek()
{
if(top==0)
printf("Stack is empty");
else
printf("The top element is %d",stack[top-1]);
}
int display()
{
int i;
if(top==0)
printf("Stack is empty");
else
{
for(i=top-1;i>=0;i--)
printf("%d\t",stack[i]);
}
}
int main()
{
int option,n;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit");
printf("\n\nPlease enter you choice of operation : ");
scanf("%d",&option);
switch(option)
{
case 1:
if(top==size)
printf("Stack is full");
else
{
printf("Enter a number to add to the stack : ");
scanf("%d",&n);
push(n);
top++;
}
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice entered");
}
}
}