Pai-Ch07-SQlinkedlist.ppt

download Pai-Ch07-SQlinkedlist.ppt

of 13

Transcript of Pai-Ch07-SQlinkedlist.ppt

  • Linked Stacks And Linked Queues(Chapter 7)

    *

    Introduction Linked StacksLinked QueuesOperations on Linked Stacks and Linked QueuesLinked Stack OperationsLinked Queue OperationsAlgorithms for Push/Pop operations on a Linked StackAlgorithms for Insert/Delete operations in a Linked QueueDynamic memory management and Linked StacksImplementation of Linked Representations ApplicationsBalancing SymbolsPolynomial RepresentationOutline

    *

    A linked stack is a linear list of elements commonly implemented as a singly linked list whose start pointer performs the role of the top pointer of a stack

    A linked queue is also a linear list of elements commonly implemented as a singly linked list but with two pointers viz., FRONT and REAR. The start pointer of the singly linked list plays the role of FRONT while the pointer to the last node is set to play the role of REAR.

    *

    *

    Algorithm 7.1: Push item ITEM into a linked stack S with top pointer TOPprocedure PUSH_LINKSTACK (TOP, ITEM)/* Insert ITEM into stack */Call GETNODE(X)DATA(X) = ITEM /*frame node for ITEM */LINK(X) = TOP /* insert node X into stack */TOP = X /* reset TOP pointer */ end PUSH_LINKSTACK.

    Push operation on a Linked stack

    *

    Algorithm 7.2: Pop from a linked stack S and output the element through ITEMprocedure POP_LINKSTACK(TOP, ITEM) /* pop element from stack and set ITEM to the element */if (TOP = 0) then call LINKSTACK_EMPTY /* check if linked stack is empty */else { TEMP = TOP ITEM = DATA(TOP) TOP = LINK(TOP) }call RETURN(TEMP) ;end POP_LINKSTACK.

    Pop operation on a Linked stack

    *

    Algorithm 7.3: Push item ITEM into a linear queue Q with FRONT and REAR as the front and rear pointer to the queue

    procedure INSERT_LINKQUEUE(FRONT,REAR,ITEM)Call GETNODE(X);DATA(X)= ITEM;LINK(X)= NIL; /* Node with ITEM is ready to be inserted into Q */ if (FRONT = 0) then FRONT = REAR = X;/* If Q is empty then ITEM is the first element in the queue Q */ else {LINK(REAR) = X; REAR = X }end INSERT_LINKQUEUE.Insert operation on a Linked Queue

    *

    Delete operation on a Linked QueueAlgorithm 7.4: Delete element from the linked queue Q through ITEM with FRONT and REAR as the front and rear pointers

    procedure DELETE_LINKQUEUE (FRONT,ITEM)if (FRONT = 0) then call LINKQUEUE_EMPTY; /* Test condition to avoid deletion in an empty queue */else {TEMP = FRONT; ITEM = DATA (TEMP); FRONT = LINK (TEMP);}call RETURN (TEMP); /* return the node TEMP to the free pool */end DELETE_LINKQUEUE.

    *

    Dynamic Memory Management And Linked StacksDynamic memory management deals with methods of allocating storage and recycling unused space for future use.

    The automatic recycling of dynamically allocated memory is also known as Garbage collection.

    If the memory storage pool is thought of as a repository of nodes, then dynamic memory management primarily revolves round the two actions of allocating nodes (for use by the application) and liberating nodes (after their release by the application).

    *

    Applications Balancing symbols

    Polynomial representation

    *

    Algorithm 7.5: To check for the balancing of parentheses in a string procedure BALANCE_ EXPR(E) /*E is the expression padded with a $ to indicate end of input*/clear stack;while not end_of_string(E) read character; /* read a character from string E*/ if the character is an open symbol then push character in to stack; if the character is a close symbol then if stack is empty then ERROR () else {pop the stack; if the character popped is not the matching symbol then ERROR(); }endwhile if stack not empty then ERROR();end BALANCE_EXPR.

    *

    Linked list representation of a polynomial