Wednesday, August 21, 2013

How to write Simple Stack Program in C

#include<conio.h>
#include<stdio.h>
 
void PUSH();
void PEEP();
int POP();
int top=-1,item,a[10];
void main()
{
int i;
clrscr();
for(i=0;i<12;i++)
{
printf("\nEnter No: ");
scanf("%d",&item);

PUSH(item);
}
POP();
POP();
PEEP();
getch();
}
void PUSH(int itemx)
{
if(top>=10)
{
printf("\n\nStack is Overflow");
}
else
{
top=top+1;
a[top]=itemx;
}
}
void PEEP()
{
int itemx=a[top];
printf("\n\nTop value : %d",itemx);
}
int POP()
{
if(top<=0)
{
printf("\nStack is Underflow ");
}
else
{
item=a[top];
a[top]=NULL;
top=top-1;

printf("\n\tElement POPED ");

return item;
}
}

No comments:

Post a Comment