Page 497 - Computer science 868 Class 12
P. 497

When the teacher returns the checked copies, then also the same pattern is followed. The topmost copy is returned
                 first, followed by the second copy from top till the last copy at the bottom.
                 Similarly, in an invitation hall, the glass plates are piled one upon another. We know that the plates are always taken
                 from top or kept at the top only.


                 13.1.1 Basic Operations on Stack
                 The following operations can be performed on a stack:

                 1.  Push: The process of entering an element into a stack is called push. The first element inserted is pushed to the
                   bottom. The top pointer increases by 1 and the next element entered is placed at the top of the previous element.
                   The process continues until we reach to the maximum size when further data cannot be entered. This situation is
                   called Stack Overflow.
                 2.  Pop: The process of removing data from a stack is called pop. The last or the topmost element is popped out first. The
                   top pointer then decreases by 1. The next element is now the topmost element which will be popped next. The process
                   continues until all the elements are popped out and the stack is empty. This condition is called Stack Underflow.
                 3.  Peep: The peep operation means finding a specific element from the stack. It returns the ith element from the stack.
                 4.  Change: The change operation involves interchanging of any two stack elements.
                   (Peep and Change operations are beyond the scope of the syllabus and are not discussed further.)

                 Let us explain the push and pop operations with an example given below.
                 A stack array stk[] of maximum size 3 is defined in memory and top is initialised to 0.

                                         Operation                                   Content in stack

                                                                                                  top is at 1
                       Step 1:  We push 15 into the stack.
                                                                         15      stk[0]

                                                                                                  top is at 2
                       Step 2:  We push 22 into the stack.
                                                                         22      stk[1]
                                                                         15      stk[0]

                       Step 3:  We push 37 into the stack.                                        top is at 3

                                                                         37      stk[2]

                                                                         22      stk[1]
                                                                         15      stk[0]
                       Step 4:  We try to push 59 into the stack.   As  top  =  3,  i.e.,  the  maximum  size  has  reached;  Stack
                                                                    Overflow message will be displayed.
                 Now let us pop the elements one by one.

                                         Operation                                   Content in stack

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

                                                                         22         stk[1]
                                                                         15         stk[0]





                                                                                                                       495
                                                                                                       Data Structures  495
   492   493   494   495   496   497   498   499   500   501   502