Pai-Ch07-SQlinkedlist.ppt

March 26, 2018 | Author: anon | Category: Queue (Abstract Data Type), Data Management, Computer Data, Computing, Technology


Comments



Description

1@ McGraw-Hill Education Copyrighted Material -Additional resource material supplied with the book Data Structures and Algorithms : Concepts, Techniques and Applications authored by G.A.V. Pai and published by Tata McGraw Hill. This resource material is for Instructor's use only. PROPRIETARY MATERIAL. © 2008 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission. Linked Stacks And Linked Queues (Chapter 7) . 3 Outline •Introduction Linked Stacks Linked Queues •Operations on Linked Stacks and Linked Queues Linked Stack Operations Linked Queue Operations Algorithms for Push/Pop operations on a Linked Stack Algorithms for Insert/Delete operations in a Linked Queue •Dynamic memory management and Linked Stacks •Implementation of Linked Representations •Applications Balancing Symbols Polynomial Representation . 4 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. 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.. . FRONT and REAR. 5 c Top a b a b front (a) Conventional representation of stack a b rear (d) Conventional representation of a queue c a Top front (b) Sequential representation of stack c b c rear (e) Sequential representation of a queue rear front Top c b a (c) Linked representation of stack a b c (f) Linked representation of a queue . .1: Push item ITEM into a linked stack S with top pointer TOP procedure 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.6 Push operation on a Linked stack Algorithm 7. end POP_LINKSTACK. 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) . .7 Pop operation on a Linked stack Algorithm 7.2: Pop from a linked stack S and output the element through ITEM procedure POP_LINKSTACK(TOP. REAR.8 Insert operation on a Linked Queue Algorithm 7. LINK(X)= NIL. /* Node with ITEM is ready to be inserted into Q */ if (FRONT = 0) then FRONT = REAR = X. REAR = X } end INSERT_LINKQUEUE. DATA(X)= ITEM.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. /* If Q is empty then ITEM is the first element in the queue Q */ else {LINK(REAR) = X. .ITEM) Call GETNODE(X). 4: Delete element from the linked queue Q through ITEM with FRONT and REAR as the front and rear pointers procedure DELETE_LINKQUEUE (FRONT. FRONT = LINK (TEMP). /* return the node TEMP to the free pool */ end DELETE_LINKQUEUE. . } call RETURN (TEMP).ITEM) if (FRONT = 0) then call LINKQUEUE_EMPTY. ITEM = DATA (TEMP).9 Delete operation on a Linked Queue Algorithm 7. /* Test condition to avoid deletion in an empty queue */ else {TEMP = FRONT. .  The automatic recycling of dynamically allocated memory is also known as Garbage collection. 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).10 Dynamic Memory Management And Linked Stacks  Dynamic memory management deals with methods of allocating storage and recycling unused space for future use.  If the memory storage pool is thought of as a repository of nodes. 11 Applications  Balancing symbols  Polynomial representation . 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. /* 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.12 Algorithm 7. while not end_of_string(E) read character. } endwhile if stack not empty then ERROR(). if the character popped is not the matching symbol then ERROR(). . end BALANCE_EXPR. 13 Linked list representation of a polynomial COEFF EXP LINK 9 6 -2 3 2 4 4 COEFF: Coefficient of the term EXP: Exponent of the variable (a) Node structure 0 (b) Linked list representation of the polynomial 9x6 -2x4 + 3x2 + 4. .
Copyright © 2024 DOKUMEN.SITE Inc.