• 沒有找到結果。

March31,2020 Hsuan-TienLin Stack

N/A
N/A
Protected

Academic year: 2022

Share "March31,2020 Hsuan-TienLin Stack"

Copied!
20
0
0

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

全文

(1)

Stack

Hsuan-Tien Lin

Dept. of CSIE, NTU

March 31, 2020

(2)

Intuition

(3)

Intuition

Stack

(4)

Intuition

Stack: Last-In-First-Out (LIFO) Stack

(constant-time) operations:

• insertTop(data), often called push(data)

• removeTop(), often called pop()

• getTop(), often called peek()

—LIFO: 擠電梯 , 洗盤子

very restricted data structure, but important for computers

—will discuss some cases later

H.-T. Lin (NTU CSIE) Stack 3/19

(5)

Intuition

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?

(6)

Intuition

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 (will see some)

H.-T. Lin (NTU CSIE) Stack 5/19

(7)

Intuition

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: security attack?

(8)

Implementation

(9)

Implementation

Stacks Implemented on Array

usually: (growable) consecutive array and push/pop at end-of-array

(10)

Implementation

Stacks Implemented on Linked List

usually: singly linked list and push/pop at head

H.-T. Lin (NTU CSIE) Stack 9/19

(11)

Implementation

Stack in STL

1 s t a c k < i n t , v e c t o r <i n t> > s_on_array ;

2 s t a c k < i n t , l i s t <i n t> > s_on_array ; implemented as containeradapter

(12)

Application: Expression Evaluation

(13)

Application: Expression Evaluation

Stack for Expression Evaluation

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

(14)

Application: Expression Evaluation

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

H.-T. Lin (NTU CSIE) Stack 13/19

(15)

Application: Expression Evaluation

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!

(16)

Application: Expression Evaluation

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

H.-T. Lin (NTU CSIE) Stack 15/19

(17)

Application: Expression Parsing

(18)

Application: Expression Parsing

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!

H.-T. Lin (NTU CSIE) Stack 17/19

(19)

Application: Expression Parsing

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

(20)

Application: Expression Parsing

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

H.-T. Lin (NTU CSIE) Stack 19/19

參考文獻

相關文件

(A) The PC has the TCP/IP protocol stack correctly installed (B) The PC has connectivity with a local host (C) The Pc has a default gateway correctly configured (D) The Pc

The stack H ss ξ (C, D; m, e, α) was constructed in section 2.3.. It is a smooth orbifold surface containing a unique orbifold point above each ℘ i,j.. An inverse morphism can

To have a good mathematical theory of D-string world-sheet instantons, we want our moduli stack of stable morphisms from Azumaya nodal curves with a fundamental module to Y to

This kind of algorithm has also been a powerful tool for solving many other optimization problems, including symmetric cone complementarity problems [15, 16, 20–22], symmetric

• 如果把 queue 換成

Return address Local variables Previous frame pointer?. Return address

Return address Local variables Previous frame pointer. Return

Stack 實作一 Stack 實作二 Stack實作三.