Page 131 - DCAP407_DATA_STRUCTURE
P. 131

Data Structure



                                          accepted*/
                                                          printf(“\n Enter the element to insert in the stack”);
                                                          scanf(“%d”,&item);         /* accepts the value entered  in
                                          variable item*/
                                                          top=top+1;          /*after the insertion, top is incremented
                                          by 1*/
                                                         stack_arr[top]=item;
                                                        }
                                                        }
                                                        int pop()          /* If  the value entered is 2, the  pop  method is
                                          called*/
                                                        {
                                                          int i  = 0;
                                                          if(top==-1)     /* if condition checks whether the stack is empty
                                          or not*/
                                                        {
                                                             printf(“\n Stack underflow”);   /* If the stack is empty, the
                                          message is displayed*/
                                                         getch();
                                                         exit(0);
                                                        }
                                                      else
                                                      {
                                                        i=stack_arr[top];     /* If  the stack is not empty,  the item is
                                          deleted*/
                                                        top=top-1;          /* top is decremented by 1*/
                                                      }
                                                       return i;
                                                      }
                                                  void display()     /* display method displays the status of the stack*/
                                                 {
                                                   int i;
                                                   if(top==-1)        /* if condition checks whether the stack is empty or
                                          not*/
                                                  {
                                                    printf(“\n Stack is empty”);
                                                    getch();
                                                    exit(0);
                                                   }
                                                  else
                                                  {
                                                  for(i=top;i>=0;i--)
                                                 {
                                                    printf(“\n The item is %d”,stack_arr[i]);  /* displays all the items in the
                                          stack*/
                                                  }
                                                  }
                                                  }
                                          Output:
                                                 Push
                                                 Pop
                                                 Display
                                                 Exit
                                                 Enter the choice
                                                  1





                          124                     LOVELY PROFESSIONAL UNIVERSITY
   126   127   128   129   130   131   132   133   134   135   136