Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-1 Chapter 3 Stacks and Queues Introduction to...
-
Upload
milo-shields -
Category
Documents
-
view
221 -
download
0
description
Transcript of Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 3-1 Chapter 3 Stacks and Queues Introduction to...
3-1Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Introduction to Data Structures
CHAPTER 3
STACKS and QUEUES
3.1 The Stack Abstract Data Type3.2 The Queue Abstract Data Type3.3 A Mazing Problem3.4 Evaluation of Expressions3.5 Multiple Stacks and Queues
3-2Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Chapter 1 Basic ConceptsChapter 2 ArraysChapter 3 Stacks and QueuesChapter 4 Linked Lists Chapter 5 TreesChapter 6 Graph
Chapter 7 SortingChapter 8 HashingChapter 9 Heap StructuresChapter 10 Search Structures
Contents
3-3Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Data Objects: Ordered list or Linear list
A = (a1, a2,..., an)
n > 0 ai is an element or atomsn = 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
3-4Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
EDCBA
top
LIFO (Last In First Out)
bottom top ai is on top of ai-1
S = (a0, ..., an-1)
3-5Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Stack: a Last-In-First-Out (LIFO) list
BA
DCBA
CBA
DCBA
EDCBAtop
toptop
toptop
top
A
Basic Concepts: Stack (cont.)
Push (A) Push (B) Push (C) Push (D) Push (E) POP( ) = E
3-6Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
q r s t q r s t
Top/ CURRENT-ADDR
Basic Concepts: Stack Application
3-7Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
local variables (size?)
3-8Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
topstack
• CreateS• Push (Add)• Pop (Delete)• IsFull• IsEmpty• Top
3-9Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Operators on stack1. CreateS(S): create S as an empty stack2. Add(i, s):
3. Delete(S):
4. IsEmpty(S):
itop
top
top top
topTop(S)
top ‘i’ = Top(S)
Push(i,s)
Pop(S)
true if S = empty → top = -1false
var←‵i′
與 Pop 不一樣
Stack Operations
5. Top(S): return the top element of stack S
3-10Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
structure Stack is objects: a finite ordered list with zero or more elements. functions: for all stackStack, itemelement, 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
3-11Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Define data member Representing a stack using 1-dim array Stack CreateS(max_stack_size) ::=
#define MAX_STACK_SIZE 100typedef struct { int key; /*other fields */} element;element stack[MAX_STACK_SIZE];int top = -1;
. . .
top
Stack Implementation
0 1 2 n-1
3-12Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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];
3-13Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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-runPush
3-14Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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.)
3-15Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Queue: a First-In-First-Out (FIFO) list
3.2 The Queue Abstract Data Type
Addrearfront
A
Add
A B
rearfront
A B C Add
rearfront
A B C D
Add
rearfront
B C D E
Add
rearfront
B C DDelete
rearfront
3-16Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-17Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-18Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-19Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Queue Implementation Data member declaration:
By using 1-dimensional array Queue CreateQ(max_queue_size) ::=
#define MAX_QUEUE_SIZE 100typedef struct {
int key; /*other fields */
} element;element queue[MAX_QUEUE_SIZE];int rear = -1;int front = -1;
3-20Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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.)
3-21Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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;}
3-22Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
.
.
.
rear = 2
addq(&rear, 10); addq(&rear, 15); addq(&rear, 20);
201510queue[0]
queue[99]
queue[1]queue[2]
front = -1
Queue Implementation (cont.)
3-23Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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];}
3-24Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
values = deleteq(&front, rear); values = 10
.
.
.
rear = 22015
queue[0]
queue[99]
queue[1]queue[2]
front = 0
10
Queue Implementation (cont.)
3-25Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Job Scheduling
no priority priority 大 小 or
operations1. CREATEQ (Q)2. ADDQ (i,Q) : add to the rear3. DELETEQ (Q) : removes the front element 4. FRONT (Q) : return the front element of Q 5. IsEmpty (Q)
job1 job2 job4
job3 job5 job6
Front Rear
multi Queue 3.4
job2
job1
OS
multi programming
job2job1
batch system
Queue Application
3-26Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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=3 f=4 r=n
f=3 r=n
Jn+1
inserting Jobn+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
3-27Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
(Solution 2) Circular Queue Q (0:n-1)
J1, J2, J3, J4
3
2
10
n-2
n-1
fr
initial f = r = 0 f= r iff Circular Queue is empty
J4J3
J1
0n-1
f
r
J2
Add: if r== n-1 then r = 0 else r = r+1 r = (r+1) mod nDelete if f== n-1 then f = 0 else f = f+1 f = (f+1) mod n
Basic Concepts: Circular Queue
3-28Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-29Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-30Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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.)
3-31Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-32Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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.)
3-33Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
“Rat-in-a-maze” Fig 3.8 Array MAZE [1..m,1..p]
0: open path; 1: barrier
1 2 . . . p
1 0 1 0 0 . .
2 1 0 0 0 . .
. 0 1 1 0 . .
.
m
j
i
entrance
exit
3.3 A Mazing Problem: Stack Application
3-34Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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 01 1 0 1 0 0 1 0 1 1 1 1 1 1 10 0 1 1 0 1 1 1 0 1 0 0 1 0 10 1 1 1 1 0 0 1 1 1 1 1 1 1 10 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 00 0 1 1 1 1 1 0 0 0 1 1 1 1 00 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
3-35Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
NW -1 -1
.
. .
E 0 1
NE -1 1
N -1 0
moves move[d].vert move[d].horiz
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
3-36Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
| | | | | | |
| p |
| |
| m |
| |
| |
| | | | | | |
0 . . . p+1
0 0 0 0 0 0
. 0 .
.
m+1 0 0 . . .0
0: not yet visited
Array MAZE [0..m+1, 0..p+1]
MARK [0..m+1, 0..p+1]
A Mazing Problem
3-37Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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 / bT2 d * eT3 a * c
T4 T1 - cT5 T4 + T2X T5 – T4
“Machine” instructions
3-38Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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 2331
scanning 3 次
Evaluation of Expressions: the Problem
32
3-39Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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**CA
ED
A/B**CD*E
A/B**C
CA
A/B**C+D*EA*C
A/B**C+D*E A/B**C+D*E-A*CCBA
**
/
*
+
*
-
Stack
Evaluation of Expressions: Example 1
3-40Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-41Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-42Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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.)
3-43Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Example 2 A*(B+C)/Drule 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.)
3-44Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
bottom top top bottom
3.5 Multiple Stacks and Queues
m[0] m[1] … m[n-2] m[n-1]
bottommost bottommoststack 1 stack 2
3-45Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
More than two Stacks, n
if the sizes are known proportional distribution unknown equal segments
1 m
stack 1 stack 2 stack 3 stack 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
3-46Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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 segmentsboundary[stack_no]
0 stack_no < MAX_STACKStop[stack_no] 0 stack_no < MAX_STACKS
…
3-47Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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_SIZE1;
Initialize stacks:
Coexist n Stacks (cont.)
3-48Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-49Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-50Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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
3-51Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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.)
3-52Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin 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.)
3-53Been-Chian Chien, Wei-Pang Yang and Wen-Yang Lin Chapter 3 Stacks and Queues
Method 2: Proportional allocation
2 4 327.1
1129
41.41159
19.01119
25.21139
2 1 4 3
Stack_i Full (cont.)