PUSH AN ELEMENT IN STACK USING ARRAY
#include<conio.h>
#include<stdio.h>
#define MAX 5
int st[MAX];
int top=-1;
void push(int st[],int val);
int pop(int st[]);
int pep(int st[]);
void display( int st[] );
void main()
{
int option ,val;
clrscr();
do{
printf("\n main menu\n");
printf("\n1:PUSH\n");
printf("\n2:POP\n");
printf("\n3:PEEP\n");
printf("\n4:DISPLAY");
printf("\n5:EXIT");
printf("\nenter your option: ");
scanf("%d",&option);
switch(option)
{
case 1: printf("\n enter value to insert in stack\n");
scanf("%d",&val);
push(st,val);
break;
case 2: val=pop(st);
if(val!=-1)
// printf("\n underflow");
//else
printf("\n %d deleted from the stack ",val);
break;
case 3: val=pep(st);
if(val!=-1)
printf("\n%d is stored at top ",val);
break;
case 4: display(st);
break;
case 5:exit(0);
default :
printf("not a correct option") ;
}
}
while(option!=5);
getch();
}
void push(int st[],int val)
{
if(top==MAX-1)
{
printf("\nover flow");
}
else
{
top++;
st[top]=val;
}
}
int pop(int st[])
{
int val;
if(top==NULL)
{
printf("\nunder flow");
}
else
{
val=st[top];
top--;
}
return val;
}
int pep(int st[])
{
if(top==-1)
{
printf("\nstack is empty");
return-1;
}
else
{
return(st[top]) ;
}
}
void display(int st[])
{
int i;
if(top==NULL)
{
printf("\nstack is empty");
}
else
{
for(i=top;i>=0;i--)
{
printf("\n%d",st[i]);
}
}
}
Post a Comment