Chapter 3 Stacks and Queues
Chapter 3 Stacks and Queues
The Stack Abstract Data Type The Queue Abstract Data Type A Mazing Problem Evaluation of Expressions
old frame pointer return address local variables old frame pointer return address
fp al
fp main
return address
(a) (b) *Figure 3.2: System stack after function call a1 (p.103)
problem: there may be available space when IsFullQ is true i.e. movement is required.
As jobs enter and leave the system, the queue gradually shift
to right. In this case, queue_full should move the entire queue to the left so that the first element is again at queue[0], front is at -1, and rear is correctly positioned.
Shifting an array is very time-consuming, queue_full has a worst case complexity of O(MAX_QUEUE_SIZE).
3.2 (6/7)
Implementation 2:
rear:
3.2 (7/7)
is slightly more difficult since we must assure that a circular rotation occurs.
1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1
exit
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
If we are at position, maze[row][col], and we wish to find the position of the next move, maze[row][col], we set:
next_row = row + move[dir].vert; next_col = col + move[dir].horiz;
#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];
R1C1D1
Pop out
maze[1][1]: entrance
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1 1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1 1 1 1 0 0 1 1 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 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1 1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1
R1C9D2
R1C8D2 R2C7D1 R3C6D1
R3C5D2
R2C4D3 R1C5D5 R1C4D2 R1C3D2 R2C2D1
1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
R1C1D1 3
maze[15][11]: exit
Analysis:
m*p
mp m / 2 / p, or p / 2 / m
3.4 (3/14)
Precedence
hierarchy and associative for C
3.4 (8/14)
Evaluation of
Postfix Expression
6 2 / 3 - 4 2 * +
2
not an operator, isoperator,anoperator, not an an operator, not is an operator, is operator, an operator, notan not is is operator, the answeranoperator,anend of string, put into into stack put into the stackelements put pop theinto the put into the stack theput two elements two stack pop stack pop pop elements two elements two the stackpop of the the stack of theand get answer of stack of stack the stack
1 0 top
4*2 4 3 2
6/2-3+4*2 6/2-3 6/2 6
STACK
6/2-3 + 4*2
12
13 0 12 13 13
12
19 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};
(
* /
stack
output
a b c +
* d /
top
now, top must +1 -1
bottommost stack 1 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
bottommost stack 2
boundary[ 0] top[ 0]
boundary[1] top[ 1]
boundary[ 2] top[ 2]
boundary[n]
All stacks are empty and divided into roughly equal segments.
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.13:Delete an item from the stack stack-no (p.130) element delete (int i) { /* remove top element from the ith stack */ if (top [i] == boundary [i]) return stack_empty (i); return memory [ top [i]--]; }
b[0]
t[0]
b[1] t[1]
t[j]
b[j+1] b[n]
*Figure 3.19: Configuration when stack i meets stack i+1, but the memory is not full (p.130)