4 Stack Queue
4 Stack Queue
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
return address
local variables
return address
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
EMPTY QUEUE
[2] [3] [2] [3]
J2 J3
front = 0 front = 0
rear = 0 rear = 3
J6 J5
J5
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;
}
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]
element stack[MAX_STACK_SIZE];
Initialize a stack to the maze’s entrance coordinates and
direction to north;
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
precedence token;
char symbol;
int op1, op2;
int n = 0; /* counter for the expression string */
int top = -1;
*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 */
}
}
/ - *+ * -
(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.
( ) + - * / % eos
isp 0 19 12 12 13 13 13 0
icp 20 19 12 12 13 13 13 0
precedence stack[MAX_STACK_SIZE];
static int isp [ ] = {0, 19, 12, 12, 13, 13, 13, 0};
static int icp [ ] = {20, 19, 12, 12, 13, 13, 13, 0};
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
bottommost bottommost
stack 1 stack 2
All stacks are empty and divided into roughly equal segments.