Page 498 - Computer science 868 Class 12
P. 498

Step 2:  22 is now the topmost element and is popped
                           from the stack.
                                                                       22                    top is at 1

                                                                       15         stk[0]

                     Step 3:  The topmost element 15 is popped from the
                           stack.

                                                                       15                   top is at 0

                     Step 4:  Another pop attempt is made.        Top is 0 and the stack is empty. Stack Underflow message
                                                                  will be displayed.

              Algorithm to push an element into stack array stk[] with top initialised to 0 is given below.

              void push(int stk[], int max, int num)
              Step 1:  Start.

              Step 2:  Declare an array stk[] of size max and initialise top to 0

              Step 3:  If top = max display “Stack Overflow”, go to Step 7
              Step 4:  Push integer num into stack stk[top]

              Step 5:  Increase top by 1
              Step 6:  Go to Step 3 if you want to continue

              Step 7:  Stop.
              Algorithm to pop elements from stack array stk[] is given below.

              void pop(int stk[], int top)

              Step 1:  Start.
              Step 2:  If top = 0 display “Stack Underflow”, go to Step 6

              Step 3:  Pop element from stk[top]
              Step 4:  Decrease top by 1

              Step 5:  Go to Step 2 if you want to continue
              Step 6:  Stop.

              Algorithm to display elements in tack array stk[] is given below:

              void display(int stk[], int top)
              Step 1:  Start.

              Step 2:  Initialise i to top-1
              Step 3:  Repeat Steps 4 and 5 while i>=0

              Step 4:  Display stk[i]
              Step 5:  Decrease i by 1

              Step 6:  Stop.




                496496  Touchpad Computer Science-XII
   493   494   495   496   497   498   499   500   501   502   503