#include<stdio.h>
#include <stdlib.h>
int s[100],top=0,n;
void push()
{
int x;
if(top>=n)
{
printf("stack overflow\n");
}
else
{
printf("enter number:");
scanf("%d",&x);
top++;
s[top]=x;
}
}
void pop()
{
if(top<=0)
{
printf("stack underflow\n");
}
else
{
printf("%d is popped\n",s[top]);
top--;
}
}
void peep()
{
int i;
printf("enter ith position:");
scanf("%d",&i);
if(top-i+1<=0)
{
printf("stack underflow\n");
}
else
{
printf("%d is %dth valur from the top\n",s[top-i+1],i);
}
}
void change()
{
int i,x;
printf("enter ith position from top to change:");
scanf("%d",&i);
if(top-i+1<=0)
{
printf("stack underflow\n");
}
else
{
printf("enter new value:");
scanf("%d",&x);
s[top-i+1]=x;
printf("new value of %dth position from top is %d\n",i,x);
}
}
int main()
{
int choise;
printf("enter length of stack:");
scanf("%d",&n);
printf(" 1 for push \n");
printf(" 2 for pop \n");
printf(" 3 for peep \n");
printf(" 4 for change \n");
printf(" 0 for exit \n");
while(1)
{
printf("enter choise:");
scanf("%d",&choise);
switch(choise)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peep();
break;
case 4:
change();
break;
case 0:
exit(1);
break;
default:
printf("enter valid number\n");
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter