This is a very simple stack progam using C language
#include <stdio.h>
#include<stdlib.h>
#define MAX 9
int stack[10];
int top=-1;
void push(int item)
{
if(top==MAX)
{
printf("\nOver Flow.....");
return;
}
else
{
top=top+1;
stack[top]=item;
}
}
int pop()
{
int a;
if(top==-1)
{
printf("\nUnder flow......");
return -1;
}
else
{
a=stack[top];
top=top-1;
return a;
}
}
void display()
{
int i;
for(i=0;i<=top;i++)
{
printf("\nPosition: %d\tVaue: %d",i,stack[i]);
}
}
void main()
{
int c,item;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter item to insert: ");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
if(item!=-1)
printf("\n%d is deleted",item);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong choice");
}
}
}
#include <stdio.h>
#include<stdlib.h>
#define MAX 9
int stack[10];
int top=-1;
void push(int item)
{
if(top==MAX)
{
printf("\nOver Flow.....");
return;
}
else
{
top=top+1;
stack[top]=item;
}
}
int pop()
{
int a;
if(top==-1)
{
printf("\nUnder flow......");
return -1;
}
else
{
a=stack[top];
top=top-1;
return a;
}
}
void display()
{
int i;
for(i=0;i<=top;i++)
{
printf("\nPosition: %d\tVaue: %d",i,stack[i]);
}
}
void main()
{
int c,item;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter item to insert: ");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
if(item!=-1)
printf("\n%d is deleted",item);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong choice");
}
}
}
0 comments:
Post a Comment
You are most welcome for making comments here. I will consider your view and will reply you soon.