Monday, May 4, 2009

PROGRAM TO IMPLEMENT STACK

#include
#include
#include
#define MAX 5
int s[MAX],top=-1;
char choice;
void push()
{
do
{
if(top==MAX-1)
{
printf("OVER FLOW\n");
choice='n';
}
else
{
printf("Enter an element\n");
top++;
scanf("%d",&s[top]);
printf("PUSH ANOTHER ELEMENT Y/N:");
fflush(stdin);
scanf("%c",&choice);
}
}while(choice=='Y'choice=='y');
}
void pop()
{
do
{
if(top==-1)
{
printf("\nUNDER FLOW\n");
choice='n';
}
else
{
printf("The poped element is %d\n",s[top]);
top--;
printf("POP ANOTHER ELEMENT Y/N:");
fflush(stdin);
scanf("%c",&choice);
}
}while(choice=='Y'choice=='y');
}
void display()
{
if(top==-1)
printf("\nUNDER FLOW\n");
else
{
printf("The elements present in the stack are\n");
while(top!=-1)
{
printf(" %d\n",s[top]);
top--;
}
}
}
void main()
{
int option;
clrscr();
do
{
printf("\nPress 1 to push an element\nPress 2 to pop an element\nPress 3 to display the elements\nPress 4 to exit\nEnter your option:");
scanf("%d",&option);
switch(option)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
break;
}
}while(option!=4);
}

No comments:

Post a Comment