Stacks & Queues

Post on 27-Dec-2021

17 views 0 download

Transcript of Stacks & Queues

Stacks & Queues

Kuan-Yu Chen (陳冠宇)

2018/10/01 @ TR-212, NTUST

2

Review• Stack

– Stack Permutation

• Expression– Infix– Prefix– Postfix

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

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

5

Applications.• System stack in the case of function calls

6

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

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)

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)

9

Implementation for Stack by Array.• Declare

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛

top

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛−1⋮

𝑎𝑎1𝑎𝑎2

𝑎𝑎𝑛𝑛

𝑎𝑎𝑛𝑛 adddelete

10

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

11

Implementation for Stack by Array…• For “pop”

12

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

13

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

14

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

write down the prefix and postfix expressions– Prefix

𝐴𝐴 𝐵𝐵

+ 𝐶𝐶

×

𝐸𝐸 𝐹𝐹

÷𝐷𝐷

÷

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

infix

prefix

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

15

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

write down the prefix and postfix expressions– Postfix

𝐴𝐴 𝐵𝐵

+ 𝐶𝐶

×

𝐸𝐸 𝐹𝐹

÷𝐷𝐷

÷

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

infix

prefix

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

16

Algorithm to Convert Infix to Postfix.

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

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 * -

19

Algorithm to Convert Infix to Prefix – 1

20

Algorithm to Convert Infix to Prefix – 2.

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

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

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

23

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

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

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

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

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

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

26

Applications – Queue• Job scheduling

– A fair method

27

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

– Given a queue

– Insert an element

– Delete an element

28

Implementation for Queue by Array.• Declare

29

Implementation for Queue by Array..

2 7

front rear

0 1 2 3 4

?

30

Implementation for Queue by Array...

7

front rear

0 1 2 3 4

31

Implementation for Queue by Array….

32

Questions?

kychen@mail.ntust.edu.tw