Sunday, May 29, 2016

WAP to implement stack with all its operations(push, pop, peep, change, display)

#include<stdio.h>
#include<conio.h>
int top=-1;
int st[10];
void push(int n)
{
if(top==10)
printf("Stack is full");
else
{
top++;
st[top]=n;
}

}
int pop()
{
int x;
if(top==-1)
printf("\nstack is empty");
else
{
x=st[top];
top--;
}
return x;
}
void peep(int n)
{
if(n>top)
{
printf("Out of Range");
}
else
{
printf("Value at ps %d is %d",n,st[top-n+1]);
}
}
void change(int p,int v)
{
if(p>top)
{
printf("Out of Range");
}
else
{
st[top-p+1]=v;
}
}
void display()
{
int i;
if(top==-1)
{
printf("Stack is empty");
return;
}
printf("\n displaying stack:");
for(i=top;i>=0;i--)
{
printf("\n%d",st[i]);
}
}
void main()
{
int n,v,p;
clrscr();
do
{
printf("\n1.Push\n ");
printf("2.Pop\n ");
printf("3.Peep\n ");
printf("4.Change\n ");
printf("5.Display\n ");
printf("6.Exit\n ");
printf("\nEnter your choice : ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nenter a value :");
scanf("%d",&v);
push(v);
break;
case 2:
v=pop();
printf("delete number is: %d",v);
break;
case 3:
printf("\nenter a positon :");
scanf("%d",&p);
peep(p);
break;
case 4:
printf("\nenter a postion :");
scanf("%d",&p);
printf("\nenter a value :");
scanf("%d",&v);
change(v,p);
break;
case 5:
display();
break;
case 6:
printf("\nThank you");
break;
default:
printf("\nYou have entered a wrong value");
}
}while(n!=6);
getch();
}

No comments:

Post a Comment