Stack implementation

Program to implement stack using array.


#include<stdio.h>
#include<conio.h>
#define stk_size 5
void push(int);
void pop();
void display();
int top=-1,n,s[10];
main()
{
clrscr();
push(20);
push(30);
pop();
push(8);
display();
getch();
return 0;
}
void push(int a)
{
if(top==(stk_size-1))
{
printf("\nStack overflow");
return;
}
top++;
s[top]=a;
}
void pop()
{
if(top==-1)
{
printf("\nStack underflow");
return ;
}
n=s[top];
--top;
}
void display()
{
int i;
if(top==-1)
{
printf("\nNo more elements");
return ;
}
for(i=top;i>=0;i--)
printf("%d\n",s[i]);
}
Category:

0 comments: