Pages

Thursday, December 10, 2015

Implementation of Stack

#include<stdio.h>
#include<conio.h>
#include<process.h>

void push();
void pop();
void display();
int stack[5];
int top=-1;
void main()
{
   int choice;
   do
     {
          printf("\n 1. PUSH\n");
          printf("\n 2. POP\n");
          printf("\n 3. DISPLAY\n");
          printf("\n 4. EXIT\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(0);
           }
          }
            while(choice!=4);
            getch();
       }
void push()
  {
    int item;
    if(top==4)
    printf("\n Stack overflow\n");
    else
      {
          printf("\n Enter the value of item\n");
          scanf("%d",&item);
          top=top+1;
          stack[top]=item;
          printf("\n %d is inserted successfully",item);
     }
  }
  void pop()
  {
    int item;
    if(top==-1)
    printf("\n Stack is empty\n");
    else
      {
          item=stack[top];
          top=top-1;
          printf("%d is deleted successfully\n",item);
     }
  }
   void display()
  {
    int i;
    if(top==-1)
    printf("\n No item to display\n");
    else
      {
          for(i=top;i>=0;i--)
          printf("ITEM: %d\t",stack[i]);
     }

  }

No comments:

Post a Comment