chapter 3 stacks and queues the stack abstract data type the queue abstract data type a mazing...

40
Chapter 3 Stacks and Chapter 3 Stacks and Queues Queues The Stack Abstract Data Type The Stack Abstract Data Type The Queue Abstract Data Type The Queue Abstract Data Type A Mazing Problem A Mazing Problem Evaluation of Expressions Evaluation of Expressions

Upload: kellie-carroll

Post on 23-Dec-2015

231 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

Chapter 3 Stacks and QueuesChapter 3 Stacks and Queues

The Stack Abstract Data TypeThe Stack Abstract Data Type The Queue Abstract Data TypeThe Queue Abstract Data Type A Mazing ProblemA Mazing Problem Evaluation of ExpressionsEvaluation of Expressions

Page 2: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.1 The stack ADT (1/5)3.1 The stack ADT (1/5) A stack is an ordered list in which insertions and

deletions are made at one end called the top. If we add the elements If we add the elements AA, , BB, , CC, , DD, , EE to the to the

stack, in that order, then stack, in that order, then EE is the first element we is the first element we delete from the stackdelete from the stack

A stack is also known as a A stack is also known as a Last-In-First-OutLast-In-First-Out ((LIFOLIFO)) list. list.

Page 3: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

old frame pointer

return address

fp

main

fp

al

(a) (b) *Figure 3.2: System stack after function call a1 (p.103)

old frame pointer

return address

local variablesold frame pointer

return address

An application of stack: stack frame of function call

(activation record)

stack frame of invoking function

system stack before a1 is invoked system stack after a1 is invoked

fp: a pointer to current stack frame

3.1 The stack ADT (2/5)3.1 The stack ADT (2/5)

Page 4: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.1 The stack ADT (3/5)3.1 The stack ADT (3/5) The ADT specification of the stack is shown in The ADT specification of the stack is shown in

Structure 3.1Structure 3.1

Page 5: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.1 The stack ADT (4/5)3.1 The stack ADT (4/5)

Implementation: using array

Page 6: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.1 The stack ADT (5/5)3.1 The stack ADT (5/5)

Page 7: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.2 The queue ADT (1/7)3.2 The queue ADT (1/7) A queue is an ordered list in which all insertion take

place one end, called the rear and all deletions take place at the opposite end, called the front

If we insert the elements If we insert the elements AA, , BB, , CC, , DD, , EE, in that , in that order, then order, then AA is the first element we delete from the is the first element we delete from the queuequeue

A stack is also known as a A stack is also known as a First-In-First-OutFirst-In-First-Out ( (FIFOFIFO)) listlist

Page 8: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.2 The queue ADT (2/7)3.2 The queue ADT (2/7) The ADT specification of the queue appears in The ADT specification of the queue appears in

Structure 3.2Structure 3.2

Page 9: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.2 The queue ADT (3/7)3.2 The queue ADT (3/7)

Implementation 1: using a one dimensional array and two variables, front and rear

Page 10: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.2 The queue ADT (4/7)3.2 The queue ADT (4/7)

problem: there may be available space when IsFullQ is true i.e. movement is required.

Page 11: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.2 The queue ADT (5/7)3.2 The queue ADT (5/7) Example 3.2 [Example 3.2 [Job schedulingJob scheduling]:]:

Figure 3.5 illustrates how an operating system might pFigure 3.5 illustrates how an operating system might process jobs if it used a sequential representation for itrocess jobs if it used a sequential representation for its queue.s queue.

As jobs enter and leave the system, the queue gradually shift As jobs enter and leave the system, the queue gradually shift to right. to right.

In this case, In this case, queue_fullqueue_full should move the entire queue to the l should move the entire queue to the left so that the first element is again at eft so that the first element is again at queuequeue[0], [0], frontfront is at -1, is at -1, and and rearrear is correctly positioned. is correctly positioned.

Shifting an array is very time-consuming, Shifting an array is very time-consuming, queue_fullqueue_full has a wors has a worst case complexity of O(t case complexity of O(MAX_QUEUE_SIZEMAX_QUEUE_SIZE).).

Page 12: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.2 (6/7)3.2 (6/7) We can obtain a more efficient represeWe can obtain a more efficient represe

ntation if we regard the array ntation if we regard the array queuequeue[[MMAX_QUEUE_SIZEAX_QUEUE_SIZE] as ] as circularcircular..

Implementation 2: regard an array as a circular queuefront: one position

counterclockwise from the first element

rear: current end

Problem: one space is left when queue is full.

Page 13: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.2 (7/7)3.2 (7/7) Implementing Implementing addqaddq and and deleteqdeleteq for a circular queu for a circular queu

e is slightly more difficult since we must assure that e is slightly more difficult since we must assure that a circular rotation occurs.a circular rotation occurs.

Page 14: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.3 A Mazing Problem (1/8)3.3 A Mazing Problem (1/8) Representation of the mazeRepresentation of the maze

The most obvious choice is a two dimensional arrayThe most obvious choice is a two dimensional array 00s the open paths and s the open paths and 11s the barrierss the barriers

Notice that not every position has eight neighbors.Notice that not every position has eight neighbors. To avoid checking for these border To avoid checking for these border

conditions we can surround the maze conditions we can surround the maze by a border of ones. Thus an by a border of ones. Thus an mmpp maze will require an (maze will require an (mm+2) +2) ( (pp+2) +2) arrayarray

The entrance is at The entrance is at position [1][1] and the exit at [position [1][1] and the exit at [mm][][pp]]

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1

1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1

1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1

1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1

1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1

1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1

1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1

1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1

1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1

1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1

1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

entrance

exit

Page 15: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.3 A Mazing Problem (2/8)3.3 A Mazing Problem (2/8) If X marks the spot of our current location, mazeIf X marks the spot of our current location, maze

[[rowrow][][colcol], then Figure 3.9 shows the possible m], then Figure 3.9 shows the possible moves from this positionoves from this position

Page 16: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.3 A Mazing Problem (3/8)3.3 A Mazing Problem (3/8) A possible implementation:A possible implementation:

Predefinition: the possible directions to move in an array,Predefinition: the possible directions to move in an array, movemove, as in Figure 3.10., as in Figure 3.10. Obtained from Figure 3.9Obtained from Figure 3.9

typedef struct {

short int vert;

short int horiz;

} offsets;

offsets move[8]; /*array of moves for each direction*/

If we are at position, maze[maze[rowrow][][colcol], ], and we wish to find the position of the next move, maze[maze[rowrow][][colcol], ], we set:

next_row = row + move[dir].vert;

next_col = col + move[dir].horiz;

Page 17: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.3 A Mazing Problem (4/8)3.3 A Mazing Problem (4/8) Initial attempt at a maze traversal algorithmInitial attempt at a maze traversal algorithm

maintain a second two-dimensional array, maintain a second two-dimensional array, markmark, to record , to record the maze positions the maze positions already checkedalready checked

use stack to keep pass history

#define MAX_STACK_SIZE 100 /*maximum stack size*/typedef struct {

short int row;short int col;short int dir;

} element;element stack[MAX_STACK_SIZE];

Page 18: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1

1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1

1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1

1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1

1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1

1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1

1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1

1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1

1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1

1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1

1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

maze[1][1]: entrance

maze[15][11]: exit

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █

R 1 C 1 D 1

R 1 C 1 D 1R: rowC: colD: dir

Initially set mark[1][1]=1

R 1 C 1 D 3

R 2 C 2 D 1

R 1 C 3 D 2

R 1 C 4 D 2

R 1 C 5 D 5

R 2 C 4 D 3

R 3 C 5 D 2

R 3 C 6 D 1

R 2 C 7 D 1

R 1 C 8 D 2

R 1 C 9 D 2

R1 C10 D 3

R2 C11 D 2

R2 C12 D 3

R3 C13 D 3

R4 C14 D 2

Pop out

R3 C13 D 6

R3 C12 D 5

Page 19: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.3 A Mazing Problem (6/8)3.3 A Mazing Problem (6/8) Review of add and delete to a stackReview of add and delete to a stack

Page 20: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.3 A Mazing Pr3.3 A Mazing Problem (7/7)oblem (7/7)

Analysis:Analysis: The worst case of comThe worst case of com

puting time of puting time of pathpath is is O(O(mpmp), where ), where mm and and pp are the number of roware the number of rows and columns of the s and columns of the maze maze respectivelyrespectively

(1,1)

(m,p)(m+2)*(p+2)

0 7 N 16 W E 2 5 S 3 4

Page 21: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.3 A Mazing Problem (8/8)3.3 A Mazing Problem (8/8)

The size of a stackThe size of a stack0 0 0 0 0 11 1 1 1 1 01 0 0 0 0 10 1 1 1 1 11 0 0 0 0 11 1 1 1 1 01 0 0 0 0 10 1 1 1 1 11 0 0 0 0 0

*Figure 3.11: Simple maze with a long path (p.116)

m*p mppmmp /2/or ,/2/

Page 22: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (1/14)(1/14)

3.4.1 Introduction3.4.1 Introduction The representation and evaluation of expressions is of greThe representation and evaluation of expressions is of gre

at interest to computer scientists.at interest to computer scientists. ((((rearrear++11====frontfront)) || || ((((rearrear====MAX_QUEUE_SIZEMAX_QUEUE_SIZE--11) ) &&&& !!frontfront)) (3.1))) (3.1) xx == aa//bb -- cc++dd**ee -- aa**cc (3.(3.

2)2)

If we examine expressions (3.1), If we examine expressions (3.1), we notice that they contains:we notice that they contains: operators: ==, +, -, ||, &&, !operators: ==, +, -, ||, &&, ! operands: operands: rear, front, MAX_QUEUE_SIZErear, front, MAX_QUEUE_SIZE parentheses: ( )parentheses: ( )

Page 23: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (2/14)(2/14)

Understanding the meaning of these or any other Understanding the meaning of these or any other expressions and statementsexpressions and statements assume assume a = a = 44, b = c = , b = c = 22, d = e = , d = e = 33 in the statement (3.2), in the statement (3.2),

finding out the value of finding out the value of x = x = a/b – c + d*e - a*ca/b – c + d*e - a*c Interpretation 1: ((4/2)-2)+(3*3)-(4*2) = 0+8+9 = 1Interpretation 1: ((4/2)-2)+(3*3)-(4*2) = 0+8+9 = 1 Interpretation 2: (4/(2-2+3))*(3-4)*2 = (4/3)*(-1)*2 = -2.66666Interpretation 2: (4/(2-2+3))*(3-4)*2 = (4/3)*(-1)*2 = -2.66666……

we would have written (3.2) differently by using parentwe would have written (3.2) differently by using parentheses to change the order of evaluation:heses to change the order of evaluation: x x = ((= ((aa/(/(b b - - cc++dd))*())*(e e - - aa)*)*cc (3.3)(3.3)

How to generate the machine instructions corresHow to generate the machine instructions corresponding to a given expression?ponding to a given expression?

precedence rule + associative ruleprecedence rule + associative rule

Page 24: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 (3/14)3.4 (3/14)

Precedence hierarchy and associative for C

Page 25: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (4/14)(4/14) Evaluating postfix expressionsEvaluating postfix expressions

The standard way of writing expressions is known as The standard way of writing expressions is known as infix notationinfix notation binary operator in-between its two operandsbinary operator in-between its two operands

Infix notation is not the one used by compilers to Infix notation is not the one used by compilers to evaluate expressionsevaluate expressions

Instead compilers typically use a parenthesis-free Instead compilers typically use a parenthesis-free notation referred to as postfixnotation referred to as postfix

Postfix: no parentheses, no precedence

Page 26: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (5/14)(5/14)

Evaluating postfix expressions is much simpler Evaluating postfix expressions is much simpler than the evaluation of infix expressions:than the evaluation of infix expressions: There are no parentheses to consider.There are no parentheses to consider. To evaluate an expression we make a single left-to-To evaluate an expression we make a single left-to-

right scan of it.right scan of it. We can evaluate We can evaluate

an expression an expression easily by using easily by using a stacka stack

Figure 3.14 shows this Figure 3.14 shows this processing when the processing when the input is nine character input is nine character string 6 2/3-4 2*+string 6 2/3-4 2*+

Page 27: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions (6/14)3.4 Evaluation of Expressions (6/14)

RepresentationRepresentation We now consider the representation of both the stack and We now consider the representation of both the stack and

the expressionthe expression

Page 28: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (7/14)(7/14)

Get TokenGet Token

Page 29: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 (8/14)3.4 (8/14)

Evaluation of Evaluation of Postfix ExpressionPostfix Expression

Page 30: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (9/14)(9/14)

string: 6 2/3-4 2*+string: 6 2/3-4 2*+

STACK

0

1

2

we make a single left-to-right we make a single left-to-right scan of itscan of it

top

6 2 / 3 - 4 2 * +6 2 / 3 - 4 2 * +

not an operator, put into the stack

6

now, top must +1

not an operator, put into the stack 2

is an operator, pop two elements of the stack

now, top must -2

add the string with the operator

6/2

not an operator, put into the stack 3 is an operator, pop two elements of the stack

6/2-3

not an operator, put into the stack 4

not an operator, put into the stack

2 is an operator, pop two elements of the stack

4*2 is an operator, pop two elements of the stack

6/2-3+4*2

end of string, pop the stack and get answer

now, top must -1

6 / 2 - 3 + 4 * 2 6 / 2 - 3 + 4 * 2

the answer isthe answer is

Page 31: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

We can describe am algorithm for producing a postfix expression from an infix one as follows

(1) Fully parenthesize expressiona / b - c + d * e - a * c

((((a / b) - c) + (d * e)) - (a * c))(2) All operators replace their corresponding right

parentheses ((((a / b) - c) + (d * e)) - (a * c))

(3)Delete all parentheses a b / c - d e * + a c * -

The order of operands is the same in infix and postfix

/ -*

+*

-

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (10/14)(10/14)

two passes

Page 32: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (11/14)(11/14) Example 3.3 [Example 3.3 [Simple expressionSimple expression]:]: Simple expression a+b*c, which yield Simple expression a+b*c, which yield

s abc*+ in postfix.s abc*+ in postfix.

Example 3.5 [ParenthesizedExample 3.5 [Parenthesized expression expression]:]: The expression a*(b+c)*d, w The expression a*(b+c)*d, which yields abc+*d* hich yields abc+*d* in postfixin postfix

match )

icp

12

13

0

isp

121313

icp

13

20

12

1913

0

0

isp

13

121213131313

0

Page 33: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (12/14)(12/14) Algorithm to convert from infix to postfix

Assumptions: operators: (, ), +, -, *, /, % operands: single digit integer or variable of one character

1. Operands are taken out immediately2. Operators are taken out of the stack as long as their in-stack precede

nce (isp) is higher than or equal to the incoming precedence (icp) of the new operator

3. ‘(‘ has low isp, and high icpop ( ) + - * / %

eosIsp 0 19 12 12 13 13 13

0Icp 20 19 12 12 13 13 13

0

precedence stack[MAX_STACK_SIZE];/* isp and icp arrays -- index is value of precedence lparen, rparen, plus,

minus, times, divide, mod, eos */static int isp [ ] = {0, 19, 12, 12, 13, 13, 13, 0};static int icp [ ] = {20, 19, 12, 12, 13, 13, 13, 0};

Page 34: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (13/14)(13/14)

a * ( b + c ) / da * ( b + c ) / d

0

1

2

stacktop

output

aa * * bb ++cc dd //

operand, print out

operator

*

now, top must +1

push into the stack

operator

the isp of “( “ is 0 and the icp of “* “ is 13the isp of “/ “ is 13 and the icp of “* “ is 13(

operand, print out

operator

the isp of “+“ is 12 and the icp of “( “ is 20

+

operand, print out

operator

operator “)”, pop and print out until “(”

now, top must - 1

operator

pop the stack and printout

operand, print out

eos

/

Page 35: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (14/14)(14/14)

Complexity: Complexity: ((nn)) The total time spent heThe total time spent he

re is re is ((nn)) as the numb as the number of tokens that get ster of tokens that get stacked and unstacked iacked and unstacked is linear in s linear in nn

where where nn is the number is the number of tokens in the expresof tokens in the expressionsion

Page 36: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.5 MULTIPLE STACKS AND 3.5 MULTIPLE STACKS AND QUEUE (1/5)QUEUE (1/5)

Two stacks

m[0], m[1], …, m[n-2], m[n-1]

bottommost bottommost stack 1 stack 2 More than two stacks (n) memory is divided into n equal segments boundary[stack_no] 0 stack_no < MAX_STACKS top[stack_no] 0 stack_no < MAX_STACKS

Page 37: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.5 MULTIPLE STACKS AND 3.5 MULTIPLE STACKS AND QUEUE (2/5)QUEUE (2/5)

boundary[ 0] boundary[1] boundary[ 2] boundary[n]top[ 0] top[ 1] top[ 2]

All stacks are empty and divided into roughly equal segments.

*Figure 3.18: Initial configuration for n stacks in memory [m]. (p.129)

0 1 [ m/n ] 2[ m/n ] m-1

Initially, boundary[i]=top[i].

Page 38: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.5 MULTIPLE STACKS AND 3.5 MULTIPLE STACKS AND QUEUE (3/5)QUEUE (3/5)

aa*(p.128)*(p.128)

#define MEMORY_SIZE 100 /* size of memory */#define MEMORY_SIZE 100 /* size of memory */

#define MAX_STACK_SIZE 100 /* max number of stacks plus 1 */#define MAX_STACK_SIZE 100 /* max number of stacks plus 1 */ /* global memory declaration */ /* global memory declaration */

element memory [MEMORY_SIZE];element memory [MEMORY_SIZE];

int top [MAX_STACKS];int top [MAX_STACKS];

int boundary [MAX_STACKS];int boundary [MAX_STACKS];

int n; /* number of stacks entered by the user */int n; /* number of stacks entered by the user */

*(p.129)*(p.129) To divide the array into roughly equal segments :To divide the array into roughly equal segments :

top[0] = boundary[0] = -1;top[0] = boundary[0] = -1;

for (i = 1; i < n; i++)for (i = 1; i < n; i++)

top [i] =boundary [i] =(MEMORY_SIZE/n)*i;top [i] =boundary [i] =(MEMORY_SIZE/n)*i;

boundary [n] = MEMORY_SIZE-1;boundary [n] = MEMORY_SIZE-1;

Page 39: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.5 MULTIPLE STACKS AND 3.5 MULTIPLE STACKS AND QUEUE (4/5)QUEUE (4/5)

*Program 3.12:*Program 3.12:Add an item to the stack Add an item to the stack stack-no stack-no (p.129)(p.129)

void add (int i, element item) {void add (int i, element item) {

/* add an item to the ith stack *//* add an item to the ith stack */

if (top [i] == boundary [i+1])if (top [i] == boundary [i+1])

stack_full (i); stack_full (i); may have unused storage may have unused storage

memory [++top [i] ] = item;memory [++top [i] ] = item;

}}

*Program 3.13:*Program 3.13:Delete an Delete an itemitem from the stack from the stack stack-no stack-no (p.130)(p.130)

element delete (int i) {element delete (int i) {

/* remove top element from the ith stack */ /* remove top element from the ith stack */

if (top [i] == boundary [i])if (top [i] == boundary [i])

return stack_empty (i);return stack_empty (i);

return memory [ top [i]--];return memory [ top [i]--];

}}

Page 40: Chapter 3 Stacks and Queues  The Stack Abstract Data Type  The Queue Abstract Data Type  A Mazing Problem  Evaluation of Expressions

3.5 MULTIPLE STACKS AND 3.5 MULTIPLE STACKS AND QUEUE (5/5)QUEUE (5/5)

b[0] t[0] b[1] t[1] b[i] t[i] t[i+1] t[j] b[j+1] b[n] b[i+1] b[i+2]

b=boundary, t=top

*Figure 3.19: Configuration when stack i meets stack i+1, but the memory is not full (p.130)

Find j, stack_no < j < n such that top[j] < boundary[j+1] or, 0 j < stack_no

meet

往左或右找一個空間

(往左 )

(往右 )