Page 132 - DCAP407_DATA_STRUCTURE
P. 132

Unit 7:  Stacks



                                      Enter the element to insert in the stack
                                      50
                                      Push
                                      Pop
                                      Display
                                      Exit
                                      Enter the choice
                                       1
                                      Enter the element to insert in the stack
                                      60
                                      Push
                                      Pop
                                      Display
                                      Exit
                                      Enter the choice
                                        3
                                       The item is 60
                                       The item is 50
                                       Push
                                       Pop
                                       Display
                                       Exit
                                       Enter the choice
                                        2
                                       Push
                                       Pop
                                       Display
                                       Exit
                                       Enter the choice
                                       3
                                      The item is 50


                               In this example:
                               1.  The header files are included and a constant value 10 is defined for variable
                                   MAX using #define header.
                               2.  An integer stack array named stack_arr[MAX] is declared. The stack array
                                   can hold 10 elements.
                               3.  Three functions are created namely, push() , pop() and display(). The user
                                   has to select an appropriate function to be performed.
                               4.  The switch statement is used to call the push(), pop(), and display()
                                   functions.

                               5.  When the user enters 1, the push() function is called. In the push() function,
                                   the if loop checks for the stack size. If the stack array is full, the program
                                   displays a message “Overflow. Stack is full”. Else, it takes the input from the
                                   user and inserts into the stack.
                               6.  When the user enters 2, the function pop() is called. In the pop() function,
                                   the if loop checks for the stack size. If the stack array is empty, the program
                                   displays a message “Stack underflow”. Else, the pop() function pops the
                                   elements present in the stack array.









                                        LOVELY PROFESSIONAL UNIVERSITY                          125
   127   128   129   130   131   132   133   134   135   136   137