Page 81 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 81

Advanced Data Structure and Algorithms




                    Notes                   }
                                            /* pops off the items from the stack */
                                            int pop( )
                                            {
                                            int data ;
                                            if ( pos == -1 )
                                            {
                                            printf ( “\nStack is empty” ) ;
                                            return ( -1 ) ;
                                            }
                                            else
                                            {
                                            data = stack[ pos ] ;
                                            pos-- ;
                                            return ( data ) ;
                                            }
                                            }
                                       2.   In this program stack is implemented and maintained using linked list. This
                                            implementation is more sophisticated compared to the one that uses an array, the
                                            added advantage being we can push as many elements as we want. A new node
                                            is created by push( ) (using malloc( )) every time an element is pushed in the stack.
                                            Each node in the linked list contains two members, data holding the data and link
                                            holding the address of the next node. The end of the stack is identified by the node

                                            holding NULL in its link part. The pop( ) function pops out the last element inserted
                                            in the stack and frees the memory allocated to hold it. stack_display( ) displays all
                                            the elements that stack holds. count( ) counts and returns the number of elements
                                            present in the stack.
                                            #include “alloc.h”
                                            struct node
                                            {
                                            int data ;
                                            struct node *link ;
                                            } ;
                                            push ( struct node **, int ) ;
                                            pop ( struct node ** ) ;
                                            main( )
                                            {
                                            struct node *top ;  /* top will always point to top of a stack */
                                            int item;
                                            top = NULL ;  /* empty stack */
                                            push ( &top, 11 ) ;
                                            push ( &top, 12 ) ;
                                            push ( &top, 13 ) ;
                                            push ( &top, 14 ) ;
                                            push ( &top, 15 ) ;



          76                               LOVELY PROFESSIONAL UNIVERSITY
   76   77   78   79   80   81   82   83   84   85   86