#include<stdio.h>
#include<conio.h>
#include<process.h>
#define STACK_SIZE 5
void push(int item,int *top,int s[])
{
if(*top==STACK_SIZE-1)
{
printf("stack over flow\n");
return;
}
s[++(*top)]=item;
}
int pop(int *top,int s[])
{
int item;
if((*top)==-1)
{printf("\n underflow");
return 0;
}
item=s[(*top)--];
return item;
}
void display(int top,int s[])
{
int i;
if(top==-1)
{
printf("stack is empty\n");
return;
}
printf("contents of the stack\n");
for(i=0;i<=top;i++)
{
printf("%d\n",s[i]);
}
}
void main()
{int top,s[10],item,choice;
clrscr();
top=-1;
for(;;)
do
{
printf("1:Push 2:pop \n");
printf("3:Display 4:Exit \n");
printf("Enter the choice \n");
printf("enter the choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter the iem to be inserted\n");
scanf("%d",&item);
push(item,&top,s);
break;
case 2:
item=pop(&top,s);
if(item==0)
printf("stack is empty");
else
printf("item deleted=%d\n",item);
break;
case 3:
display(top,s);
break;
case 4:
exit(0);
}}
while(choice>=4);
getch();
}
OUTPUT:-

#include<conio.h>
#include<process.h>
#define STACK_SIZE 5
void push(int item,int *top,int s[])
{
if(*top==STACK_SIZE-1)
{
printf("stack over flow\n");
return;
}
s[++(*top)]=item;
}
int pop(int *top,int s[])
{
int item;
if((*top)==-1)
{printf("\n underflow");
return 0;
}
item=s[(*top)--];
return item;
}
void display(int top,int s[])
{
int i;
if(top==-1)
{
printf("stack is empty\n");
return;
}
printf("contents of the stack\n");
for(i=0;i<=top;i++)
{
printf("%d\n",s[i]);
}
}
void main()
{int top,s[10],item,choice;
clrscr();
top=-1;
for(;;)
do
{
printf("1:Push 2:pop \n");
printf("3:Display 4:Exit \n");
printf("Enter the choice \n");
printf("enter the choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter the iem to be inserted\n");
scanf("%d",&item);
push(item,&top,s);
break;
case 2:
item=pop(&top,s);
if(item==0)
printf("stack is empty");
else
printf("item deleted=%d\n",item);
break;
case 3:
display(top,s);
break;
case 4:
exit(0);
}}
while(choice>=4);
getch();
}
OUTPUT:-
Very awesome!!! When I seek for this I found this website at the top of all blogs in search engine.
ReplyDeletec++ programming