Stack Program


//Stack

#include<stdio.h>
#include<conio.h>
#define SIZE 4
int ch,top=-1,stack[SIZE],item;
void main()
{
 clrscr();
 while(1)
 {
  printf("*** Menu ***\n 1. Push\n 2. Pop\n 3. Display\n 4. Exit\n");
  printf("Enter your choice");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1: push();
  break;
   case 2: pop();
  break;
   case 3: display();
  break;
   case 4: exit();
   default:
  printf("You are out of choice!");
   }
  }
}

int push()
{
 if(top==SIZE-1)
  printf("Stack overflow i.e. full!");
 else
 {
  printf("Enter the item to be pushed into the stack");
  scanf("%d",&item);
  top++;
  stack[top]=item;
  }
  return 0;
}

int pop()
{
 if(top==-1)
  printf("Stack is empty\n");
 else
 {
  item=stack[top];
  top--;
  printf("The deleted item from stack is %d\n",item);
 }
 return 0;
}

int display()
{
 int i;
 if(top==-1)
  printf("Stack underflow!");
 else
 {
  printf("Stack display\n");
  for(i=top;i>=0;i--)
  {
   printf("Element %d",stack[i]);
  }
  printf("\n");
 }
 return 0;
}

Post a Comment

Previous Post Next Post