#include<stdio.
h>
#include<conio.h>
#include<dos.h>
void display();
void push();
void pop();
void peep();
void update();
int stack[5],top=-1;
void main()
{
int ch;
while(1)
{
clrscr();
printf(" 1. Push \n 2. Pop \n 3. Display \n 4. Peep \n 5. Update \n 6. Exit");
printf("\n Enter choice : ") ;
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peep();
break;
case 5:
update();
break;
case 6:
exit(0);
}
getch();
}
}
void push()
{
int val;
if(top>=4)
{
printf("\n Stack overflow");
}
else
{
printf("\nEnter value :");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("\n Stack underflow");
}
else
{
printf("\n %d",stack[top]);
top=top-1;
}
}
void display()
{
int i;
for(i=top;i>=0;i--)
{
printf("\n %d",stack[i]);
}
}
void peep()
{
int num;
printf("\n Enter number : ");
scanf("%d",&num);
if((top-num+1)<=-1)
{
printf("\n Invalid number");
}else
{
printf("\n %d is located at %d ",stack[top-num+1],num);
}
}
void update()
{
int num;
printf("\n Enter number : ");
scanf("%d",&num);
if((top-num+1)<=-1)
{
printf("\n Invalid number");
}else
{
printf("\n %d is located at %d ",stack[top-num+1],num);
printf("\n Enter value : ");
scanf("%d",&stack[top-num+1]);
}
}
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void display();
void push();
void pop();
void peep();
void update();
int *stack,size,top=-1;
void main()
{
int ch;
clrscr();
printf("Enter size of the stack : ");
scanf("%d",&size);
while(1)
{
clrscr();
printf(" 1. Push \n 2. Pop \n 3. Display \n 4. Peep \n 5. Update \n 6. Exit");
printf("\n Enter choice : ") ;
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peep();
break;
case 5:
update();
break;
case 6:
exit(0);
}
getch();
}
}
void push()
{
int val;
if(top>=size+1)
{
printf("\n Stack overflow");
}
else
{
printf("\nEnter value :");
scanf("%d",&val);
if(stack==NULL)
{
stack=(int*)calloc(1,sizeof(int));
}
else
{
stack=(int*)realloc(stack,1*sizeof(int));
}
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
{
printf("\n Stack underflow");
}
else
{
printf("\n %d",stack[top]);
free(stack[top]);
top=top-1;
}
}
void display()
{
int i;
for(i=top;i>=0;i--)
{
printf("\n %d",stack[i]);
}
}
void peep()
{
int num;
printf("\n Enter number : ");
scanf("%d",&num);
if((top-num+1)<=-1)
{
printf("\n Invalid number");
}else
{
printf("\n %d is located at %d ",stack[top-num+1],num);
}
}
void update()
{
int num;
printf("\n Enter number : ");
scanf("%d",&num);
if((top-num+1)<=-1)
{
printf("\n Invalid number");
}else
{
printf("\n %d is located at %d ",stack[top-num+1],num);
printf("\n Enter value : ");
scanf("%d",&stack[top-num+1]);
}
}