Download presentation

Presentation is loading. Please wait.

Published bySantos Simeon Modified over 4 years ago

1
CS 235102 Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012

2
1 Stack – Abstract Data Type (ADT) Object: Stack An order list in which insertions and deletions are made at only one end. Operations: Insert(a) or Push(a) ::= insert element a into the stack. Delete() or Pop() ::= delete and return top element from the stack. IsFull ::= return FALSE or TRUE. IsEmpty ::= return FALSE or TRUE.

3
Stack operates in Last-In-First-Out (LIFO) order 2 Operatons on Stack BABA CBACBA top Insert A A Insert BInsert C empty

4
Operations on a Stack 3 BABA DBADBA top DeleteInsert D CBACBA top

5
Using a 1-D array Stack [MAX_STACK_SIZE] top: point to the top of the element of the stack top = -1 denotes an empty stack. 4 Representation of a stack

6
5 Operations: Push Push( int *top, element item) { if (*top >= MAX_STACK_SIZE-1) { stackFull(); return; } *top = *top +1; stack[*top] = item; }

7
Operations: Pop 6 /* return the top element from the stack */ element Pop (int *top) { if (*top == -1) return StackEmpty(); item = stack[*top]; *top = *top-1; return item; }

8
System stack: used in the run time to process nested procedure calls The return addresses of previous outer procedures are stored in the stack. Application of Stack 7

9
8 By the time Sub3 is called, the stack contains all three return addresses: Return address to Sub2 Return address to Sub1 Return address to main System stack

10
Application of Stack Creation and activation: record for the invoked subprogram return address for calling procedure Parameters of current procedure local variables in current procedure When exit a subprogram, delete the activation record on the top of the stack. 9

11
10 Queue – Abstract Data Type (ADT) Object: Queue an ordered list in which all insertions take place at the end and all deletions take place at the other end Operations: Add(a) ::= insert element a at the rear of the queue. DeleteQ() ::= delete and return front element from the queue. IsFullQ ::= return FALSE or TRUE. IsEmptyQ ::= return FALSE or TRUE.

12
Operations on a Queue Eg. First-In-First-Out (FIFO) order: f: front pointer r: rear pointer 11

13
Representation of a Queue Use a 1-D array Queue [MAX_QUEUE_SIZE] front, rear: two variables point to the two ends of the queue. Initialization: front = -1; rear = -1 front: one less than the position of the first element in the queue. 12 Queue

14
Representation of a Queue front == rear empty Queue 13 Queue frontrear

15
Operations: AddQ 14 AddQ(int *rear, element item) { if (*rear == MAX_Queue_SIZE - 1){ QueueFull(); return; } *rear = *rear +1; queue[*rear] = item; }

16
Operations: DeleteQ 15 element DeleteQ(int *front, int rear) { if (*front == rear) return QueueEmpty(); *front = *front + 1; return queue[*front]; }

17
Operations: Problem Problem: a Job queue by operating system Q[1] Q[2] Q[3]… J_1Insert J_1 J_1 J_2 J_3 Insert J_2, J_3 J_2 J_3 Delete J_2 J_3 J_4 J_5 Insert J_4, J_5 J_3 J_4 J_5 Delete J_4 J_5 Delete The queue shifts to the right When queue is full, we need to move the entire queue left time consuming 16

18
Solution Use a circular array to represent a queue, 17 if rear == Max_Queue_Size -1 then rear = 0; else rear = rear +1; rear = (rear+1) % Max_Queue_Size; [1][1] [0][0] [2][2] [3][3] [4][4] [5][5] front rear

19
How to test “Empty” & “Full” ? Empty front == rear 18 [2] [1][1] [0][0] [3][3] front rear

20
How to test “Empty” & “Full” ? Full rear + 1 == front use only n-1 element use n elements will not be able to distinguish full or empty 19 [2] [1][1] [0][0] [3][3] front rear [4][4]

21
AddQ using Circular Array 20 AddQ( int front, int *rear, element item) { *rear = (*rear +1) % MAX_QUEUE_SIZE; if (front == *rear){ QueueFull(); return; } Queue[*rear] = item; }

22
DeleteQ using Circular Array 21 element DeleteQ(int *front, int rear) { if (*front == rear) return QueueEmpty(); *front = (*front +1) % MAX_QUEUE_SIZE; return queue[*front]; }

23
Evaluation of An Expression x = A/B-C How to evaluate an expression ? let A=6, B=3, C=2 (A/B)-C =(6/3)-2 =2-2 =0 (A/(B-C)) =(6/(3-2) ) =6/1 =6 22

24
Rules to evaluate an expression Assign precedence to operator, and Evaluate first the operator with higher precedence Left to right for the operator with the same precedence (in general), Right to left for prefix unary operators 23

25
Relues to evaluate an expression 24

26
Example 1 ALU two operands & one result Compiler generates codes for the ALU Eg: D=A+B*C mul B C T1 add A T1 D source destination 25 +, -, *, /

27
Example 2 Eg2: D=A+B-C add A B T1 sub T1 C D Generates correct code so that when the code is evaluated, the expression is computed correctly. 26

28
Infix and Postfix Notations Infix notation = operator comes in–between the operands Eg: A+B*C A+B-C It is not easier for compiler to generate code to evaluate an expression in infix notation Postfix notation: each operator appears after its operands Eg: A+B-Cinfix AB+C-postfix 27

29
Translate infix to postfix How to translate from infix to postfix? A/B-C First Algorithm: (1) Fully parenthesize the expression (2) Move all operators so that they replace their corresponding right parenthesis (3) Delete all parenthesis 28

30
Examples Eg1: ( ( A / B ) – C ) A B / C - Eg2: ( ( A + B ) - ( C * D ) ) A B + C D * - Eg3: ((((A / B ) – C ) + ( D * E ) ) - ( A * C ) ) A B / C - D E * + A C * - 29

31
What is good about postfix? Evaluation of postfix expression done in one pass Eg1: A + B - C A B + C - add A B T1 sub T1 C T2 Scan the expression from left to right Use stack to hold operands Example 1 30 + T1 T2 -

32
Example 1: A B + C - ‘+’ pop operands A, B T1 = A + B push T1 to stack 31 A BABA T1=A+B A B C C T1=A+B ‘-’ pop operands C, T1 T2 = T1 – C push T2 to stack T2=T1-C

33
Example 2 Ex2: A+B-C*D A B + C D * - 32 A BABA A B T1=A+B ‘+’ pop two operands A, B T1 = A+B push the result “*” pop two operands C, D T2 = C*D push the result C T1=A+B C D C T1=A+B D T2=C*D T1=A+B

34
Example 2 33 T2=C*D T1=A+B ‘-’ pop T1, T2 T3 = T1 – T2 push the result T3=T1-T2 Ex2: A+B-C*D A B + C D * -

35
Example 3 ‘/’ pop two operands T1 = A/B push result 34 A A BABA B T1=A/B Ex3. A B / C – D E * + A C * -

36
Example 3 ‘-’ pop two operands T2=T1-C push the result 35 C T1=A/B T2=T1-C C Ex3. A B / C – D E * + A C * -

37
Example 3 “*” pop two operands T3= D*E push the result 36 D T2=T1-C D E D T2=T1-C E T3=D*E T2=T1-C Ex3. A B / C – D E * + A C * -

38
Example 3 “*” Pop two operands T4=T3+T2 Push the result 37 T4=T3+T2 A T4=T3+T2 A C A T4 C Ex3. A B / C – D E * + A C * -

39
Example 3 “*” Pop two operands T5=A*C Push the result 38 T5=A*C T4 T6=T4-T5 Ex3. A B / C – D E * + A C * - “-” Pop two operands T6=T4 – T5 Push the result

40
Postfix vs Infix Postfix- when you see an operator, operands have been seen in front of it (on the top of the stick) Infix- when you see an operator, you do not know if the operand is ready Thus when we see the operator, we cannot do the evaluation for this operator yet. 39

41
Evaluate Postfix Expression EvalPostfixExpression(expression e) { x = NextToken(e); while (x is not end of expression) do { if (x is an operand) then push(&top, x) else{ /* x is an operator */ 1. pop the correct number of operands for x from stack; 2. perform the operation; (or generate the code) 3. push the result onto the stack; } x = NextToken(e); }

42
How to Transform Infix to Postfix? How to translate from infix to postfix? First algorithm: “Fully parenthesize” the expression Convert full parenthesized expr. to postfix Disadvantage: Need two passes of scanning the expression 41

43
Algorithm Two Alg2: 1. scan the expression just once 2. utilize stack Observations: 1: The positions of operands are not changed passing any operands to the output immediately 2: Store the operator in stack until it is time to pass it to the output 42

44
Algorithm Two “it is time”: means that the precedence of operator in the stack is larger or equal to that of the incoming operator (left-to-right associativity) 43

45
Example 1 2.1push operator to the stack 2.2 operators are taken out of stack as long as their precedence is higher or equal to the precedence of new incoming operator Eg1: A / B – C A B / C - 44 / A / A B - AB/ - AB/C AB/C- - eoe

46
Example 2 Eg2: A + B – C * D A B + C D * - 45 + A + A B - AB+ *-*- AB+C *-*- AB+CD AB+CD* - AB+CD*- - * eoe

47
Example 3 Eg3: A / B – C + D * E – A * C 46 / A / A B / AB/ - AB/C - AB/C- - + A B + AB/C-D + *

48
Example 3 Eg3: A / B – C + D * E – A * C 47 A B/C-DE *+*+ AB/C-DE*+ - - + AB/C-DE*+A - * AB/C-DE*+AC *-*- AB/C-DE*+AC*-

49
Transform Infix Expr. with ( ) Expression with ( ) 1. ‘( )’ evaluate first 2. ‘(‘ push ‘(’ to stack always 3. when ’(’ is on top of the stack, then push other operators (always) to stack 4. ‘)’ pop the operators until you see the matched ‘(‘ Eg. A * ( B + C ) / D 48 * ( (*(* AB (*(* + A +(*+(*

50
Transform Infix Expr. with ( ) 49 +(*+(* ) * ABC+ / ABC+*D ABC / eoe ABC+*D/ Eg. A * ( B + C ) / D

51
Transform Infix to Postfix 50 PosfixExpression(InfixExpression: e) { while (not end of expression){ x = NextToken(e); if x is an operand then write (x) else switch(x){ case ’(’: push (&top, ‘(’); break; case ‘)’: do { y = pop(&top); if (y!= ‘(’) write(y); } while y!= ‘(’; break; default: while (precedence of operator on top of stack >= precedence of x) { y=pop(&top); write(y); } push(&top, x); } } do{ y = pop (&top); write(y);} while !EmptyStack(); }

52
Multiple Stacks Multiple stacks Store multiple stacks in 1-D array # of stacks = 2 : - Using top[0] & top[1] # of stacks = k > 2 : - Using b[i] & top[i] for each stack b[0] b[1] b[2] b[k] 51 top[0]top[1]

53
B[i] and Top[i] for each Stack B[i] : points to the position immediately to the left of the bottom element of stack[i] Top[i]: points to the top element of stack[i] 52

54
To test empty & full of stack[i] empty: top[i] == b[i] full: top[i] == b[i+1] Add an item to stack[i] Operations 53 void push(int i, element item) { if (top[i] == b[i+1]) then StackFull(i); else { top[i] = top[i]+1; M[top[i]] = item; } }

55
Operations Delete top element from stack[i] StackFull(i) for stack[i]: Have to check all stacks (all memory elements) If there exists empty space in array M[] == > shift other stacks to give space to stack[i] 54 element pop(int i) { if (top[i] == b[i]) then StackEmpty(i); returnM[top[i]--]; }

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google