Вы находитесь на странице: 1из 13

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)

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

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.

Top

b
a

front

(a) Conventional
representation of stack
a

rear

(d) Conventional
representation of a queue

Top

front

(b) Sequential
representation of stack

rear

(e) Sequential representation


of a queue
rear
front

Top
c

(c) Linked
representation of stack

(f) Linked representation of a


queue

Push operation on a Linked stack


Algorithm 7.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.

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, 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.

Insert operation on a Linked Queue


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.

Delete operation on a Linked Queue


Algorithm 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.

10

Dynamic Memory Management And Linked Stacks


Dynamic 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).

11

Applications

Balancing symbols

Polynomial representation

12

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.

13

Linked list representation of a


polynomial

COEFF

EXP

LINK

-2

COEFF: Coefficient of the term


EXP: Exponent of the variable
(a) Node structure

(b) Linked list representation of the


polynomial 9x6 -2x4 + 3x2 + 4.

Вам также может понравиться