1
STACKS AND QUEUES
UNIT 2
UNIT 2
CHAPTER 3 2
Token Operator Precedence Associativity
()
[]
-> .
Function call
Array element
Struct or union member
17
17
17
Left-to-right
i-- i++ Increment, decrement 16 Left-to-right
--I ++i
!
~
- +
& *
Sizeof
Decrement, increment
Logical not
One’s complement
Unary minus or plus
Address or indirection
Size (in bytes)
15 Right- to-left
(type) Type cast 14 Right- to-left
* / % Multiplicative 13 Left-to-right
+ - Binary add or subtract 12 Left-to-right
<< >> Shift 11 Left-to-right
CHAPTER 3 3
> >=
< <=
Relational 10 Left-to-
right
== != Equality 9 Left-to-
right
& Bitwise and 8 Left-to-
right
^ Bitwise exclusive or 7 Left-to-
right
| Bitwise or 6 Left-to-
right
&& Logical and 5 Left-to-
right
|| Logical or 4 Left-to-
right
?: Conditional 3 Right- to-
left
= += -= /= *=
%= <<= >>= &=
^= |=
Assignment 2 Right- to-
left
. comma 1 Left-to-
right
*Figure 3.12: Precedence hierarchy for C (p.130)
CHAPTER 3 4
Stack (stack: a Last-In-First-Out (LIFO) list )
Stack
An ordered list
Insertions and deletions are made at one end, called top
Illustration
Inserting and deleting elements in a stack
A
B
A
C
B
A
D
C
B
A
E
D
C
B
A
D
C
B
A
top
top
top
top
top
top
push push pop
CHAPTER 3 5
Some stack applications
Implementing recusive call
Expression evaluation
Infix to postfix
Postfix evaluation
Maze problem
Breadth First Search
……
CHAPTER 3 6
old frame pointer
return address
fp
main
fp
al
(a) (b)
*Figure 3.2: System stack after function call a1 (p.109)
old frame pointer
return address
local variables
old frame pointer
return address
an application of stack: stack frame of function call
system stack before a1 is invoked system stack after a1 is invoked
fp: a pointer to current stack frame
CHAPTER 3 7
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
abstract data type for stack
CHAPTER 3 8
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.
*ADT 3.1: Abstract data type Stack (p.109)
CHAPTER 3 9
Stack CreateS(max_stack_size) ::=
#define MAX_STACK_SIZE 100 /* maximum stack size */
typedef struct {
int key;
/* other fields */
} element;
element stack[MAX_STACK_SIZE];
int top = -1;
Boolean IsEmpty(Stack) ::= top< 0;
Boolean IsFull(Stack) ::= top >= MAX_STACK_SIZE-1;
Implementation: using array
CHAPTER 3 10
void add(int *top, element item)
{
if (*top >= MAX_STACK_SIZE-1) {
stack_full( );
return;
}
stack[++*top] = item;
}
*program 3.1: Add to a stack (p.111)
Add to a stack
CHAPTER 3 11
element delete(int *top)
{
if (*top == -1)
return stack_empty( ); /* returns and error key */
return stack[(*top)--];
}
*Program 3.2: Delete from a stack (p.111)
Delete from a stack
CHAPTER 3 12
Queue (Queue: a First-In-First-Out (FIFO) list)
Queue
An ordered list
All insertions take place at one end, rear
rear
All deletions take place at the opposite end, front
front
Illustration
A
B
A
C
B
A
D
C
B
A
D
C
B
rear
front
rear
front
rear
front
rear
front
rear
front
CHAPTER 3 13
Some queue applications
Job scheduling
Event list in simulator
Server and Customs
……
CHAPTER 3 14
front rear Q[0] Q[1] Q[2] Q[3] Comments
-1
-1
-1
-1
0
1
-1
0
1
2
2
2
J1
J1 J2
J1 J2 J3
J2 J3
J3
queue is empty
Job 1 is added
Job 2 is added
Job 3 is added
Job 1 is deleted
Job 2 is deleted
*Figure 3.5: Insertion and deletion from a sequential queue (p.117)
Application: Job scheduling
CHAPTER 3 15
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
Queue (ADT)
CHAPTER 3 16
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.
*ADT 3.2: Abstract data type Queue (p.115)
CHAPTER 3 17
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: using array
CHAPTER 3 18
void addq(int *rear, element item)
{
if (*rear == MAX_QUEUE_SIZE_1) {
queue_full( );
return;
}
queue [++*rear] = item;
}
*Program 3.3: Add to a queue (p.108) (1/e)
Add to a queue
CHAPTER 3 19
element deleteq(int *front, int rear)
{
if ( *front == rear)
return queue_empty( ); /* return an error key */
return queue [++ *front];
}
*Program 3.4: Delete from a queue(p.108) (1/e)
Delete from a queue
CHAPTER 3 20
*Figure 3.6: Empty and nonempty circular queues (p.117)
Implementation 2: regard an array as a circular queue
front: one position counterclockwise from the first element
rear: current end
front = 0
rear = 0
[0]
[1]
[2] [3]
[4]
[5]
Empty circular Queue
front = 0
rear = 3
[0]
[1]
[2] [3]
[4]
[5]
J1
J2 J3
Nonempty circular queue
CHAPTER 3 21
*Figure 3.7: Full circular queues and then we remove the item (p.110)
(1/e)
Problem: one space is left when queue is full
front = 0
rear = 5
[0]
[1]
[2] [3]
[4]
[5]
J1
J2 J3
J4
J5
front = 4
rear = 3
[0]
[1]
[2] [3]
[4]
[5]
J7
J8 J9
J6 J5
Full Circular queue
(waste one space )
CHAPTER 3 22
避免出現 rear=front 而無法分辨 circular
queue 是滿的 ? 還是空的 ? 所以最多存
放 Maxsize -1 個空間
或是加入一個 COUNT 變數表示 queue
的個數 COUNT=0 ( 空 )
COUNT=Maxsize ( 滿 )
CHAPTER 3 23
void addq(int front, int *rear, element item)
{
*rear = (*rear +1) % MAX_QUEUE_SIZE;
if (front == *rear) /* reset rear and print error */
return;
}
queue[*rear] = item;
}
*Program 3.5: Add to a circular queue (p.110) (1/e)
Add to a circular queue
CHAPTER 3 24
element deleteq(int* front, int rear)
{
element item;
if (*front == rear)
return queue_empty( );
/* queue_empty returns an error key */
*front = (*front+1) % MAX_QUEUE_SIZE;
return queue[*front];
}
*Program 3.6: Delete from a circular queue (p.111) (1/e)
Delete from a circular queue
CHAPTER 3 25
Evaluation of Expressions
Evaluating a complex expression in computer
((rear+1==front)||((rear==MaxQueueSize-1)&&!
front))
x= a/b- c+ d*e- a*c
Figuring out the order of operation within any
expression
A precedence hierarchy within any programming
language
See Figure 3.12
CHAPTER 3 26
Evaluation of Expressions (Cont.)
Ways to write expressions
Infix (standard)
Prefix
Postfix
compiler, a parenthesis-free notation
Infix Postfix
2+3*4 2 3 4*+
a*b+5 ab*5+
(1+2)*7 1 2+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*-
CHAPTER 3 27
Evaluation of Postfix Expressions
• Left-to-right scan Postfix expression,
1) Stack operands
operands until find an operator,
2) Meet operator, remove correct operands for
this operator,
3) Perform the operation,
4) Stack the result
• Remove the answer from the top of stack
CHAPTER 3 28
Token Stack
Stack Top
[0] [1] [2]
6 6 0
2 6 2 1
/ 6/2 0
3 6/2 3 1
- 6/2-3 0
4 6/2-3 4 1
2 6/2-3 4 2 2
* 6/2-3 4*2 1
+ 6/2-3+4*2 0
Postfix evaluation of 6 2/3-4 2*+
6 2/3-4 2*+
Evaluation of Postfix Expressions
CHAPTER 3 29
#define MAX_STACK_SIZE 100
#define MAX_EXPR_SIZE 100 /* max size of expression */
typedef enum{1paran, rparen, plus, minus, times, divide,
mod, eos, operand} precedence;
int stack[MAX_STACK_SIZE]; /* global stack */
char expr[MAX_EXPR_SIZE]; /* input string */
Assumptions:
operators: +, -, *, /, %
operands: single digit integer
CHAPTER 3 30
int eval(void)
{
precedence token;
char symbol;
int op1, op2;
int n = 0; /* counter for the expression string */
int top = -1;
token = get_token(&symbol, &n);
while (token != eos) {
if (token == operand)
add(&top, symbol-’0’); /* stack add */
exp: character array
CHAPTER 3 31
else {
/* remove two operands, perform operation, and
return result to the stack */
op2 = delete(&top); /* stack delete */
op1 = delete(&top);
switch(token) {
case plus: add(&top, op1+op2); break;
case minus: add(&top, op1-op2); break;
case times: add(&top, op1*op2); break;
case divide: add(&top, op1/op2); break;
case mod: add(&top, op1%op2);
}
}
token = get_token (&symbol, &n);
}
return delete(&top); /* return result */
}
*Program 3.9: Function to evaluate a postfix expression (p.122)
CHAPTER 3 32
precedence get_token(char *symbol, int *n)
{
*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;
}
}
*Program 3.10: Function to get a token from the input string (p.123)
CHAPTER 3 33
Infix to Postfix
1) Method I
1) Fully parenthesize the expression
2) Move all binary operators so that they replace their
corresponding right parentheses
3) Delete all parentheses
 Examples:a/b-c+d*e-a*c
 ((((a/b)-c)+(d*e))-(a*c)), fully parentheses
 ab/
/c-
-de*+
*+ac*-
*-, replace right parentheses and delete
all parentheses
 Disadvantage
 inefficient, two passes
CHAPTER 3 34
Infix to Postfix
 Method II
1) scan the infix expression left-to-right
2) output operand encountered
3) output operators depending on their precedence, i.e.,
higher precedence operators first
 Example: a+b*c
a+b*c, simple expression
Token Stack Top Output
[0] [1] [2]
a -1 a
+ + 0 a
b + 0 ab
* + * 1 ab
c + * 1 abc
eos -1 abc*+
CHAPTER 3 35
Infix to Postfix
Example: a*(b+c)*d , parenthesized expression
Token Stack Top Output
[0] [1] [2]
a -1 a
* * 0 a
( * ( 1 a
b * ( 1 ab
+ * ( + 2 ab
c * ( + 2 abc
) * 0 abc+
* * 0 abc+*
d * 0 abc+*d
eos * 0 abc+*d*
CHAPTER 3 36
Infix to Postfix
Last two examples suggests a precedence-based scheme
for stacking and unstacking operators
isp (in-stack precedence)
icp (in-coming precedence)
precedence stack[MaxStackSize];
/* isp and icp arrays - index is value of precedence
lparen, rparen, plus, minus, time 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};
 See program 3.11- (n)
CHAPTER 3 37
void postfix(void)
{
/* output the postfix of the expression. The expression
string, the stack, and top are global */
char symbol;
precedence token;
int n = 0;
int top = 0; /* place eos on stack */
stack[0] = eos;
for (token = get _token(&symbol, &n); token != eos;
token = get_token(&symbol, &n)) {
if (token == operand)
printf (“%c”, symbol);
else if (token == rparen ){
CHAPTER 3 38
/*unstack tokens until left parenthesis */
while (stack[top] != lparen)
print_token(delete(&top));
delete(&top); /*discard the left parenthesis */
}
else{
/* remove and print symbols whose isp is greater
than or equal to the current token’s icp */
while(isp[stack[top]] >= icp[token] )
print_token(delete(&top));
add(&top, token);
}
}
while ((token = delete(&top)) != eos)
print_token(token);
print(“n”);
}
*Program 3.11: Function to convert from infix to postfix (p.126)
(n)
f(n)=(g(n)) iff there exist
positive
constants c1, c2, and n0 such
that c1g(n)f(n)c2g(n) for all
n, nn0.
f(n)=(g(n)) iff g(n) is both an
upper and lower bound on f(n).
CHAPTER 3 39
*Figure 3.17: Infix and postfix expressions (p.127)
(1) evaluation
(2) transformation
後序優於中序 :
去除運算子優先權 , 結合性和括號
方便 complier 計算運算子的值 , 掃描一次便可求結果
Infix Prefix
a*b/c
a/b-c+d*e-a*c
a*(b+c)/d-g
/*abc
-+-/abc*de*ac
-/*a+bcdg
CHAPTER 3 40
Multiple stacks and queues
Two stacks
m[0], m[1], …, m[n-2], m[n-1]
bottommost bottommost
stack 1 stack 2
More than two stacks (n)
memory is divided into n equal segments
boundary[stack_no]
0  stack_no < MAX_STACKS
top[stack_no]
0  stack_no < MAX_STACKS
CHAPTER 3 41
boundary[ 0] boundary[1] boundary[ 2] boundary[n]
top[ 0] top[ 1] top[ 2]
All stacks are empty and divided into roughly equal segments.
*Figure 3.18: Initial configuration for n stacks in memory [m]. (p.140)
0 1 [ m/n ] 2[ m/n ] m-1
Initially, boundary[i]=top[i].
CHAPTER 3 42
#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 */
p.139
top[0] = boundary[0] = -1;
for (i = 1; i < n; i++)
top[i] =boundary[i] =(MEMORY_SIZE/n)*i;
boundary[n] = MEMORY_SIZE-1;
p.139
CHAPTER 3 43
void add(int i, element item)
{
/* add an item to the ith stack */
if (top[i] == boundary [i+1])
stack_full(i); may have unused storage
memory[++top[i]] = item;
}
*Program 3.12:Add an item to the stack stack-no (p.129)
element delete(int i)
{
/* remove top element from the ith stack */
if (top[i] == boundary[i])
return stack_empty(i);
return memory[top[i]--];
}
*Program 3.13:Delete an item from the stack stack-no (p.130)
CHAPTER 3 44
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] b[i+2]
b=boundary, t=top
*Figure 3.19: Configuration when stack i meets stack i+1,
but the memory is not full (p.141)
Find j, stack_no < j < n
such that top[j] < boundary[j+1]
or, 0  j < stack_no
meet
往左或右找一個空間
( 往左 )
( 往右 )
CHAPTER 3 45
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
*Figure 3.8: An example maze(p.123)
A Mazing Problem
1: blocked path 0: through path
CHAPTER 3 46
NW
[row-1][col-1]
N
[row-1][col]
NE
[row-1][col+1]
W
[row][col-1] 
[row][col]
E
[row][col+1]
SW
[row+1][col-1]
S
[row+1][col]
SE
[row+1][col+1]
*Figure 3.9: Allowable moves (p.124)
a possible representation
CHAPTER 3 47
typedef struct {
short int vert;
short int horiz;
} offsets;
offsets move[8]; /*array of moves for each direction*/
a possible implementation
Name Dir move[dir].vert move[dir].horiz
N
NE
E
SE
S
SW
W
NW
0
1
2
3
4
5
6
7
-1
-1
0
1
1
1
0
-1
0
1
1
1
0
-1
-1
-1
next_row = row + move[dir].vert;
next_col = col + move[dir].horiz;
*Figure 3.10 : Table of moves
(p.124)
CHAPTER 3 48
#define MAX_STACK_SIZE 100
/* maximum stack size */
typedef struct {
short int row;
short int col;
short int dir;
} element;
element stack[MAX_STACK_SIZE];
Use stack to keep pass history
CHAPTER 3 49
Initialize a stack to the maze’s entrance coordinates and direction to north;
while (stack is not empty){
/* move to position at top of stack */
<row, col, dir> = delete from top of stack;
while (there are more moves from current position) {
<next_row, next_col > = coordinates of next move;
dir = direction of move;
if ((next_row == EXIT_ROW)&& (next_col == EXIT_COL))
success;
if (maze[next_row][next_col] == 0 &&
mark[next_row][next_col] == 0) {
CHAPTER 3 50
/* legal move and haven’t been there */
mark[next_row][next_col] = 1;
/* save current position and direction */
add <row, col, dir> to the top of the stack;
row = next_row;
col = next_col;
dir = north;
}
}
}
printf(“No path foundn”);
*Program 3.11: Initial maze algorithm (p.126)
CHAPTER 3 51
000001
111110
100001
011111
100001
111110
100001
011111
100000
*Figure 3.11: Simple maze with a long path (p.127)
The size of a stack?
m*p
CHAPTER 3 52
void path (void)
{
/* output a path through the maze if such a path exists */
int i, row, col, next_row, next_col, dir, found = FALSE;
element position;
mark[1][1] = 1; top =0;
stack[0].row = 1; stack[0].col = 1; stack[0].dir = 1;
while (top > -1 && !found) {
position = delete(&top);
row = position.row; col = position.col;
dir = position.dir;
while (dir < 8 && !found) {
/*move in direction dir */
next_row = row + move[dir].vert;
next_col = col + move[dir].horiz;
CHAPTER 3 53
if (next_row==EXIT_ROW && next_col==EXIT_COL)
found = TRUE;
else if ( !maze[next_row][next_col] &&
!mark[next_row][next_col] {
mark[next_row][next_col] = 1;
position.row = row; position.col = col;
position.dir = ++dir;
add(&top, position);
row = next_row; col = next_col; dir = 0;
}
else ++dir;
}
}
CHAPTER 3 54
if (found) {
printf(“The path is :n”);
printf(“row coln”);
for (i = 0; i <= top; i++)
printf(“ %2d%5d”, stack[i].row, stack[i].col);
printf(“%2d%5dn”, row, col);
printf(“%2d%5dn”, EXIT_ROW, EXIT_COL);
}
else printf(“The maze does not have a pathn”);
}
*Program 3.12:Maze search function (p.128)

STACKS AND QUEUES in Data Structures.ppt

  • 1.
  • 2.
    CHAPTER 3 2 TokenOperator Precedence Associativity () [] -> . Function call Array element Struct or union member 17 17 17 Left-to-right i-- i++ Increment, decrement 16 Left-to-right --I ++i ! ~ - + & * Sizeof Decrement, increment Logical not One’s complement Unary minus or plus Address or indirection Size (in bytes) 15 Right- to-left (type) Type cast 14 Right- to-left * / % Multiplicative 13 Left-to-right + - Binary add or subtract 12 Left-to-right << >> Shift 11 Left-to-right
  • 3.
    CHAPTER 3 3 >>= < <= Relational 10 Left-to- right == != Equality 9 Left-to- right & Bitwise and 8 Left-to- right ^ Bitwise exclusive or 7 Left-to- right | Bitwise or 6 Left-to- right && Logical and 5 Left-to- right || Logical or 4 Left-to- right ?: Conditional 3 Right- to- left = += -= /= *= %= <<= >>= &= ^= |= Assignment 2 Right- to- left . comma 1 Left-to- right *Figure 3.12: Precedence hierarchy for C (p.130)
  • 4.
    CHAPTER 3 4 Stack(stack: a Last-In-First-Out (LIFO) list ) Stack An ordered list Insertions and deletions are made at one end, called top Illustration Inserting and deleting elements in a stack A B A C B A D C B A E D C B A D C B A top top top top top top push push pop
  • 5.
    CHAPTER 3 5 Somestack applications Implementing recusive call Expression evaluation Infix to postfix Postfix evaluation Maze problem Breadth First Search ……
  • 6.
    CHAPTER 3 6 oldframe pointer return address fp main fp al (a) (b) *Figure 3.2: System stack after function call a1 (p.109) old frame pointer return address local variables old frame pointer return address an application of stack: stack frame of function call system stack before a1 is invoked system stack after a1 is invoked fp: a pointer to current stack frame
  • 7.
    CHAPTER 3 7 structureStack 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 abstract data type for stack
  • 8.
    CHAPTER 3 8 BooleanIsEmpty(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. *ADT 3.1: Abstract data type Stack (p.109)
  • 9.
    CHAPTER 3 9 StackCreateS(max_stack_size) ::= #define MAX_STACK_SIZE 100 /* maximum stack size */ typedef struct { int key; /* other fields */ } element; element stack[MAX_STACK_SIZE]; int top = -1; Boolean IsEmpty(Stack) ::= top< 0; Boolean IsFull(Stack) ::= top >= MAX_STACK_SIZE-1; Implementation: using array
  • 10.
    CHAPTER 3 10 voidadd(int *top, element item) { if (*top >= MAX_STACK_SIZE-1) { stack_full( ); return; } stack[++*top] = item; } *program 3.1: Add to a stack (p.111) Add to a stack
  • 11.
    CHAPTER 3 11 elementdelete(int *top) { if (*top == -1) return stack_empty( ); /* returns and error key */ return stack[(*top)--]; } *Program 3.2: Delete from a stack (p.111) Delete from a stack
  • 12.
    CHAPTER 3 12 Queue(Queue: a First-In-First-Out (FIFO) list) Queue An ordered list All insertions take place at one end, rear rear All deletions take place at the opposite end, front front Illustration A B A C B A D C B A D C B rear front rear front rear front rear front rear front
  • 13.
    CHAPTER 3 13 Somequeue applications Job scheduling Event list in simulator Server and Customs ……
  • 14.
    CHAPTER 3 14 frontrear Q[0] Q[1] Q[2] Q[3] Comments -1 -1 -1 -1 0 1 -1 0 1 2 2 2 J1 J1 J2 J1 J2 J3 J2 J3 J3 queue is empty Job 1 is added Job 2 is added Job 3 is added Job 1 is deleted Job 2 is deleted *Figure 3.5: Insertion and deletion from a sequential queue (p.117) Application: Job scheduling
  • 15.
    CHAPTER 3 15 structureQueue 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 Queue (ADT)
  • 16.
    CHAPTER 3 16 BooleanIsEmptyQ(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. *ADT 3.2: Abstract data type Queue (p.115)
  • 17.
    CHAPTER 3 17 QueueCreateQ(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: using array
  • 18.
    CHAPTER 3 18 voidaddq(int *rear, element item) { if (*rear == MAX_QUEUE_SIZE_1) { queue_full( ); return; } queue [++*rear] = item; } *Program 3.3: Add to a queue (p.108) (1/e) Add to a queue
  • 19.
    CHAPTER 3 19 elementdeleteq(int *front, int rear) { if ( *front == rear) return queue_empty( ); /* return an error key */ return queue [++ *front]; } *Program 3.4: Delete from a queue(p.108) (1/e) Delete from a queue
  • 20.
    CHAPTER 3 20 *Figure3.6: Empty and nonempty circular queues (p.117) Implementation 2: regard an array as a circular queue front: one position counterclockwise from the first element rear: current end front = 0 rear = 0 [0] [1] [2] [3] [4] [5] Empty circular Queue front = 0 rear = 3 [0] [1] [2] [3] [4] [5] J1 J2 J3 Nonempty circular queue
  • 21.
    CHAPTER 3 21 *Figure3.7: Full circular queues and then we remove the item (p.110) (1/e) Problem: one space is left when queue is full front = 0 rear = 5 [0] [1] [2] [3] [4] [5] J1 J2 J3 J4 J5 front = 4 rear = 3 [0] [1] [2] [3] [4] [5] J7 J8 J9 J6 J5 Full Circular queue (waste one space )
  • 22.
    CHAPTER 3 22 避免出現rear=front 而無法分辨 circular queue 是滿的 ? 還是空的 ? 所以最多存 放 Maxsize -1 個空間 或是加入一個 COUNT 變數表示 queue 的個數 COUNT=0 ( 空 ) COUNT=Maxsize ( 滿 )
  • 23.
    CHAPTER 3 23 voidaddq(int front, int *rear, element item) { *rear = (*rear +1) % MAX_QUEUE_SIZE; if (front == *rear) /* reset rear and print error */ return; } queue[*rear] = item; } *Program 3.5: Add to a circular queue (p.110) (1/e) Add to a circular queue
  • 24.
    CHAPTER 3 24 elementdeleteq(int* front, int rear) { element item; if (*front == rear) return queue_empty( ); /* queue_empty returns an error key */ *front = (*front+1) % MAX_QUEUE_SIZE; return queue[*front]; } *Program 3.6: Delete from a circular queue (p.111) (1/e) Delete from a circular queue
  • 25.
    CHAPTER 3 25 Evaluationof Expressions Evaluating a complex expression in computer ((rear+1==front)||((rear==MaxQueueSize-1)&&! front)) x= a/b- c+ d*e- a*c Figuring out the order of operation within any expression A precedence hierarchy within any programming language See Figure 3.12
  • 26.
    CHAPTER 3 26 Evaluationof Expressions (Cont.) Ways to write expressions Infix (standard) Prefix Postfix compiler, a parenthesis-free notation Infix Postfix 2+3*4 2 3 4*+ a*b+5 ab*5+ (1+2)*7 1 2+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*-
  • 27.
    CHAPTER 3 27 Evaluationof Postfix Expressions • Left-to-right scan Postfix expression, 1) Stack operands operands until find an operator, 2) Meet operator, remove correct operands for this operator, 3) Perform the operation, 4) Stack the result • Remove the answer from the top of stack
  • 28.
    CHAPTER 3 28 TokenStack Stack Top [0] [1] [2] 6 6 0 2 6 2 1 / 6/2 0 3 6/2 3 1 - 6/2-3 0 4 6/2-3 4 1 2 6/2-3 4 2 2 * 6/2-3 4*2 1 + 6/2-3+4*2 0 Postfix evaluation of 6 2/3-4 2*+ 6 2/3-4 2*+ Evaluation of Postfix Expressions
  • 29.
    CHAPTER 3 29 #defineMAX_STACK_SIZE 100 #define MAX_EXPR_SIZE 100 /* max size of expression */ typedef enum{1paran, rparen, plus, minus, times, divide, mod, eos, operand} precedence; int stack[MAX_STACK_SIZE]; /* global stack */ char expr[MAX_EXPR_SIZE]; /* input string */ Assumptions: operators: +, -, *, /, % operands: single digit integer
  • 30.
    CHAPTER 3 30 inteval(void) { precedence token; char symbol; int op1, op2; int n = 0; /* counter for the expression string */ int top = -1; token = get_token(&symbol, &n); while (token != eos) { if (token == operand) add(&top, symbol-’0’); /* stack add */ exp: character array
  • 31.
    CHAPTER 3 31 else{ /* remove two operands, perform operation, and return result to the stack */ op2 = delete(&top); /* stack delete */ op1 = delete(&top); switch(token) { case plus: add(&top, op1+op2); break; case minus: add(&top, op1-op2); break; case times: add(&top, op1*op2); break; case divide: add(&top, op1/op2); break; case mod: add(&top, op1%op2); } } token = get_token (&symbol, &n); } return delete(&top); /* return result */ } *Program 3.9: Function to evaluate a postfix expression (p.122)
  • 32.
    CHAPTER 3 32 precedenceget_token(char *symbol, int *n) { *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; } } *Program 3.10: Function to get a token from the input string (p.123)
  • 33.
    CHAPTER 3 33 Infixto Postfix 1) Method I 1) Fully parenthesize the expression 2) Move all binary operators so that they replace their corresponding right parentheses 3) Delete all parentheses  Examples:a/b-c+d*e-a*c  ((((a/b)-c)+(d*e))-(a*c)), fully parentheses  ab/ /c- -de*+ *+ac*- *-, replace right parentheses and delete all parentheses  Disadvantage  inefficient, two passes
  • 34.
    CHAPTER 3 34 Infixto Postfix  Method II 1) scan the infix expression left-to-right 2) output operand encountered 3) output operators depending on their precedence, i.e., higher precedence operators first  Example: a+b*c a+b*c, simple expression Token Stack Top Output [0] [1] [2] a -1 a + + 0 a b + 0 ab * + * 1 ab c + * 1 abc eos -1 abc*+
  • 35.
    CHAPTER 3 35 Infixto Postfix Example: a*(b+c)*d , parenthesized expression Token Stack Top Output [0] [1] [2] a -1 a * * 0 a ( * ( 1 a b * ( 1 ab + * ( + 2 ab c * ( + 2 abc ) * 0 abc+ * * 0 abc+* d * 0 abc+*d eos * 0 abc+*d*
  • 36.
    CHAPTER 3 36 Infixto Postfix Last two examples suggests a precedence-based scheme for stacking and unstacking operators isp (in-stack precedence) icp (in-coming precedence) precedence stack[MaxStackSize]; /* isp and icp arrays - index is value of precedence lparen, rparen, plus, minus, time 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};  See program 3.11- (n)
  • 37.
    CHAPTER 3 37 voidpostfix(void) { /* output the postfix of the expression. The expression string, the stack, and top are global */ char symbol; precedence token; int n = 0; int top = 0; /* place eos on stack */ stack[0] = eos; for (token = get _token(&symbol, &n); token != eos; token = get_token(&symbol, &n)) { if (token == operand) printf (“%c”, symbol); else if (token == rparen ){
  • 38.
    CHAPTER 3 38 /*unstacktokens until left parenthesis */ while (stack[top] != lparen) print_token(delete(&top)); delete(&top); /*discard the left parenthesis */ } else{ /* remove and print symbols whose isp is greater than or equal to the current token’s icp */ while(isp[stack[top]] >= icp[token] ) print_token(delete(&top)); add(&top, token); } } while ((token = delete(&top)) != eos) print_token(token); print(“n”); } *Program 3.11: Function to convert from infix to postfix (p.126) (n) f(n)=(g(n)) iff there exist positive constants c1, c2, and n0 such that c1g(n)f(n)c2g(n) for all n, nn0. f(n)=(g(n)) iff g(n) is both an upper and lower bound on f(n).
  • 39.
    CHAPTER 3 39 *Figure3.17: Infix and postfix expressions (p.127) (1) evaluation (2) transformation 後序優於中序 : 去除運算子優先權 , 結合性和括號 方便 complier 計算運算子的值 , 掃描一次便可求結果 Infix Prefix a*b/c a/b-c+d*e-a*c a*(b+c)/d-g /*abc -+-/abc*de*ac -/*a+bcdg
  • 40.
    CHAPTER 3 40 Multiplestacks and queues Two stacks m[0], m[1], …, m[n-2], m[n-1] bottommost bottommost stack 1 stack 2 More than two stacks (n) memory is divided into n equal segments boundary[stack_no] 0  stack_no < MAX_STACKS top[stack_no] 0  stack_no < MAX_STACKS
  • 41.
    CHAPTER 3 41 boundary[0] boundary[1] boundary[ 2] boundary[n] top[ 0] top[ 1] top[ 2] All stacks are empty and divided into roughly equal segments. *Figure 3.18: Initial configuration for n stacks in memory [m]. (p.140) 0 1 [ m/n ] 2[ m/n ] m-1 Initially, boundary[i]=top[i].
  • 42.
    CHAPTER 3 42 #defineMEMORY_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 */ p.139 top[0] = boundary[0] = -1; for (i = 1; i < n; i++) top[i] =boundary[i] =(MEMORY_SIZE/n)*i; boundary[n] = MEMORY_SIZE-1; p.139
  • 43.
    CHAPTER 3 43 voidadd(int i, element item) { /* add an item to the ith stack */ if (top[i] == boundary [i+1]) stack_full(i); may have unused storage memory[++top[i]] = item; } *Program 3.12:Add an item to the stack stack-no (p.129) element delete(int i) { /* remove top element from the ith stack */ if (top[i] == boundary[i]) return stack_empty(i); return memory[top[i]--]; } *Program 3.13:Delete an item from the stack stack-no (p.130)
  • 44.
    CHAPTER 3 44 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] b[i+2] b=boundary, t=top *Figure 3.19: Configuration when stack i meets stack i+1, but the memory is not full (p.141) Find j, stack_no < j < n such that top[j] < boundary[j+1] or, 0  j < stack_no meet 往左或右找一個空間 ( 往左 ) ( 往右 )
  • 45.
    CHAPTER 3 45 01 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 *Figure 3.8: An example maze(p.123) A Mazing Problem 1: blocked path 0: through path
  • 46.
    CHAPTER 3 46 NW [row-1][col-1] N [row-1][col] NE [row-1][col+1] W [row][col-1] [row][col] E [row][col+1] SW [row+1][col-1] S [row+1][col] SE [row+1][col+1] *Figure 3.9: Allowable moves (p.124) a possible representation
  • 47.
    CHAPTER 3 47 typedefstruct { short int vert; short int horiz; } offsets; offsets move[8]; /*array of moves for each direction*/ a possible implementation Name Dir move[dir].vert move[dir].horiz N NE E SE S SW W NW 0 1 2 3 4 5 6 7 -1 -1 0 1 1 1 0 -1 0 1 1 1 0 -1 -1 -1 next_row = row + move[dir].vert; next_col = col + move[dir].horiz; *Figure 3.10 : Table of moves (p.124)
  • 48.
    CHAPTER 3 48 #defineMAX_STACK_SIZE 100 /* maximum stack size */ typedef struct { short int row; short int col; short int dir; } element; element stack[MAX_STACK_SIZE]; Use stack to keep pass history
  • 49.
    CHAPTER 3 49 Initializea stack to the maze’s entrance coordinates and direction to north; while (stack is not empty){ /* move to position at top of stack */ <row, col, dir> = delete from top of stack; while (there are more moves from current position) { <next_row, next_col > = coordinates of next move; dir = direction of move; if ((next_row == EXIT_ROW)&& (next_col == EXIT_COL)) success; if (maze[next_row][next_col] == 0 && mark[next_row][next_col] == 0) {
  • 50.
    CHAPTER 3 50 /*legal move and haven’t been there */ mark[next_row][next_col] = 1; /* save current position and direction */ add <row, col, dir> to the top of the stack; row = next_row; col = next_col; dir = north; } } } printf(“No path foundn”); *Program 3.11: Initial maze algorithm (p.126)
  • 51.
    CHAPTER 3 51 000001 111110 100001 011111 100001 111110 100001 011111 100000 *Figure3.11: Simple maze with a long path (p.127) The size of a stack? m*p
  • 52.
    CHAPTER 3 52 voidpath (void) { /* output a path through the maze if such a path exists */ int i, row, col, next_row, next_col, dir, found = FALSE; element position; mark[1][1] = 1; top =0; stack[0].row = 1; stack[0].col = 1; stack[0].dir = 1; while (top > -1 && !found) { position = delete(&top); row = position.row; col = position.col; dir = position.dir; while (dir < 8 && !found) { /*move in direction dir */ next_row = row + move[dir].vert; next_col = col + move[dir].horiz;
  • 53.
    CHAPTER 3 53 if(next_row==EXIT_ROW && next_col==EXIT_COL) found = TRUE; else if ( !maze[next_row][next_col] && !mark[next_row][next_col] { mark[next_row][next_col] = 1; position.row = row; position.col = col; position.dir = ++dir; add(&top, position); row = next_row; col = next_col; dir = 0; } else ++dir; } }
  • 54.
    CHAPTER 3 54 if(found) { printf(“The path is :n”); printf(“row coln”); for (i = 0; i <= top; i++) printf(“ %2d%5d”, stack[i].row, stack[i].col); printf(“%2d%5dn”, row, col); printf(“%2d%5dn”, EXIT_ROW, EXIT_COL); } else printf(“The maze does not have a pathn”); } *Program 3.12:Maze search function (p.128)