stack & queue

19
Stack & Queue Winter-Camp-2011 CSIE.NTNU [email protected]@Taiw an CC – 非非非非 – 非非非非

Upload: calla

Post on 14-Jan-2016

50 views

Category:

Documents


0 download

DESCRIPTION

Stack & Queue. Winter-Camp-2011 CSIE.NTNU. [email protected]@Taiwan. CC – 非商業性 – 相同方式. Stack. 一種串列式資料結構 LIFO (Last In First Out). Stack Sample. Make a Stack. -1. 0. 1. 2. 3. 4. 5. 6. 7. #define SIZE 8 int my_stack[SIZE]; int pointer = -1. if (pointer < 0) null. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Stack & Queue

Stack & Queue

Winter-Camp-2011CSIE.NTNU

[email protected]@Taiwan

CC – 非商業性 – 相同方式

Page 2: Stack & Queue

Stack

• 一種串列式資料結構• LIFO (Last In First Out)

Page 3: Stack & Queue

Stack Sample

Page 4: Stack & Queue

Make a Stack

0 1 2 3 4 5 6 7-1

if (pointer < 0) null.

if (pointer == SIZE) full.

#define SIZE 8int my_stack[SIZE];int pointer = -1

Page 5: Stack & Queue

Queue

• 一種串列式資料結構• FIFO(First In First Out)

Page 6: Stack & Queue

Queue Sample

Page 7: Stack & Queue

Make a Queue

0 1 2 3 4 5 6 7

if (front == rear) null.

if (rear == SIZE) full.

Page 8: Stack & Queue

Circular Queue

Page 9: Stack & Queue

Stack Applications

• LISP functions

• Infix to Postfix

Page 10: Stack & Queue

LISP Functions

Page 11: Stack & Queue

LISP Functions

(    ( (    ) (    ) ) )oddp - 5 2+ * 2 3

Page 12: Stack & Queue

LISP Functions

(    ( (    ) (    ) ) ) - 將左括號放入 stack , 看到右括號就從 stack 拿出一個左括號

(

(

((

Page 13: Stack & Queue

Infix to Postfix

• Infix : (1 + 2) * 3 + 4• Postfix : 1 2 + 3 * 4 +

Page 14: Stack & Queue

Infix to Postfix

• Use a Stack• Output the numbers• Push operators into the stack– If there are operators having higher(or equal)

precedence, pop them.– When read ) , pop all operators until pop a ( .

Page 15: Stack & Queue

Infix to Postfix

• Operator Precedence

– Outside : ( > * = / > + = -– Inside : * = / > + = - > (

Page 16: Stack & Queue

Infix to Postfix

• ( 1 + 2 ) * 3 / 4

(*

+

/

1 2 + 3 * 4 /

Page 17: Stack & Queue

Queue Applications

• BFS

Page 18: Stack & Queue

Test Yourself

• 673 - Parentheses Balance• 271 - Simply Syntax• 727 - Equation

Page 19: Stack & Queue

Test YourselfPractice:Use a Circular Queue

Sample Input:100200-1420-1-110-10

Sample Output:100200420