/*Serial No.140 [swami122.cpp]*/
#include<stdio.h>
#include<conio.h>
int top=-1,stack_arr[5];
void push()
{
if(top<4)
{
top++;
printf("Enter the element to insert= ");
scanf("%d",&stack_arr[top]);
}
else
printf("\nStack is full.\n");
}
void display_stack()
{
printf("\nElements in the stack are:\n");
if(top==-1)
printf("\nStack is empty.\n");
else
{
for(int i=0;i<=top;i++)
{
printf("%d ",stack_arr[i]);
}
}
}
void pop()
{
if(top==-1)
printf("\nStack is Empty.You can't pop.\n");
else
{
int value=stack_arr[top];
top--;
printf("\nElement poped= %d\n",value);
}
}
void main()
{
int choice;
clrscr();
do
{
printf("\n\t[1] Push");
printf("\n\t[2] Pop");
printf("\n\t[3] Display");
printf("\n\t[7] Exit");
printf("\nEnter your choice= ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display_stack();
}
}while(choice!=7);
getch();
}
No comments:
Post a Comment
If you have any doubt, feel free to ask...