Saturday, December 5, 2009

STACK

Simple easy stack program in c language.This is one of the concept of data structure.

The Example program is ,

#include
#include
void main()
{

int top=0,i,n,ch,a[10],s=0,f=0;
clrscr();
while(1)
{
printf("\n\t\t\t MENU ");
printf("\n\t\t1.PUSH");
printf("\n\t\t2.POP");
printf("\n\t\t3.DISPLAY");
printf("\n\t\t4.EXIT");
printf("\n\t\tENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
clrscr();

printf("\n\t PUSH ");
printf("\nENTER THE TOTAL ELEMENT:");
scanf("%d",&n);
if((top==n)||(f==1))
{
printf("STACK FULL");
else if((n<=s)||(top
{
printf("ENTER THE VALUE ONE BY ONE:");
for(i=1;i<=n;i++)
{
top=top+1;
scanf("%d",&a[top]);
}
printf("top %d",top);
f=1;
}
break;
case 2:
clrscr();

printf("\n\n\n\t\t\t POP");
if(top==0)
printf("\nSTACK EMPTY");
else
{
top=top-1;
s=s+1;
printf("\nTHE POPED ELEMENT IS:%d",a[top+1]);
f=0;
}
break;
case 3:
clrscr();

printf("\n\n\n\n\t\t\t DISPLAY");
for(i=top;i>0;i--)
{
printf("\nTHE VALUE IS:%d",a[i]);
}
break;
case 4:
clrscr();

printf("\n\n\n\n\t\t\t EXIT");
exit(0);
break;
}
}
}

0 comments:

Post a Comment

  © Blogger template Coozie by Ourblogtemplates.com 2008

Back to TOP