Sunday, 17 June 2012

Stack program in c with pop(), push() and display() functions


Stack program in c with pop(), push() and display() function.



#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 5
int top=-1;
void push();
int pop();
void display();
int stack_arr[max];
void main()
{
int choice;
clrscr();
while(1)
{
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Wrong choice\n");
}
}
}

void push()
{
int pushed_item;
if(top==(max-1))
{
printf("Stack is overflow\n");
}
else
{
printf("Enter the item to pushed on stack\n");
scanf("%d",&pushed_item);
top=top+1;
stack_arr[top]=pushed_item;
}
}



int pop()
{
int e;
if(top==-1)
printf("Stack underflow\n");
else
{
e=stack_arr[top];
printf("popped element is %d\n",e);
top=top-1;
}
return(e);
}


void display()
{
int i;
if(top==-1)
printf("Stack is empty\n");
else
{
printf("Stack elements\n");
for(i=top;i>=0;i--)
{
printf("%d\n",stack_arr[i]);
}
}
}

No comments:

Post a Comment