Monday 12 December 2011

WAP to inplement the concept of stack with entring 5 elements, deleting & displaying the stack on the console screen.


#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
void push();
void pop();
void display();
void exo();
int stack[MAX],i,top=-1,ch;
void main()
{

clrscr();
do
{
printf("\nEnter your choice");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit ");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exo();
break;
default: printf("\nWrong Choice ");
break;
}
}while(ch!=4);
getch();
}
void push()
{
if(top==MAX-1)
{
printf("\nStack is full ");
}

else
{
top++;
printf("\nEnter the element ");
scanf("%d",&stack[top]);

}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty ");
}
else
{
printf("\nThe deleted element is %d",stack[top]);

}
top--;
}
void display()
{
if(top==-1)
{
printf("\nStack doesn't have any element in it ");
}
else
{
printf("\n The stack elements are:-");
for(i=top;i>=0;i--)
{

printf("\n%d",stack[i]);

}
}
}
void exo()
{
exit (0);
}

2 comments: