Friday, 8 March 2013

Write a program in C using integers to perform all stack operations.


Program that performs all stack operation using array of integers.

/*Program of stack using array*/
#include<stdio.h>
#define MAX 5

int top=1;
int stack_arr[MAX];

main()
{
int choice;
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:
exit(1);
default:
     printf(“Wrong Coice\n”)
}/*End of Switch*/
}/*End of While*/
}*/End of main()*/
/*push function*/
push()
{
     int pushed_item;
     if(top==(MAX-1))
     {
          printf(“Stack Overflow\n”);
     }
     else
     {
printf(“Enter the item to be pushed in stack :”);
scanf(“%d”, & pushed_item);
top=top+1;
stack_arr[top]=pushed_item;
     }
}/*End of push()*/
/*pop function*/
pop()
{
     if(top==-1)
     {
          printf(“Stack Underflow\n”);
}
     else
{
printf(“Popped Element is : %d\n”,stack_arr[top]);
top=top-1;
}
}/*End of pop()*/
/*display function*/
display()
{
int I;
if(top==-1)
{
printf(“Stack is Empty\n”);
}
else
{
printf(“Stack Element : \n”);
for (i=top;i>=0;i--)
{
     printf(“%d\n”,stack_arr[i]);
}
}
}/*End of display()*/