This preview shows pages 1–8. Sign up to view the full content.
More Stack Applications
•
Parentheses Matching
•
Maze Router
•
Infix to Postfix
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Parentheses Matching
•
(
(
(
a+b
)
*c+de
)
/
(
f+g
)

(
h+j
)
*
(
kl
)
)
/(mn)
–
Output pairs
(u,v)
such that the left parenthesis at
position
u
is matched with the right parenthesis at
v
.
•
(2,6)
(1,13)
(15,19)
(21,25)
(27,31)
(0,32)
(34,38)
•
(
a+b
)
)
*(
(
c+d
)
–
(0,4)
–
right parenthesis at 5 has no matching left parenthesis
–
(8,12)
–
left parenthesis at 7 has no matching right parenthesis
Parentheses Matching
•
scan expression from left to right
•
when a left parenthesis is encountered, add its
position to the stack
•
when a right parenthesis is encountered, remove
matching position from stack
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Example
•
(
(
(
a+b
)
*c+de
)
/
(
f+g
)

(
h+j
)
*
(
kl
)
)
/(mn)
0
1
2
Example
•
(
(
(
a+b
)
*c+de
)
/
(
f+g
)

(
h+j
)
*
(
kl
)
)
/(mn)
0
1
(2,6)
(1,13)
15
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Example
•
(
(
(
a+b
)
*c+de
)
/
(
f+g
)

(
h+j
)
*
(
kl
)
)
/(mn)
0
1
(2,6)
(1,13)
(15,19)
21
Example
•
(
(
(
a+b
)
*c+de
)
/
(
f+g
)

(
h+j
)
*
(
kl
)
)
/(mn)
0
1
(2,6)
(1,13)
(15,19)
(21,25)
27
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 04/13/2011 for the course DATA STRUC 101 taught by Professor Hansanyong during the Spring '11 term at 한동대학교.
 Spring '11
 HanSanyong

Click to edit the document details