Professional Documents
Culture Documents
// ------------------------------
B
A
main
Push (mains ar) Push (As ar) Runtime Stack Push (Bs ar) Use info in Top ar to return control to A Pop Use info in Top ar to return control to main Pop Use info in Top ar to return control to OS Pop
General Algorithm ( ( { x x x } ) x [ ] x x )
get next symbol set balanced flag to true while (there are more input symbols and expression still balanced) if (next symbol is opening symbol) Push symbol onto stack else if (next symbol is closing symbol) if (stack is empty) set balanced to false else use Top to get copy of opening symbol on top of stack Pop the stack if (opening symbol does not match closing symbol) set balanced to false else ignore symbol get next symbol if (balanced and stack is empty) well-formed else ill-formed
{
[ ( ( Stack
. . .
[0] items
-1 top
. . .
70 items
[0]
0 top
. . .
28 70 items
[0]
1 top
. . .
88 28 70 items
[0]
2 top
. . .
88 28 70 items
[0]
1 top
. . .
95 28 70 items
[0]
2 top
. . .
95 28 70 items
[0]
1 top
. . .
95 28 70 items
[0]
0 top
. . .
95 28 70 items
[0]
-1 top
Want to minimize customer waiting time Want to minimize server idle time
But now front is at position[1] , not [0] Need to shift remaining items down!
Keep track of both front and rear Note: queue is empty when (front 1) == rear
Keep track of both front and rear Note: queue is empty when (front 1) == rear
Keep track of both front and rear Note: queue is empty when (front 1) == rear
Keep track of both front and rear Note: queue is empty when (front 1) == rear
Keep track of both front and rear Note: queue is empty when (front 1) == rear
Keep track of both front and rear Note: queue is empty when (front 1) == rear
Keep track of both front and rear Note: queue is empty when (front 1) == rear
Keep track of both front and rear Note: queue is empty when (front 1) == rear
Keep track of both front and rear Note: queue is empty when (front 1) == rear
Keep track of both front and rear Note: queue is empty when (front -1) == rear
rear
Enqueue (A)
rear
rear
rear
rear
Enqueue (A) Enqueue (B) Enqueue (C) Dequeue (ch) Dequeue (ch)
Enqueue (A) Enqueue (B) Enqueue (C) Dequeue (ch) Dequeue (ch) Dequeue (ch)