• 沒有找到結果。

April7,2015 Hsuan-TienLin Stacks,Queues,Deques

N/A
N/A
Protected

Academic year: 2022

Share "April7,2015 Hsuan-TienLin Stacks,Queues,Deques"

Copied!
13
0
0

加載中.... (立即查看全文)

全文

(1)

Stacks, Queues, Deques

Hsuan-Tien Lin

Dept. of CSIE, NTU

April 7, 2015

(2)

A Simple Application: Parentheses Balancing

in C, the following characters show up in pairs: (), [], {}, ""

good: {xxx(xxxxxx)xxxxx"xxxx"x}

bad: {xxx(xxxxxx}xxxxx"xxxx"x}

the LISP programming language

(append (pow (* (+ 3 5) 2) 4) 3) how can we check parentheses balancing?

(3)

Stack Solution to Parentheses Balancing

inner-most parentheses pair =⇒ top-most plate

’(’: 堆盤子上去 ; ’)’: 拿盤子下來 Parentheses Balancing Algorithm

for each c in the input do if c is a left character

push c to the stack else if c is a right character

pop d from the stack and check if match end if

end for

many more sophisticated use in compiler design

(4)

System Stack

recall: function call ⇔ 拿新的草稿紙來算

old (original) scrap paper: temporarily not used, 可以壓在下面 System Stack: 一疊草稿紙 , each paper (stack frame) contains

return address: where to return to the previous scrap paper local variables (including parameters): to be used for calculating within this function

previous frame pointer: to be used when escaping from this function

some related issues: stack overflow? security attack?

(5)

Stacks Implemented on Array (5.1.4)

Reading Assignment

be sure to go ask the TAs or me if you are still confused

(6)

Stacks Implemented on Linked List (5.1.5)

Reading Assignment

be sure to go ask the TAs or me if you are still confused

(7)

Stack for Expression Evaluation (Supplementary)

a/b − c + d ∗ e − a ∗ c precedence: {∗, /} first; {+, −} later steps

f = a/b g = f − c h = d ∗ e i = g + h j = a ∗ c

` =i − j

Postfix Notation

same operand order, but put “operator”afterneeded operands

—can “operate” immediately when seeing operator

—no need to look beyond for precedence

(8)

Postfix from Infix (Usual) Notation

infix:

3 / 4 − 5 + 6 ∗ 7 − 8 ∗ 9

parenthesize:

3 / 4 − 5 + 6 ∗ 7 − 8 ∗ 9

for every triple in parentheses, switch orders

remove parentheses

difficult to parenthesize efficiently

(9)

Evaluate Postfix Expressions

34/5 − 67 ∗ +89 ∗ −

how to evaluate? left-to-right, “operate” when see operator 3, 4, / ⇒ 0.75

0.75, 5, - ⇒ -4.25

-4.25, 6, 7, * ⇒ -4.25, 42 (note: -4.25 stored for latter use) -4.25, 42, + ⇒ 37.75

37.75, 8, 9, * ⇒ 37.75, 72 (note: 37.75 stored for latter use) 37.75, 72, - ⇒ ...

stored where?

stackso closest operands will be considered first!

(10)

Stack Solution to Postfix Evaluation

Postfix Evaluation

for each token in the input do if token is a number

push token to the stack else if token is an operator

sequentially pop operands at−1, · · · ,a0from the stack push token(a0,a1,at−1)to the stack

end if end for

return the top of stack

matches closely with the definition of postfix notation

(11)

One-Pass Algorithm for Infix to Postfix

infix ⇒ postfix efficiently?

at/, not sure of what to do (need later operands) sostore a/b − c + d ∗ e − a ∗ c

at-, know that a / b can be a b / because-is of lower precedence a/b−c + d ∗ e − a ∗ c

at+, know that ? - c can be ? c - because+is of same precedence but {-,+} is left-associative

a/b − c+d ∗ e − a ∗ c

at*, not sure of what to do (need later operands) sostore a/b − c + d∗e − a ∗ c

stored where?stackso closest operators will be considered first!

(12)

Stack Solution to Infix-Postfix Translation

for each token in the input do if token is a number

output token

else if token is an operator

while top of stack is of higher (or same) precedence do pop and output top of stack

end while

push token to the stack end if

end for

here: infix to postfix with operator stack

—closest operators will be considered first recall: postfix evaluation with operand stack

—closest operands will be considered first

mixing the two algorithms (say, use two stacks): simple calculator

(13)

Some More Hints on Infix-Postfix Translation

for each token in the input do if token is a number

output token

else if token is an operator

while top of stack is of higher (or same) precedence do pop and output top of stack

end while

push token to the stack end if

end for

for left associativity and binary operators

right associativity? same precedence needs to wait unary/trinary operator? same

parentheses? higest priority

at ’(’, cannot pop anything from stack

—like seeing ’*’ while having ’+’ on the stack at ’)’, can pop until ’(’ —like parentheses matching

參考文獻

相關文件

If k = 1, the compactness condition can be replaced by (M, g) being com- plete, by the classical Obata’s theorem..

• old (original) scrap paper: temporarily not used, 可以壓在下面 System Stack: 一疊草稿紙 , each paper (stack frame) contains. • return address: where to return to the

• old (original) scrap paper: temporarily not used, 可 以壓在下面 System Stack: 一疊草稿紙 , each paper (stack frame) contains. • return address: where to return to

inner-most parentheses pair =⇒ top-most plate. ’(’: 堆 盤子上去 ;

In Section 3, the shift and scale argument from [2] is applied to show how each quantitative Landis theorem follows from the corresponding order-of-vanishing estimate.. A number

Now, nearly all of the current flows through wire S since it has a much lower resistance than the light bulb. The light bulb does not glow because the current flowing through it

Continuation of Suspension of Face-to-face Classes for Schools in Hong Kong Since the latest epidemic situation is still very severe, the Education Bureau (EDB)

In the case of nonlinear complementarity problems, the three classes of NCP-functions used in this paper can be exploited to design other solution methods such as merit