Page 127 - DCAP407_DATA_STRUCTURE
P. 127

Data Structure



                          7.1.1   Stack Structure
                          The stack data structure is used to maintain records of a file in which the order among the records of file
                          is not important. Figure 7.1 displays the structure of a stack where stack is like a hollow cylinder with a
                          closed bottom end and an open top end. In the stack data structure, the records are added and deleted
                          at the top end. Last-In-First-Out (LIFO) principle is followed to retrieve records from the stack. The
                          records added last are accessed first. In Figure 7.1, the order of entry of the records in the stack is item 1,
                          2, 3, 4 and the order of retrieval of the records from the stack is item 4, 3, 2, 1.


                                                     Figure 7.1: Stack Data Structure



















                          7.2   Basic Operations of Stack

                          The basic operations of stack are to:
                          1.  Insert an element in the stack (Push operation)
                          2.  Delete an element from the stack (Pop operation)
                          7.2.1   Push Operation

                          The procedure to insert a new element to the stack is called push operation. The push operation adds an
                          element on the top of the stack. ‘Top’ refers to the element on the top of stack. Push makes the ‘Top’
                          point to the recently added element on the stack. After every push operation, the ’Top’ is incremented
                          by one. When the array is full, the status of stack is FULL and the condition is called stack overflow. No
                          element can be inserted when the stack is full. Figure 7.2 illustrates the push operation in the stack data
                          structure.
                                                         Figure 7.2: Push Operation in the
                                                              Stack Data Structure















                          In the figure 7.2, the stack has two elements 45 and 36. The ’Top’ points to ‘36’ as it is the last item in the
                          stack. Element 52 is added on the stack through push operation. The ‘Top’ points to ‘52’ after the push






                          120                     LOVELY PROFESSIONAL UNIVERSITY
   122   123   124   125   126   127   128   129   130   131   132