3-1 Been-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 Type 3.2 The Queue Abstract Data Type 3.3 A Mazing Problem 3.4 Evaluation of Expressions 3.5 Multiple Stacks and Queues
53
Embed
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.
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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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){
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
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
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
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 */