WAP
to show Stack functions
#include<conio.h>
#include<stdio.h>
#include<process.h>
int stack[20],top=-1,ch;
void display()
{
if(top>=0)
{
printf("\n\n Index > value\n");
for(int
i=0; i<top;i++)
{
printf("\n %d ",i);
printf(" >
%d",stack[i]);
}
printf("\n top(%d)> %d",i,stack[i]);
}
else
{
printf("\n\nNo element to display.");
}
getch();
}
void push()
{
int val;
if(top>19)
{
printf("\nStack
Overflow!!!");
getch();
}
else
{
printf("\nEnter the element to be pushed:");
scanf("%d",&val);
stack[++top]=val;
display();
}
}
void pop()
{
int val;
if(top<0)
{
printf("\nStack Underflow!!!");
getch();
}
else
{
val=stack[top--];
printf("\nElement popped:%d",val);
display();
}
}
void change()
{
int
temp,in;
if(top>=0)
{
display();
agn2:
printf("\nEnter the index of the element to be interchanged with
top:");
scanf("%d",&in);
if(in>=0 && in<top)
{
temp=stack[top];
stack[top]=stack[in];
stack[in]=temp;
printf("\nAfter change");
display();
}
else
{
printf("\nEnter valid index");
getch();
goto agn2;
}
}
else
{
printf("\nStack is empty.");
getch();
}
}
void main()
{
agn:
clrscr();
printf("\n\tMenu\n1.Push\n2.Pop\n3.Display\n4.Change\n5.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
push(); goto agn;
case 2:
pop(); goto agn;
case 3:
display(); goto agn;
case 4:
change(); goto agn;
case 5:
exit(0);
default:
printf("\n Please Enter a valid choice.");
goto
agn;
}
}
Output:
No comments:
Post a Comment