Presentation is loading. Please wait.

Presentation is loading. Please wait.

Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-1 Chapter 3 Stacks and Queues Introduction to Data Structures CHAPTER 3 STACKS and QUEUES 3.1 The Stack.

Similar presentations


Presentation on theme: "Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-1 Chapter 3 Stacks and Queues Introduction to Data Structures CHAPTER 3 STACKS and QUEUES 3.1 The Stack."— Presentation transcript:

1 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-1 Chapter 3 Stacks and Queues Introduction to Data Structures CHAPTER 3 STACKS and QUEUES 3.1 The Stack Abstract Data Type 3.2 The Queue Abstract Data Type 3.3 A Mazing Problem 3.4 Evaluation of Expressions 3.5 Multiple Stacks and Queues

2 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-2 Chapter 3 Stacks and Queues Chapter 1 Basic Concepts Chapter 2 Arrays Chapter 3 Stacks and Queues Chapter 4 Linked Lists Chapter 5 Trees Chapter 6 Graph Chapter 7 Sorting Chapter 8 Hashing Chapter 9 Heap Structures Chapter 10 Search Structures Contents

3 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-3 Chapter 3 Stacks and Queues Data Objects: Ordered list or Linear list A = (a 1, a 2,..., a n ) n > 0 a i is an element or atoms n = 0 null, empty list Stack Queue Representing by - Array (ch. 2) - Linked (ch. 4) special cases of Ordered List 3.1 The Stack Abstract Data Type

4 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-4 Chapter 3 Stacks and Queues Stack: is an ordered list in which all insertions and deletions are made at one end, called the top Two operators: Push and Pop. Basic Concepts: Stack EDCBAEDCBA top LIFO (Last In First Out) bottom top a i is on top of a i-1 S = ( a 0,..., a n-1 )

5 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-5 Chapter 3 Stacks and Queues  Stack: a Last-In-First-Out (LIFO) list BABA DCBADCBA CBACBA DCBADCBA EDCBAEDCBA top A Basic Concepts: Stack (cont.) Push (A) Push (B) Push (C) Push (D) Push (E) POP( ) = E

6 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-6 Chapter 3 Stacks and Queues Example 3.1 [System Stack]: Stack Application e.g. (Problem: process of subroutine calls) O.S proc. MAIN proc. A1 proc. A2 proc. A3 run MAIN call A1 call A2 call A3 q: r: s: t: end qrst qrst Top/ CURRENT-ADDR Basic Concepts: Stack Application

7 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-7 Chapter 3 Stacks and Queues old frame pointer return address fp main fp (a) (b) old frame pointer a1 return address local variables old frame pointer Main return addr. An application of stack: stack frame of function call stack frame of invoking function System Stack before A1 is invoked System Stack after A1 is invoked fp: a pointer to current stack frame Stack Application: Function Call local variables (size?)

8 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-8 Chapter 3 Stacks and Queues Data Structure: Stack Specification: ADT3.1: Abstract data type Stack (p.104) Objects Specify - Define data member - Declare member functions top stack CreateS Push (Add) Pop (Delete) IsFull IsEmpty Top

9 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-9 Chapter 3 Stacks and Queues Operators on stack 1. CreateS(S): create S as an empty stack 2. Add(i, s): 3. Delete(S): 4. IsEmpty(S): i top   Top(S)  top ‘ i ’ = Top(S) Push(i,s) Pop(S) true if S = empty → top = -1 false var ← ‵ i′ 與 Pop 不一樣 Stack Operations 5. Top(S): return the top element of stack S

10 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-10 Chapter 3 Stacks and Queues structure Stack is objects: a finite ordered list with zero or more elements. functions: for all stack  Stack, item  element, max_stack_size  positive integer Stack CreateS(max_stack_size) ::= create an empty stack whose maximum size is max_stack_size Boolean IsFull(stack, max_stack_size) ::= if (number of elements in stack == max_stack_size) return TRUE else return FALSE Stack Add(stack, item) ::= if (IsFull(stack)) stack_full else insert item into top of stack and return Boolean IsEmpty(stack) ::= if(stack == CreateS(max_stack_size)) return TRUE else return FALSE Element Delete(stack) ::= if(IsEmpty(stack)) return else remove and return the item on the top of the stack. Stack ADT Specification

11 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-11 Chapter 3 Stacks and Queues Define data member Representing a stack using 1-dim array Stack CreateS(max_stack_size) ::= #define MAX_STACK_SIZE 100 typedef struct { int key; /*other fields */ } element; element stack[MAX_STACK_SIZE]; int top = -1;... top Stack Implementation 0 1 2 n-1

12 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-12 Chapter 3 Stacks and Queues Stack Implementation (cont.) stack[0] stack[99]...... stack[1] stack[2] top = -1  empty if top = 99  full element stack[100];

13 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-13 Chapter 3 Stacks and Queues Stack functions Boolean IsEmpty(Stack) ::= top == -1; Boolean IsFull(Stack) ::= top >= MAX_STACK_SIZE-1; Stack Add(stack, item) ::= void add(int *top, element item) { if (*top >= MAX_STACK_SIZE-1) { stack_full(); return; } stack[++*top] = item; } Stack Implementation (cont.) top Garbage collection & Re-run Push

14 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-14 Chapter 3 Stacks and Queues Stack Delete(stack, item) ::= void delete(int *top) { if (*top == -1) { return stack_empty(); return stack[(*top)--]; } Pop Stack Implementation (cont.)

15 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-15 Chapter 3 Stacks and Queues  Queue: a First-In-First-Out (FIFO) list 3.2 The Queue Abstract Data Type Add rear front A Add A B rear front A B C Add rear front A B C D Add rear front B C D E Add rear front B C D Delete rear front

16 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-16 Chapter 3 Stacks and Queues Queue: is an ordered list in which all insertions take place at one end, the rear, all deletions take place at the other end, the front. A B C D E front rear FIFO (First In First Out) Basic Concepts: Queue

17 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-17 Chapter 3 Stacks and Queues Specification: ADT3.2: Abstract data type Queue (p.107) Objects Specify - Define data member - Declare member functions Basic Concepts: Queue Specification CreateQ IsFullQ IsEmptyQ AddQ DeleteQ FrontQ 0 1 n-1 f = r = -1

18 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-18 Chapter 3 Stacks and Queues structure Queue is objects: a finite ordered list with zero or more elements. functions: for all queue  Queue, item  element, max_queue_size  positive integer Queue CreateQ(max_queue_size) ::= create an empty queue whose maximum size is max_queue_size Boolean IsFullQ(queue, max_queue_size) ::= if (number of elements in queue == max_queue_size) return TRUE else return FALSE Queue AddQ(queue, item) ::= if (IsFullQ(queue)) queue_full else insert item at rear of queue and return queue Boolean IsEmptyQ(queue) ::= if (queue ==CreateQ(max_queue_size)) return TRUE else return FALSE Element DeleteQ(queue) ::= if (IsEmptyQ(queue)) return else remove and return the item at front of queue. QUEUE ADT Specification p.3-26

19 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-19 Chapter 3 Stacks and Queues Queue Implementation Data member declaration: By using 1-dimensional array Queue CreateQ(max_queue_size) ::= #define MAX_QUEUE_SIZE 100 typedef struct { int key; /*other fields */ } element; element queue[MAX_QUEUE_SIZE]; int rear = -1; int front = -1;

20 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-20 Chapter 3 Stacks and Queues queue[0] queue[99]...... queue[1] queue[2] element queue [100]; front = rear = -1  empty if rear = 99  full Queue Implementation (cont.)

21 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-21 Chapter 3 Stacks and Queues Queue Implementation (cont.) Stack functions Boolean IsEmptyQ(queue) ::= front == rear; Boolean IsFullQ(queue) ::= rear == MAX_QUEUE_SIZE-1; Queue AddQ(queue, item) ::= void addq(int *rear, element item) { /* add an item to the queue */ if (*rear == MAX_QUEUE_SIZE_1) { queue_full( ); return; } queue[++*rear] = item; }

22 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-22 Chapter 3 Stacks and Queues...... rear = 2 addq(&rear, 10); addq(&rear, 15); addq(&rear, 20); 20 15 10 queue[0] queue[99] queue[1] queue[2] front = -1 Queue Implementation (cont.)

23 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-23 Chapter 3 Stacks and Queues Queue Implementation (cont.) Queue DeleteQ(queue) ::= element deleteq(int *front, int rear) { /* remove element at the front of the queue */ if ( *front == rear) return queue_empty( ); /* return an error key */ return queue[++ *front]; }

24 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-24 Chapter 3 Stacks and Queues values = deleteq(&front, rear); values = 10...... rear = 2 20 15 queue[0] queue[99] queue[1] queue[2] front = 0 10 Queue Implementation (cont.)

25 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-25 Chapter 3 Stacks and Queues Job Scheduling no priority priority 大 小 or operations 1. CREATEQ (Q) 2. ADDQ (i,Q) : add to the rear 3. DELETEQ (Q) : removes the front element 4. FRONT (Q) : return the front element of Q 5. IsEmpty (Q) job1job2job4 job3job5job6 Front Rear multi Queue 3.4 job2 job1 OS multi programming job2job1 batch system Queue Application

26 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-26 Chapter 3 Stacks and Queues (Solution 1) (Solution 2) More efficient queue representation: by regarding the array Q(1:n) as Circular.  f=0 r=3f=4 r=n  f=3 r=n J n+1 inserting Job n+1 Queue Full Problem  Problem: there may be available space when IsFullQ is true i.e. may need some movements Boolean IsFullQ(queue, max_queue_size ) ::= if (number of elements in queue == max_queue_size) return TRUE else return FALSE p.3-18

27 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-27 Chapter 3 Stacks and Queues (Solution 2) Circular Queue Q (0:n-1) J1, J2, J3, J4 3 2 1 0 n-2 n-1 fr initial f = r = 0 f= r iff Circular Queue is empty J4 J3 J1 0 n-1 f r J2 Add: if r== n-1 then r = 0 else r = r+1  r = (r+1) mod n Delete if f== n-1 then f = 0 else f = f+1  f = (f+1) mod n Basic Concepts: Circular Queue

28 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-28 Chapter 3 Stacks and Queues Note: AddQ r = (r+1) mod n if r == f then call QUEUE_FULL Need to leave a space as unused How to utilize the full space of circular queue Using an additional tag tag = 0 iff Queue empty = 1 iff QUEUE_FULL Note: testing needs time!! Slow down the algorithms. r f r 0 n-1 Basic Concepts: Circular Queue

29 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-29 Chapter 3 Stacks and Queues void addq(int front, int *rear, element item) { /* add an item to the queue */ *rear = (*rear +1) % MAX_QUEUE_SIZE; if (front == *rear) queue_full(rear); /* reset rear and print error */ return; } queue[*rear] = item; } Add to a circular queue Circular Queue: Add

30 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-30 Chapter 3 Stacks and Queues J2 J1 J3 [2] [0] [5] [1] [3] [4] front = 0 rear = 3 addq(front, &rear, ‘J1’); addq(front, &rear, ‘J2’); addq(front, &rear, ‘J3’); Circular Queue: Add (cont.)

31 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-31 Chapter 3 Stacks and Queues element deleteq(int* front, int rear) { element item; /* remove front element from the queue and put it in item */ if (*front == rear) return queue_empty( ); /* queue_empty returns an error key */ *front = (*front+1) % MAX_QUEUE_SIZE; return queue[*front]; } Delete from a circular queue Circular Queue: Delete

32 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-32 Chapter 3 Stacks and Queues values = deleteq(&front, rear); J2 J1 J3 [2] [0] [5] [1] [3] [4] front = 1 rear = 3 values = ‘J1’ Circular Queue: Delete (cont.)

33 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-33 Chapter 3 Stacks and Queues “Rat-in-a-maze” Fig 3.8 Array MAZE [1..m,1..p] 0: open path ; 1: barrier 12...p 10100.. 21000...0110... m j i entrance exit 3.3 A Mazing Problem: Stack Application

34 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-34 Chapter 3 Stacks and Queues 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 Entrance Exit 1: blocked path 0: through path A Mazing Problem

35 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-35 Chapter 3 Stacks and Queues moves (i, j) N (i-1, j) (i, j+1) E (i+1, j) S (i+1, j+1) SE NENN 2-dim array moves move[d].vertmove[d].horiz N0 NE1 E01... NW next step (g, h) g := i + move[d].vert h := j + move[d].horiz (i-1, j+1) (i-1, j-1) W (i, j-1) (i+1, j-1) SW A Mazing Problem

36 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-36 Chapter 3 Stacks and Queues ||||||| |p| || |m| || || ||||||| 0...p+1 000000.0.. m+1 00...0 0: not yet visited Array MAZE [0..m+1, 0..p+1] MARK [0..m+1, 0..p+1] A Mazing Problem

37 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-37 Chapter 3 Stacks and Queues Example: X = a / b - c + d * e - a * c Let a = 4, b = c = 2, d = e = 3 Evaluation 1: ((4/2)-2)+(3*3)-(4*2)=0 +9 - 8=1 Evaluation 2: (4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.66666 … How to generate the machine instructions corresponding to a given expression?  precedence rule + associative rule 3.4 Evaluation of Expressions T1  a / b T2  d * e T3  a * c T4  T1 - c T5  T4 + T2 X  T5 – T4 “Machine” instructions

38 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-38 Chapter 3 Stacks and Queues Evaluation of expressions: Stack Application Expression  Machine language (or Assembly code) Evaluation E.g. 1. Expression: X  A / B ** C + D * E – A * C (1) a+b ab+ +ab infix postfix(suffix) prefix ABC**/DE*+AC*- postfix of (1) 1 21 22 23 31 scanning 3 次 Evaluation of Expressions: the Problem 32

39 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-39 Chapter 3 Stacks and Queues Example 1. Infix: X  A / B ** C + D * E – A * C (1) Postfix: ABC**/DE*+AC*- Evaluate (1)  Evaluate postfix of (1) Note: scan once B**C A E D A/B**C D*E A/B**C C A A/B**C+D*E A*C A/B**C+D*E A/B**C+D*E-A*C CBACBA **  // ** ++ ** -- Stack Evaluation of Expressions: Example 1

40 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-40 Chapter 3 Stacks and Queues Example 2. Infix: A / B – C + D * E – A * C Postfix: A B / C – D E * + A C * – Figure 3.14: Postfix evaluation, p.120 Operations vs. postfix Ex. ??? Program 3.9: Evaluating Postfix Expressions, p. 122 int eval(void) Evaluation of Expressions: Example 2

41 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-41 Chapter 3 Stacks and Queues Method 1 Rule (p.121) 1. Fully parenthesize the expression 2. Move all binary op. so that they replace their corresponding ‘)‘ 3. Delete all parentheses Example 1. A/B**C+E*D-A*C  ((( A/(B**C))+(D*E))-(A*C))  ABC**/DE*+AC*- Example 2. A / B – C + D * E – A * C (Ex?) Conversion: Infix  Postfix

42 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-42 Chapter 3 Stacks and Queues Method 2: using stack Rule 1. operands  output; operators  Stack 2. Stack 中,新 priority > 於前者則 “ 疊上 : push”  前者跳出。 3. 最後, stack 中之 operator , pop 出來。 Example 1 A/B**C+E*D-A*C Ref. Fig 3.12 priority ** / ++ *+*+ -- *-*- ABC**/DE*+AC*- output pop Conversion: Infix  Postfix (cont.)

43 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-43 Chapter 3 Stacks and Queues Example 2 A*(B+C)/D rule 4: “(“ 及之後的 op. 依序放入 stack ,遇到 ”)” 時,一直 pop ,直到 pop 出 “(” Example 3 (A*B)+C/D AB*CD/+ Program 3.11: Infix  Postfix, p. 126 void postfix(void) +(*+(* )) * // / ABC+*D/ output Conversion: Infix  Postfix (cont.)

44 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-44 Chapter 3 Stacks and Queues coexist two variable-size stacks Note: STACK_FULL will occur only when the # of elements exceeds the total space bottom top bottom top top bottom  3.5 Multiple Stacks and Queues m[0] m[1] … m[n-2] m[n-1] bottommost stack 1 stack 2

45 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-45 Chapter 3 Stacks and Queues More than two Stacks, n if the sizes are known  proportional distribution unknown  equal segments 1 m stack 1stack 2stack 3stack 4 m-1 0  m/n  2  m/n  3  m/n  b(1) b(2) b(3) b(4) b(n) t(1) t(2) t(3) t(4) initial b(i) = t(i) = i  m/n   1, 0  i < n for stack i Coexist n Stacks

46 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-46 Chapter 3 Stacks and Queues boundary[0] boundary[1] boundary[2] boundary[n] top[0] top[1] top[2] -1 0  m/n  2  m/n  m-1 m-1 Initially, boundary[i] = top[i]. Coexist n Stacks (cont.) n Stacks: stack_1, stack_2, stack_3, stack_4, …, stack_n-1 memory is divided into n equal segments boundary[stack_no] 0  stack_no < MAX_STACKS top[stack_no] 0  stack_no < MAX_STACKS …

47 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-47 Chapter 3 Stacks and Queues #define MEMORY_SIZE 100 /* size of memory */ #define MAX_STACK_SIZE 100 /* max number of stacks plus 1 */ /* global memory declaration */ element memory[MEMORY_SIZE]; int top[MAX_STACKS]; int boundary[MAX_STACKS]; int n; /* number of stacks entered by the user */ top[0] = boundary[0] =  1; for (i = 1; i < n; i++) top[i] = boundary[i] = (MEMORY_SIZE/n) * i; boundary[n] = MEMORY_SIZE  1; Initialize stacks: Coexist n Stacks (cont.)

48 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-48 Chapter 3 Stacks and Queues void add(int i, element item) { /* add an item to the ith stack */ if (top[i] == boundary [i+1]) stack_full(i); memory[++top[i]] = item; } element delete(int i) { /* remove top element from the ith stack */ if (top[i] == boundary[i]) return stack_empty(i); return memory[top[i]--]; } Coexist n Stacks: Add and Delete

49 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-49 Chapter 3 Stacks and Queues Find j, stack i < j < n or 0  j < stack i such that top[j] < boundary[j+1] Stack i meets Stack i+1, but the memory is not full b[0] t[0] b[1] t[1] b[i] t[i] t[i+1] t[j] b[j+1] b[n] b[i+1] space Stack_i Full b[0] t[0] b[1] t[1] b[i] t[i] t[i+1] t[j] b[j+1] b[n] b[i+1] shift stacks

50 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-50 Chapter 3 Stacks and Queues If there is space available, it should shift stacks so that space is allocated to the full stack. b[0] t[0] b[1] t[1] b[i] t[i] t[i+1] t[j] b[j+1] b[n] b[i+1] shift stacks Stack_i Full (cont.) b[0] t[0] b[1] t[1] b[i] t[i] t[i+1] t[j] b[j+1] b[n] b[i+1] space

51 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-51 Chapter 3 Stacks and Queues STACK_FULL STACK i i+1 b(i) t(i) b(i+1)if t(i)=b(i+1) Sequential allocation Method 1: p.130 rule (1), (2), (3). Method 2: proportional allocation : Non-sequential allocation (Ch.4) Stack_i Full (cont.)

52 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-52 Chapter 3 Stacks and Queues Method 1 Rule (1) Right searching (2) Left searching (3) If fails during both searching, no free space Stack_i Full (cont.)

53 Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-53 Chapter 3 Stacks and Queues Method 2: Proportional allocation 2 4 3 2 1 4 3 Stack_i Full (cont.)


Download ppt "Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-1 Chapter 3 Stacks and Queues Introduction to Data Structures CHAPTER 3 STACKS and QUEUES 3.1 The Stack."

Similar presentations


Ads by Google