Page 148 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 148

Unit 9: Stacks




          Self Assessment                                                                       Notes

          Fill in the blanks:
          3.   ............................ operation is used to insert the data elements in a stack.
          4.   ............................ function is used for deleting the elements from a stack.

          5.   Verifying the item placed at the top of the stack without removing it is called
               ............................ operation.
          6.   When the value of top is initialized to –1 initially, it means that the stack is ............................
               initially.
          7.   The stack empty condition is even known as ............................

          9.3 Representation of Stacks

          We can represent stacks in two ways:

               Sequential Representation of Stacks
               Linked list Representation of Stacks
          These are discussed below.

          9.3.1 Sequential Representation of Stacks

          A Stack contains an ordered list of elements and an array is also used to store ordered list of
          elements. Hence, it would be very easy to manage a stack using an array. However, the problem
          with an array is that we are required to declare the size of the array before using it in a program.
          Therefore, the size of stack would be fixed. Though an array and a stack are totally different data
          structures, an array can be used to store the elements of a stack.


               !
             Caution  We can declare the array with a maximum size large enough to manage a stack.
          Program given below implements a stack using an array.
          #include<stdio.h>
          int choice, stack[10], top, element;
          void menu();
          void push();
          void pop();
          void showelements();
          void main()
          { choice=element=1;
           top=0;
           menu();
          }
          void menu()
          {






                                           LOVELY PROFESSIONAL UNIVERSITY                                   141
   143   144   145   146   147   148   149   150   151   152   153