stacks & queues

32
Stacks & Queues Kuan -Yu Chen (陳冠宇) 2018/10/01 @ TR-212, NTUST

Upload: others

Post on 27-Dec-2021

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stacks & Queues

Stacks & Queues

Kuan-Yu Chen (陳冠宇)

2018/10/01 @ TR-212, NTUST

Page 2: Stacks & Queues

2

Review• Stack

– Stack Permutation

• Expression– Infix– Prefix– Postfix

Page 3: Stacks & Queues

3

Stacks.• A stack is an ordered list in which insertions and deletions

are made at one end called the top– Given a stack 𝑆𝑆 = (𝑎𝑎1,𝑎𝑎2, … ,𝑎𝑎𝑛𝑛)

• 𝑎𝑎1 is the bottom element• 𝑎𝑎𝑛𝑛 is the top element• 𝑎𝑎𝑖𝑖 is on top of element 𝑎𝑎𝑖𝑖−1

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛

top

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛−1⋮

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛

𝑎𝑎𝑛𝑛 adddelete

Page 4: Stacks & Queues

4

Stacks..• By the definition of stack, if we add the elements 𝐴𝐴,𝐵𝐵,𝐶𝐶,𝐷𝐷,𝐸𝐸

to the stack, in that order, then 𝐸𝐸 is the first element we delete from the stack– Last-In-First-Out

Page 5: Stacks & Queues

5

Applications.• System stack in the case of function calls

Page 6: Stacks & Queues

6

Applications..• System stack in the case of function calls

Page 7: Stacks & Queues

7

Applications…• For a recursive function, the stack can be used to store the

processing status– Given an Ackerman’s function 𝐴𝐴(𝑚𝑚,𝑛𝑛), please calculate 𝐴𝐴(1,2)

𝐴𝐴 𝑚𝑚,𝑛𝑛 = �𝑛𝑛 + 1, 𝑖𝑖𝑖𝑖 𝑚𝑚 = 0

𝐴𝐴 𝑚𝑚 − 1,1 , 𝑖𝑖𝑖𝑖 𝑛𝑛 = 0𝐴𝐴 𝑚𝑚 − 1,𝐴𝐴 𝑚𝑚,𝑛𝑛 − 1 , 𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑖𝑖𝑜𝑜𝑜𝑜

𝐴𝐴 1,2 = 𝐴𝐴 0,𝐴𝐴 1,1

𝐴𝐴 1,1 = 𝐴𝐴 0,𝐴𝐴 1,0

𝐴𝐴 1,0 = 𝐴𝐴 0,1

𝐴𝐴 0,1 = 2

𝐴𝐴(1,2)𝐴𝐴(1,2)𝐴𝐴(1,1)

𝐴𝐴(1,2)𝐴𝐴(1,1)𝐴𝐴(1,0)

Page 8: Stacks & Queues

8

Applications….• For a recursive function, the stack can be used to store the

processing status– Given an Ackerman’s function 𝐴𝐴(𝑚𝑚,𝑛𝑛), please calculate 𝐴𝐴(1,2)

𝐴𝐴 𝑚𝑚,𝑛𝑛 = �𝑛𝑛 + 1, 𝑖𝑖𝑖𝑖 𝑚𝑚 = 0

𝐴𝐴 𝑚𝑚 − 1,1 , 𝑖𝑖𝑖𝑖 𝑛𝑛 = 0𝐴𝐴 𝑚𝑚 − 1,𝐴𝐴 𝑚𝑚,𝑛𝑛 − 1 , 𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑖𝑖𝑜𝑜𝑜𝑜

𝐴𝐴 1,2 = 𝐴𝐴 0,𝐴𝐴 1,1 = 𝐴𝐴 0,3 = 4

𝐴𝐴 1,1 = 𝐴𝐴 0,𝐴𝐴 1,0 = 𝐴𝐴 0,2 = 3

𝐴𝐴 1,0 = 𝐴𝐴 0,1 = 2

𝐴𝐴 0,1 = 2

𝐴𝐴(1,2)

𝐴𝐴(1,2)𝐴𝐴(1,1)

𝐴𝐴(1,2)𝐴𝐴(1,1)𝐴𝐴(1,0)

Page 9: Stacks & Queues

9

Implementation for Stack by Array.• Declare

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛

top

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛−1⋮

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛

𝑎𝑎𝑛𝑛 adddelete

Page 10: Stacks & Queues

10

Implementation for Stack by Array..• For “push”

Page 11: Stacks & Queues

11

Implementation for Stack by Array…• For “pop”

Page 12: Stacks & Queues

12

Implementation for Stack by Array….• For “display”

Page 13: Stacks & Queues

13

Implementation for Stack by Array…..• For “peek”

Page 14: Stacks & Queues

14

Prefix Expression• Given a infix expression (𝐴𝐴 + 𝐵𝐵) × 𝐶𝐶 ÷ (𝐷𝐷 − 𝐸𝐸 ÷ 𝐹𝐹), please

write down the prefix and postfix expressions– Prefix

𝐴𝐴 𝐵𝐵

+ 𝐶𝐶

×

𝐸𝐸 𝐹𝐹

÷𝐷𝐷

÷

𝑜𝑜𝑒𝑒𝑜𝑜𝑚𝑚𝑜𝑜𝑛𝑛𝑜𝑜 postfix

infix

prefix

÷× +𝐴𝐴𝐵𝐵𝐶𝐶 − 𝐷𝐷 ÷ 𝐸𝐸𝐹𝐹

Page 15: Stacks & Queues

15

Postfix Expression• Given a infix expression (𝐴𝐴 + 𝐵𝐵) × 𝐶𝐶 ÷ (𝐷𝐷 − 𝐸𝐸 ÷ 𝐹𝐹), please

write down the prefix and postfix expressions– Postfix

𝐴𝐴 𝐵𝐵

+ 𝐶𝐶

×

𝐸𝐸 𝐹𝐹

÷𝐷𝐷

÷

𝑜𝑜𝑒𝑒𝑜𝑜𝑚𝑚𝑜𝑜𝑛𝑛𝑜𝑜 postfix

infix

prefix

𝐴𝐴𝐵𝐵 + 𝐶𝐶 × 𝐷𝐷𝐸𝐸𝐹𝐹 ÷−÷

Page 16: Stacks & Queues

16

Algorithm to Convert Infix to Postfix.

Page 17: Stacks & Queues

17

Algorithm to Convert Infix to Postfix..• Take 𝐴𝐴 − 𝐵𝐵 ÷ 𝐶𝐶 + 𝐷𝐷𝐷𝐸𝐸 × 𝐹𝐹 ÷ 𝐺𝐺 × 𝐻𝐻 for example

Infix Scanned Stack Postfix Expression(

A ( A- ( - A( ( - ( AB ( - ( A B/ ( - ( / A BC ( - ( / A B C+ ( - ( + A B C /( ( - ( + ( A B C /D ( - ( + ( A B C / D% ( - ( + ( % A B C / DE ( - ( + ( % A B C / D E

Page 18: Stacks & Queues

18

Algorithm to Convert Infix to Postfix...• Take 𝐴𝐴 − 𝐵𝐵 ÷ 𝐶𝐶 + 𝐷𝐷𝐷𝐸𝐸 × 𝐹𝐹 ÷ 𝐺𝐺 × 𝐻𝐻 for example

Infix Scanned Stack Postfix ExpressionE ( - ( + ( % A B C / D E* ( - ( + ( * A B C / D E %F ( - ( + ( * A B C / D E % F) ( - ( + A B C / D E % F */ ( - ( + / A B C / D E % F *G ( - ( + / A B C / D E % F * G) ( - A B C / D E % F * G / +* ( - * A B C / D E % F * G / +H ( - * A B C / D E % F * G / + H) A B C / D E % F * G / + H * -

Page 19: Stacks & Queues

19

Algorithm to Convert Infix to Prefix – 1

Page 20: Stacks & Queues

20

Algorithm to Convert Infix to Prefix – 2.

– Take (𝐴𝐴 − 𝐵𝐵 ÷ 𝐶𝐶) × (𝐴𝐴 ÷ 𝐾𝐾 − 𝐿𝐿) for example• Step1: (𝐿𝐿 − 𝐾𝐾 ÷ 𝐴𝐴) × (𝐶𝐶 ÷ 𝐵𝐵 − 𝐴𝐴)• Step2: 𝐿𝐿𝐾𝐾𝐴𝐴 ÷ −𝐶𝐶𝐵𝐵 ÷ 𝐴𝐴 −ו Step3: × −𝐴𝐴 ÷ 𝐵𝐵𝐶𝐶 −÷ 𝐴𝐴𝐾𝐾𝐿𝐿

Page 21: Stacks & Queues

21

Homeork1: Expression Convertor.• Given a infix expression, please convert the expression to

both prefix and postfix expressions– The implementation must base on stack– Please show the results step-by-step– Please upload your source codes and a paper report to moodle– TA will ask you to demo your program– The hard deadline is 2018/10/15 8:00

Infix Scanned Stack Postfix Expression(

A ( A- ( - A( ( - ( AB ( - ( A B/ ( - ( / A BC ( - ( / A B C

Page 22: Stacks & Queues

22

Homeork1: Expression Convertor..• Given a infix expression, please convert the expression to

both prefix and postfix expressions– The maximum length of the input expression will always less

than 30– Only five operators need to be considered

• +,−, ×, ÷, 𝐷– The operands are capital letters (i.e., A~Z)

Infix Scanned Stack Postfix Expression(

A ( A- ( - A( ( - ( AB ( - ( A B/ ( - ( / A BC ( - ( / A B C

Page 23: Stacks & Queues

23

Homeork1: Expression Convertor..• Given a infix expression, please convert the expression to

both prefix and postfix expressions– (𝐴𝐴 − 𝐵𝐵 ÷ 𝐶𝐶) × (𝐴𝐴 ÷ 𝐾𝐾 − 𝐿𝐿)

• Prefix: × −𝐴𝐴 ÷ 𝐵𝐵𝐶𝐶 −÷ 𝐴𝐴𝐾𝐾𝐿𝐿• Postfix: 𝐴𝐴𝐵𝐵𝐶𝐶 ÷ −𝐴𝐴𝐾𝐾 ÷ 𝐿𝐿 −×

– 𝐴𝐴 − 𝐵𝐵 ÷ 𝐶𝐶 + 𝐷𝐷𝐷𝐸𝐸 × 𝐹𝐹 ÷ 𝐺𝐺 × 𝐻𝐻• Prefix: −𝐴𝐴 ×+÷ 𝐵𝐵𝐶𝐶 ÷× 𝐷𝐷𝐷𝐸𝐸𝐹𝐹𝐺𝐺𝐻𝐻• Postfix: 𝐴𝐴𝐵𝐵𝐶𝐶 ÷ 𝐷𝐷𝐸𝐸𝐷𝐹𝐹 × 𝐺𝐺 ÷ +𝐻𝐻 × −

Page 24: Stacks & Queues

24

Queue.• A queue is an ordered list in which insertions take place at

one end (rare) and deletions are made at the opposite end (front)– Given a queue 𝑄𝑄 = (𝑎𝑎1,𝑎𝑎2, … ,𝑎𝑎𝑛𝑛)

• 𝑎𝑎1 is the front element• 𝑎𝑎𝑛𝑛 is the rear element• 𝑎𝑎𝑖𝑖 is behind element 𝑎𝑎𝑖𝑖−1

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛−1⋮

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛

𝑎𝑎𝑛𝑛 add

deletefront

rear

Page 25: Stacks & Queues

25

Queue..• By the definition of queue, if we insert the elements 𝐴𝐴,𝐵𝐵,𝐶𝐶,𝐷𝐷,𝐸𝐸 in the order, then 𝐴𝐴 is the first element deleted from the queue– First-In-First-Out

Page 26: Stacks & Queues

26

Applications – Queue• Job scheduling

– A fair method

Page 27: Stacks & Queues

27

Array Representation of Queues• Queues can be easily represented using arrays

– Given a queue

– Insert an element

– Delete an element

Page 28: Stacks & Queues

28

Implementation for Queue by Array.• Declare

Page 29: Stacks & Queues

29

Implementation for Queue by Array..

2 7

front rear

0 1 2 3 4

?

Page 30: Stacks & Queues

30

Implementation for Queue by Array...

7

front rear

0 1 2 3 4

Page 31: Stacks & Queues

31

Implementation for Queue by Array….

Page 32: Stacks & Queues

32

Questions?

[email protected]