Thursday, June 27, 2013

Write a menu program to demonstrate the simulation of stack operation in array implementation.



#include<stdio.h>
#include<conio.h>
#define N 50
void make(stack *);
int isfull(stack *);
int isempty(stack *);
void push(stack *,int);
int pop(stack *);
void display(stack *);
typedef struct{
                int s[N];
                int top;
                }stack;

 void make(stack *ps)
  {
  ps->top=-1;
  }

  int isfull(stack *ps)
    {
    if(ps->top==N-1)
     return(1);
    else
     return(0);
     }

  int isempty(stack *ps)
    {
    if(ps->top==-1)
     return(1);
     else
     return(0);
     }

  void push(stack *ps,int item)
     {
     if(isfull(ps))
       printf("\nStack is full");
     else
       {ps->top=ps->top+1;
       ps->s[ps->top]=item;
    }
}

   int pop(stack *ps)
       {
          if(isempty(ps))
            printf("\nStack is empty");
          return(0);
          else
            return(ps->s[ps->top--]);
       }

   void display(stack *ps)
     {
     int i;
    if(isempty(ps))
      printf(“The stack is empty”);
   else
     {
     for(i=ps->top;i<=0;i--)
       printf("\n%d",ps->s[i]);
       }
    }

    main()
      {
      stack st;
      int ch,el;
      clrscr();
      do{
      printf("\n1:To push item in a stack");
      printf("\n2:To pop item from a stack");
      printf("\n3:To display item in a stack");
      printf("\n4:To exit");
      scanf("%d",&ch);
      make(st);
      switch(ch)
          {
           case 1:
              printf("\nEnter the element");
              scanf("%d",el);
              push(st,el);
              break;
           case 2:
              printf("\n%d",pop(st));
              break;
           case 3:
              display(st);
              break;
          }
     }while(ch!=4);
     getch();
     }

No comments:

Post a Comment