Write a program to perform Push, Pop, Peep & Update operation on a Stack Array.
CODING:
#include<stdio.h>
#include<conio.h>
#define max 10
int st[max],top=-1;
void push(int st[],int val);
int pop(int st[]);
int peep(int st[]);
int update(int st[]);
void display(int st[]);
void main()
{
int val,ch;
clrscr();
do
{
printf("\nS E L E C T O P E R A T I O N\n");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.PEEK");
printf("\n4.UPDATE");
printf("\n5.OPEN");
printf("\n6.EXITE");
printf("\nEnter The option:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number to be pushed on to the stack:");
scanf("%d",&val);
push(st,val);
break;
case 2:
val=pop(st);
printf("\nThe Value deleted from the stack is:%d",val);
break;
case 3:
val=peep(st);
printf("\nThe Value stored at the top of the stack is:%d",val);
break;
case 4:
val=update(st);
printf("\nThe value is update:%d",val);
break;
case 5:
display(st);
break;
case 6:
printf("PRESS ENTER..");
break;
}
}while(ch!=6);
getch();
}
void push(int st[],int val)
{
if(top==max-1)
{
printf("\nSTACk OVERFLOW");
}
else
{
top++;
st[top]=val;
}
}
void display(int st[])
{
int i;
if(top==-1)
printf("STACK IS EMPTY");
else
{
printf("STACK\n");
for(i=top;i>=0;i--)
printf("\n%d",st[i]);
}
}
int pop(int st[])
{
int temp;
if(top==-1)
{
printf("STACK IS UNDERFLOW");
return -1;
}
else
{
temp=st[top];
top--;
return temp;
}
}
int peep(int st[])
{
if(top==-1)
{
printf("STACK IS EMPTY");
return (st[top]);
}
else
return (st[top]);
}
int update(int st[])
{
int i,update;
printf("Enter the position:");
scanf("%d",&i);
if(top-i+1<=0 )
{
printf("Stack is empty.");
return 0;
}
else
{
update=st[top-i+1];
printf("New value:");
scanf("%d",&st[top-i+1]);
}
return (update);
}
CODING:
#include<stdio.h>
#include<conio.h>
#define max 10
int st[max],top=-1;
void push(int st[],int val);
int pop(int st[]);
int peep(int st[]);
int update(int st[]);
void display(int st[]);
void main()
{
int val,ch;
clrscr();
do
{
printf("\nS E L E C T O P E R A T I O N\n");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.PEEK");
printf("\n4.UPDATE");
printf("\n5.OPEN");
printf("\n6.EXITE");
printf("\nEnter The option:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the number to be pushed on to the stack:");
scanf("%d",&val);
push(st,val);
break;
case 2:
val=pop(st);
printf("\nThe Value deleted from the stack is:%d",val);
break;
case 3:
val=peep(st);
printf("\nThe Value stored at the top of the stack is:%d",val);
break;
case 4:
val=update(st);
printf("\nThe value is update:%d",val);
break;
case 5:
display(st);
break;
case 6:
printf("PRESS ENTER..");
break;
}
}while(ch!=6);
getch();
}
void push(int st[],int val)
{
if(top==max-1)
{
printf("\nSTACk OVERFLOW");
}
else
{
top++;
st[top]=val;
}
}
void display(int st[])
{
int i;
if(top==-1)
printf("STACK IS EMPTY");
else
{
printf("STACK\n");
for(i=top;i>=0;i--)
printf("\n%d",st[i]);
}
}
int pop(int st[])
{
int temp;
if(top==-1)
{
printf("STACK IS UNDERFLOW");
return -1;
}
else
{
temp=st[top];
top--;
return temp;
}
}
int peep(int st[])
{
if(top==-1)
{
printf("STACK IS EMPTY");
return (st[top]);
}
else
return (st[top]);
}
int update(int st[])
{
int i,update;
printf("Enter the position:");
scanf("%d",&i);
if(top-i+1<=0 )
{
printf("Stack is empty.");
return 0;
}
else
{
update=st[top-i+1];
printf("New value:");
scanf("%d",&st[top-i+1]);
}
return (update);
}
Comments
Post a Comment