cs235102 data structures chapter 3 stacks and queues
Post on 22-Dec-2015
230 views
TRANSCRIPT
CS235102 CS235102 Data StructuresData Structures
Chapter 3 Stacks and QueuesChapter 3 Stacks and Queues
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 0s the open paths and 0s 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
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
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[If we are at position, maze[rowrow][][colcol], and we wish to find ], and we wish to find the position of the next move, maze[the position of the next move, maze[rowrow][][colcol], we set:], we set:
next_row = row + move[dir].vert;
next_col = col + move[dir].horiz;
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];
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
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
3.3 A Mazing Pr3.3 A Mazing Problem (7/8)oblem (7/8)
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
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/
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: ( )
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
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
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*+
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
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
We can describe an algorithm for producing a postfix expression from an infix one as follows
EX: Trans a / b - c + d * e - a * c To postfix(1) Fully parenthesize expression
a / 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
3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (11/14)(11/14)
Algorithm to convert from infix to postfix Assumptions:
operators: (, ), +, -, *, /, % operands: single digit integer or variable of one character
1. Scan string from left to right
2. Operands are taken out immediately
3. Operators are taken out of the stack as long as their in-stack precedence (isp) is higher than or equal to the incoming precedence (icp) of the new operator
4. ‘(‘ has low isp, and high icpop ( ) + - * / %
eos
Isp0 19 12 12 13 13 130
Icp20 19 12 12 13 13 130
3.4 Evaluation of Expressions 3.4 Evaluation of Expressions (12/14)(12/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
EX: a+b*c
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
/
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