0% found this document useful (0 votes)
52 views

4 Stack Queue

A maze is represented as a 2D array where 1s represent blocked paths and 0s represent open paths. The goal is to find a path from the entrance to the exit. Stacks and queues can be used to solve the maze problem. A stack implements depth-first search where the program explores as far as possible along each branch before backtracking. A queue implements breadth-first search where the program explores all neighboring paths from the starting point before moving to paths farther away. The maze is represented by allowing movement in 8 directions from each cell - north, northeast, east, southeast, south, southwest, west, northwest. Possible next positions are calculated based on the current position and direction of movement.

Uploaded by

ALBERT ZHANG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

4 Stack Queue

A maze is represented as a 2D array where 1s represent blocked paths and 0s represent open paths. The goal is to find a path from the entrance to the exit. Stacks and queues can be used to solve the maze problem. A stack implements depth-first search where the program explores as far as possible along each branch before backtracking. A queue implements breadth-first search where the program explores all neighboring paths from the starting point before moving to paths farther away. The maze is represented by allowing movement in 8 directions from each cell - north, northeast, east, southeast, south, southwest, west, northwest. Possible next positions are calculated based on the current position and direction of movement.

Uploaded by

ALBERT ZHANG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Chapter 3

STACKS AND QUEUES


All the programs in this file are selected from
Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed
“Fundamentals of Data Structures in C”,
Stacks
3.1; page 107 - 111
stack: a Last-In-First-Out (LIFO)
list
Pop

E top
D top D D top
C top C C C
B top B B B B
A top A A A A A

*Figure 3.1: Inserting and deleting elements in a stack (p.108)


An application of stack: stack
frame of function call (activation record)
fp: a pointer to current stack frame

Previous frame pointer

return address

local variables

Previous frame pointer

return address

system stack before a1 is invoked system stack after a1 is invoked


(a) (b)
*Figure 3.2: System stack after function call (p.109)
abstract data type for stack
ADT 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 Push(stack, item) ::=
if (IsFull(stack)) stackFull
else insert item into top of stack and return
Boolean IsEmpty(stack) ::=
if(stack == CreateS(max_stack_size))
return TRUE
else return FALSE

Element Pop(stack) ::=


if(IsEmpty(stack)) return
else remove and return the item on the top
of the stack.

*Structure 3.1: Abstract data type Stack (p.110)


Implementation: using array
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;
Add to a stack
void push(int *top, element item)
{
/* add an item to the global stack */
if (*top >= MAX_STACK_SIZE-1) {
stackFull( );
return;
}
stack[++*top] = item;
}
*program 3.1: Add to a stack (p.111)
Delete from a stack

element pop(int *top)


{
/*deleat and return the top element from the stack */
if (*top == -1)
return stackEmpty( ); /* returns and error key */
return stack[(*top)--];
}

*Program 3.2: Delete from a stack (p.111)


Queues
3.3; PAGE 114
Queue:
a First-In-First-Out (FIFO) list
D rear D rear
C rear C C
B Rear B B B front
rear
A front A front A front A front A

*Figure 3.4: Inserting and deleting elements in a queue (p.114)


Application: Job scheduling
front rear Q[0] Q[1] Q[2] Q[3] Comments
-1 -1 queue is empty front = = rear
-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

*Figure 3.5: Insertion and deletion from a sequential queue (p.117)


Abstract data type of queue
ADT 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.

*ADT 3.2: Abstract data type Queue (p.115)


Implementation 1: using array
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
Add to a queue
void addq(int *rear, element item)
{
/* add an item to the queue */
if (*rear == MAX_QUEUE_SIZE - 1) {
queue_full( );
return;
}
queue [++*rear] = item;
}

*Program 3.5: Add to a queue (p.116)


D rear ==max-queue size-1
C
Delete from a queue front

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];
}

*Program 3.4: Delete from a queue(p.108)

problem: there may be available space when IsFullQ is true


I.E. movement is required.
Implementation 2:
regard an array as a circular queue
front: one position counterclockwise from the first element
rear: current end

EMPTY QUEUE
[2] [3] [2] [3]
J2 J3

[1] [4] [1] J1 [4]

[0] [5] [0] [5]

front = 0 front = 0
rear = 0 rear = 3

*Figure 3.6: Empty and nonempty circular queues (p.117)


Problem: one space is left when queue is full

FULL QUEUE FULL QUEUE Delete J1, J2, J3, J4

Add J6, J7, J8, J9

[2] [3] [2] [3]


J2 J3 J8 J9

[1] J1 J4 [4] [1] J7 [4]

J6 J5
J5

[0] [5] [0] [5]

front =0 front =4
rear = 5 rear =3

*Figure 3.7: Full circular queues and then we remove the item (p.117)
Add to a circular queue
void addq(int front, int *rear, element item)
{
/* add an item to the queue */
*rear = (*rear +1) % MAX_QUEUE_SIZE;
if (front == *rear) /* reset rear and print error */
return;
}
queue[*rear] = item;
}

*Program 3.7: Add to a circular queue (p.118)


Delete from a circular queue
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];
}
*Program 3.8: Delete from a circular queue (p.119)
3.5 A Mazing
Problem
PAGE 120
A Mazing Problem

entrance 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 exit
1: blocked path 0: through path
*Figure 3.8: An example maze(p.123)
a possible representation
NW N NE
[row-1][col-1] [row-1][col] [row-1][col+1]

W E
[row][col-1]  [row][col+1]
[row][col]
SW S SE
[row+1][col-1] [row+1][col] [row+1][col+1]

*Figure 3.9: Allowable moves (p.127)


(next_row , next_col)

a possible implementation North


(row , col)

next_row = row + move[dir].vert;


typedef struct { next_col = col + move[dir].horiz;
short int vert;
short int horiz; Name Dir move[dir].vert move[dir].horiz
} offsets;
N 0 -1 0
offsets move[8];
/*array of moves for NE 1 -1 1
each direction*/ E 2 0 1
SE 3 1 1
S 4 1 0
SW 5 1 -1
W 6 0 -1
NW 7 -1 -1
Use stack to keep pass history
#define MAX_STACK_SIZE 100
/*maximum stack size*/

typedef struct { (row, col, dir)


short int row;
short int col; back
short int dir; new move
} element;

element stack[MAX_STACK_SIZE];
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) {
/* 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;
}
} <row, col>
} SE
printf(“No path found\n”);
<next_row, next_col>
*Program 3.11: Initial maze algorithm (p.126)
The size of a stack?
0 0 0 0 0 1
1 1 1 1 1 0
1 0 0 0 0 1
0 1 1 1 1 1
1 0 0 0 0 1
1 1 1 1 1 0
1 0 0 0 0 1
0 1 1 1 1 1 m*p
1 0 0 0 0 0
*Figure 3.11: Simple maze with a long path (p.127)
(1,1)
Entrance (m,p)

void path (void) (m+2)*(p+2)


{
/* 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); 0
row = position.row; col = position.col; 7 N 1
dir = position.dir; 6W E2
5 S 3
while (dir < 8 && !found) { 4
/*move in direction dir */
next_row = row + move[dir].vert;
next_col = col + move[dir].horiz;
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;
}
}
if (found) {
printf(“The path is :\n”);
printf(“row col\n”);
for (i = 0; i <= top; i++)
printf(“ %2d%5d”, stack[i].row, stack[i].col);
printf(“%2d%5d\n”, row, col);
printf(“%2d%5d\n”, EXIT_ROW, EXIT_COL);
}
else printf(“The maze does not have a path\n”);
}

*Program 3.12:Maze search function (p.128)


Evaluation of
Expressions
3.6; PAGE 127
Evaluation of Expressions
X=a/b-c+d*e-a*c
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

() function call 17 left-to-right


[] array element
-> . struct or union member
-- ++ increment, decrement2 16 left-to-right

-- ++ decrement, increment3 15 right-to-left


! logical not
- one’s complement
-+ unary minus or plus
&* address or indirection
sizeof size (in bytes)
(type) type cast 14 right-to-left

*/% mutiplicative 13 Left-to-right


+- binary add or subtract 12 left-to-right

<< >> shift 11 left-to-right

> >= 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

1.The precedence column is taken from Harbison and Steele.


2.Postfix form
3.Prefix form

*Figure 3.12: Precedence hierarchy for C (p.130)


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*-

*Figure 3.13: Infix and postfix notation (p.131)

Postfix: no parentheses, no precedence


1. Infix to postfix conversion
2. Postfix evaluation
Token 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

*Figure 3.14: Postfix evaluation (p.131)


Postfix Evaluation
Assumptions:
operators: +, -, *, /, %
operands: single digit integer
#define MAX_STACK_SIZE 100 /* maximum stack size */
#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 */
int eval(void)
{
/* evaluate a postfix expression, expr, maintained as a
global variable, ‘\0’ is the end of the expression.
The stack and top of the stack are global variables.
get_token is used to return the token type and
the character symbol. Operands are assumed to be single
character digits */

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) exp: character array
push(&top, symbol-’0’); /* stack insert */
else {
/* remove two operands, perform operation, and
return result to the stack */
op2 = pop(&top); /* stack delete */
op1 = pop(&top);
switch(token) {
case plus: push(&top, op1+op2); break;
case minus: push(&top, op1-op2); break;
case times: push(&top, op1*op2); break;
case divide: push(&top, op1/op2); break;
case mod: push(&top, op1%op2);
}
}
token = getToken (&symbol, &n);
}
return pop(&top); /* return result */
}

*Program 3.13: Function to evaluate a postfix expression (p.133)


precedence getToken(char *symbol, int *n)
{
/* get the next token, symbol is the character
representation, which is returned, the token is
represented by its enumerated value, which
is returned in the function name */

*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 */
}
}

*Program 3.14: Function to get a token from the input string


(p.134)
Infix to Postfix Conversion
(Intuitive Algorithm)
(1) Fully parenthesized expression
a / b - c + d * e - a * c -->
((((a / b) - c) + (d * e)) - a * c))

(2) All operators replace their corresponding right


parentheses.
((((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, * > +
Token Stack Top Output
[0] [1] [2]
a -1 a
+ + 0 a
b + 0 ab
* + * 1 ab
c +precedence
* 1 abc
eos small large -1 abc*+
*Figure 3.15: Translation of a+b*c to postfix (p.135)
a *1 (b +c) *2 d
Token Stack Top Output
[0] [1] [2]
a -1 a
*1 *1 0 a
( *1 ( 1 a
b *1 ( 1 ab
+ *1 ( + 2 ab
c *1 ( + 2 abc
) *1 match ) 0 abc+
*2 *2 *1 = *2 0 abc+*1
d *2 0 abc+*1d
eos *2 0 abc+*1d*2
* Figure 3.16: Translation of a*(b+c)*d to postfix (p.135)
possibly more than one
Rules
(1) Operators are taken out of the stack as long as their
in-stack precedence (ISP) is higher than or equal to the
incoming precedence (ICP) of the new operator.

(2) ( has low in-stack precedence, and high incoming


precedence.

( ) + - * / % 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};

isp: in-stack precedence


icp: incoming precedence
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 = getToken(&symbol, &n); token != eos;


token = geToken(&symbol, &n)) {
if (token == operand)
printf (“%c”, symbol);
else if (token == rparen ){
/*unstack tokens until left parenthesis */
while (stack[top] != lparen)
printToken(pop(&top));
pop(&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] )
printToken(pop(&top));
f(n)=(g(n)) iff there exist positive
push(&top, token); constants c1, c2, and n0 such
} that c1g(n)f(n)c2g(n) for all
} //for n, nn0.
while ((token = pop(&top)) != eos)
printToken(token);
print(“\n”); f(n)=(g(n)) iff g(n) is both an
} (n) upper and lower bound on f(n).

*Program 3.15: Function to convert from infix to postfix (p.137)


Infix Prefix

a*b/c /*abc
a/b-c+d*e-a*c -+-/abc*de*ac
a*(b+c)/d-g -/*a+bcdg

(1) evaluation
(2) transformation

*Figure 3.17: Infix and postfix expressions (p.138)


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
Initially, boundary[i]=top[i].

0 1 [ m/n ] 2[ m/n ] m-1

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.129)


#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)
void push(int i, element item)
{
/* add an item to the ith stack */
if (top[i] == boundary [i+1])
stackFull(i); may have unused storage
memory[++top[i]] = item;
}

*Program 3.16:Add an item to the stack stack-no (p.140)


element pop(int i)
{
/* remove top element from the ith stack */
if (top[i] == boundary[i])
return stackEmpty(i);
return memory[top[i]--];
}

*Program 3.17:Delete an item from the stack stack-no (p.141)

You might also like