Stack

Program to show the implementation of stack.


#include<stdio.h>
#include<conio.h>
#define stk_size 5
void push(int);
void pop();
void display();
int top=-1,n,s[10],j;
main()
{
int num;
clrscr();
printf("\n1 : PUSH\n2 : POP\n3 : DISPLAY\n4 : DISPLAY AND EXIT\n");
while(j!=5)
{
printf("\nEnter choice ");
scanf("%d",&j);
switch(j)
{
case 1: printf("\nEnter the value to be pushed to the stack ");
scanf("%d",&num);
push(num);
break;
case 2: pop();
break;
case 3: display();
break;
default:display();
break;
}
}
getch();
return 0;
}
void push(int a)
{
if(top==(stk_size-1))
{
printf("\nStack overflow");
return;
}
top++;
s[top]=a;
printf("Element pushed!!");
}
void pop()
{
if(top==-1)
{
printf("\nStack underflow");
return ;
}
n=s[top];
--top;
printf("Element popped..!!");
}
void display()
{
int i;
if(top==-1)
{
printf("\nNo more elements");
return ;
}
for(i=top;i>=0;i--)
printf("%d\n",s[i]);
if(j==4) j=5;
}
Category:

0 comments: