Stacks & Queues Kuan-Yu Chen ( 陳冠宇 ) 2018/10/01 @ TR-212, NTUST
Review Stack Stack Permutation Expression Infix Prefix Postfix 2
Stacks. A stack is an ordered list in which insertions and deletions are made at one end called the top Given a stack SS = (aa 1, aa 2,, aa nn ) aa 1 is the bottom element aa nn is the top element aa nn aa ii is on top of element aa ii 1 add delete top aa nn aa nn aa nn 1 aa 2 aa 1 aa 2 aa 1 aa 2 aa 1 3
Stacks.. By the definition of stack, if we add the elements AA, BB, CC, DD, EE to the stack, in that order, then EE is the first element we delete from the stack Last-In-First-Out 4
Applications. System stack in the case of function calls 5
Applications.. System stack in the case of function calls 6
Applications For a recursive function, the stack can be used to store the processing status Given an Ackerman s function AA(mm, nn), please calculate AA(1,2) nn + 1, iiii mm = 0 AA mm, nn = AA mm 1,1, iiii nn = 0 AA mm 1, AA mm, nn 1, ooooooooooooooooo AA 1,2 = AA 0, AA 1,1 AA 1,1 = AA 0, AA 1,0 AA 1,0 = AA 0,1 AA(1,2) AA(1,1) AA(1,2) AA 0,1 = 2 AA(1,0) AA(1,1) AA(1,2) 7
Applications. For a recursive function, the stack can be used to store the processing status Given an Ackerman s function AA(mm, nn), please calculate AA(1,2) nn + 1, iiii mm = 0 AA mm, nn = AA mm 1,1, iiii nn = 0 AA mm 1, AA mm, nn 1, ooooooooooooooooo AA 1,2 = AA 0, AA 1,1 = AA 0,3 = 4 AA(1,2) AA 1,1 = AA 0, AA 1,0 = AA 0,2 = 3 AA 1,0 = AA 0,1 = 2 AA 0,1 = 2 AA(1,0) AA(1,1) AA(1,2) AA(1,1) AA(1,2) 8
Declare Implementation for Stack by Array. aa nn add delete top aa nn aa nn aa nn 1 aa 2 aa 1 aa 2 aa 1 aa 2 aa 1 9
Implementation for Stack by Array.. For push 10
Implementation for Stack by Array For pop 11
Implementation for Stack by Array. For display 12
Implementation for Stack by Array.. For peek 13
Prefix Expression Given a infix expression (AA + BB) CC (DD EE FF), please write down the prefix and postfix expressions Prefix prefix eeeeeeeeeeeeee postfix + CC DD infix AA BB EE FF +AAAAAA DD EEEE 14
Postfix Expression Given a infix expression (AA + BB) CC (DD EE FF), please write down the prefix and postfix expressions Postfix prefix eeeeeeeeeeeeee postfix + CC DD infix AA BB EE FF AAAA + CC DDDDDD 15
Algorithm to Convert Infix to Postfix. 16
Algorithm to Convert Infix to Postfix.. Take AA BB CC + DDDDD FF GG HH for example Infix Scanned Stack Postfix Expression ( A ( A - ( - A ( ( - ( A B ( - ( A B / ( - ( / A B C ( - ( / A B C + ( - ( + A B C / ( ( - ( + ( A B C / D ( - ( + ( A B C / D % ( - ( + ( % A B C / D E ( - ( + ( % A B C / D E 17
Algorithm to Convert Infix to Postfix... Take AA BB CC + DDDDD FF GG HH for example Infix Scanned Stack Postfix Expression E ( - ( + ( % 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 * - 18
Algorithm to Convert Infix to Prefix 1 19
Algorithm to Convert Infix to Prefix 2. Take (AA BB CC) (AA KK LL) for example Step1: (LL KK AA) (CC BB AA) Step2: LLLLLL CCCC AA Step3: AA BBBB AAAAAA 20
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 ( ( - ( A B ( - ( A B / ( - ( / A B C ( - ( / A B C 21
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 ( ( - ( A B ( - ( A B / ( - ( / A B C ( - ( / A B C 22
Homeork1: Expression Convertor.. Given a infix expression, please convert the expression to both prefix and postfix expressions (AA BB CC) (AA KK LL) Prefix: AA BBBB AAAAAA Postfix: AAAAAA AAAA LL AA BB CC + DDDDD FF GG HH Prefix: AA + BBBB %DDDDDDDDDD Postfix: AAAAAA DDDDDDD GG +HH 23
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 QQ = (aa 1, aa 2,, aa nn ) aa 1 is the front element aa nn is the rear element aa ii is behind element aa ii 1 aa nn add aa nn rear aa nn 1 front aa 2 aa 1 aa 2 aa 1 delete 24
Queue.. By the definition of queue, if we insert the elements AA, BB, CC, DD, EE in the order, then AA is the first element deleted from the queue First-In-First-Out 25
Job scheduling A fair method Applications Queue 26
Array Representation of Queues Queues can be easily represented using arrays Given a queue Insert an element Delete an element 27
Implementation for Queue by Array. Declare 28
Implementation for Queue by Array..? front rear 2 7 0 1 2 3 4 29
Implementation for Queue by Array... front rear 7 0 1 2 3 4 30
Implementation for Queue by Array. 31
Questions? kychen@mail.ntust.edu.tw 32