Page 146 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 146

Unit 9: Stacks




                                                                                                Notes
                                Figure 9.9: Top= 5 and Size=6, Stack Full



















          Source: http://newtonapples.com/operations-on-stack-c-language/
          If the stack is not full, line 9 actually inserts the elements passed as argument in the array stack[]
          and it will be inserted at the same location where the top variable will be having value. Line 10
          displays the element that is present in the top of the stack.

                                   Figure 9.10: Stack[top] = Element

















          Source: http://newtonapples.com/operations-on-stack-c-language/

          9.2.2 Implementation of Pop Operation

          The pop operation can be shown as a function pop() which actually helps to remove an element
          from the stack.
          pop()
          {
            if(top== -1) // Checking if stack is empty
            {
              printf(“STACK EMPTY, no elements to delete”);
              exit(0);
            }
            int ele;
            ele=stack[top]; //ele holds the top most element that is to deleted
            printf(“The element deleted is %d”, stack[top]);




                                           LOVELY PROFESSIONAL UNIVERSITY                                   139
   141   142   143   144   145   146   147   148   149   150   151