CSCE 3110 Data Structures & Algorithm Analysis: Stacks and Queues Reading: Chap.3 Weiss
CSCE 3110 Data Structures & Algorithm Analysis: Stacks and Queues Reading: Chap.3 Weiss
E top
D top D D
C top C C C
B top B B B B
A top A A A A A
Stack Applications
Real life
Pile of books
Plate trays
More applications related to computer science
Program execution stack (read more from your text)
Evaluating expressions
Stack ADT
push O(?)
pop O(?)
isEmpty O(?)
isFull O(?)
a = 4, b = c = 2, d = e = 3
Interpretation 1:
((4/2)-2)+(3*3)-(4*2)=0 + 8+9=1
Interpretation 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
Token Operator Precedence1 Associativity
bitwise or 6 left-to-right
logical or 4 left-to-right
?: conditional 3 right-to-left
= += -= assignment 2 right-to-left
/= *= %=
<<= >>=
&= ^= =
, comma 1 left-to-right
user compiler
Infix Postfix
2+3*4 234*+
a*b+5 ab*5+
(1+2)*7 12+7*
a*b/c ab*c/
(a/(b-c+d))*(e-a)*c abc-d+/ea-*c*
a/b-c+d*e-a*c ab/c-de*+ac*-
*symbol =expr[(*n)++];
switch (*symbol) {
case ‘(‘ : return lparen;
case ’)’ : return rparen;
case ‘+’: return plus;
case ‘-’ : return minus;
case ‘/’ : return divide;
case ‘*’ : return times;
case ‘%’ : return mod;
case ‘\0‘ : return eos;
default : return operand;
/* no error checking, default is operand */
}
}
Infix to Postfix Conversion
(Intuitive Algorithm)
(1) Fully parenthesized expression
a / b - c + d * e - a * c -->
((((a / b) - c) + (d * e)) – (a * c))
/ - +* -*
(3) Delete all parentheses.
ab/c-de*+ac*-
two passes
The orders of operands in infix and postfix are the same.
a + b * c, * > +
( ) + - * / % eos
isp 0 19 12 12 13 13 13 0
icp 20 19 12 12 13 13 13 0
precedence stack[MAX_STACK_SIZE];
/* isp and icp arrays -- index is value of precedence
lparen, rparen, plus, minus, times, divide, mod, eos */
static int isp [ ] = {0, 19, 12, 12, 13, 13, 13, 0};
static int icp [ ] = {20, 19, 12, 12, 13, 13, 13, 0};
D rear
C rear C D rear
B rear B B C
rear front A front
A A front A front B
front
Applications: Job Scheduling
front rear Q[0] Q[1] Q[2] Q[3] Comments
-1 -1 queue is empty
-1 0 J1 Job 1 is added
-1 1 J1 J2 Job 2 is added
-1 2 J1 J2 J3 Job 3 is added
0 2 J2 J3 Job 1 is deleted
1 2 J3 Job 2 is deleted
Queue ADT
objects: a finite ordered list with zero or more elements.
methods:
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 Enqueue(queue, item) ::=
if (IsFullQ(queue)) queue_full
else insert item at rear of queue and return queue
Queue ADT (cont’d)
Implementation 1
Implementation 2
Implementation 1:
createQ, isEmptyQ, isFullQ
Queue createQ(max_queue_size) ::=
# define MAX_QUEUE_SIZE 100/* Maximum queue size */
typedef struct {
int key;
/* other fields */
} element;
element queue[MAX_QUEUE_SIZE];
int rear = -1;
int front = -1;
Boolean isEmpty(queue) ::= front == rear
Boolean isFullQ(queue) ::= rear == MAX_QUEUE_SIZE-1
Implementation 1:
enqueue
front = 0 front = 0
rear = 0 rear = 3
J5 J6 J5
front =0 front =4
rear = 5 rear =3
How to test when queue is empty?
How to test when queue is full?
Enqueue in a Circular Queue
enqueue O(?)
dequeue O(?)
size O(?)
isEmpty O(?)
isFull O(?)