#include<stdio.h>
#include<conio.h>
int stk[10];
int top=-1;
void push(int);
int pop();
void disp();
void main()
{
int ch,item;
clrscr();
while(1)
{
printf("\n\n1....Push\n2....Pop\n3.....Display\n4....Exit");
printf("\nEnter Ur Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter Item:");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
if(item!=-1)
printf("\nItem Poped= %d",item);
break;
case 3:
disp();
break;
case 4:
printf("\program terminating...\nPress any key to continue...");
exit(0);
default:
printf("\nEnter correct choice");
}}}
void push(int item)
{
if (top==9)
{
printf("stack overflow");
return ;
}
top=top+1;
stk[top]=item;
}
int pop()
{
int item;
if(top==-1)
{
printf("stack Underflow");
return (-1);
}
item=stk[top];
top=top-1;
return (item);
}
void disp()
{
int i;
if(top==-1)
{
printf("stack Underflow");
return ;
}
printf("\nStack is:");
for(i=top;i>=0;i--)
printf("\n%d",stk[i]);
}
Output:
#include<conio.h>
int stk[10];
int top=-1;
void push(int);
int pop();
void disp();
void main()
{
int ch,item;
clrscr();
while(1)
{
printf("\n\n1....Push\n2....Pop\n3.....Display\n4....Exit");
printf("\nEnter Ur Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter Item:");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
if(item!=-1)
printf("\nItem Poped= %d",item);
break;
case 3:
disp();
break;
case 4:
printf("\program terminating...\nPress any key to continue...");
exit(0);
default:
printf("\nEnter correct choice");
}}}
void push(int item)
{
if (top==9)
{
printf("stack overflow");
return ;
}
top=top+1;
stk[top]=item;
}
int pop()
{
int item;
if(top==-1)
{
printf("stack Underflow");
return (-1);
}
item=stk[top];
top=top-1;
return (item);
}
void disp()
{
int i;
if(top==-1)
{
printf("stack Underflow");
return ;
}
printf("\nStack is:");
for(i=top;i>=0;i--)
printf("\n%d",stk[i]);
}
Output:
Post a Comment