Академический Документы
Профессиональный Документы
Культура Документы
arrays
linked lists
E.G.M. Petrakis
stacks, queues
Array Implementation
A simplified version of list
implementation
E.G.M. Petrakis
stacks, queues
Stack ADT
interface Stack {
public void clear( )
public void push(Object it)
public Object pop( )
public Object topValue( )
public bool isEmpty( )
}
E.G.M. Petrakis
stacks, queues
size
top
E.G.M. Petrakis
size
stacks, queues
E.G.M. Petrakis
stacks, queues
// Constructor
// Initialize stack
// Create header node
stacks, queues
Applications
Checking mathematical expressions
Equal number of left/right (, {, [, ), }, ]
((A+ B)
)A + B C)
- (A + B)) CA
-
E.G.M. Petrakis
stacks, queues
wrong
Algorithm
Read the expression from left to right
p: next symbol in expression
Repeat until (empty(stack) ) {
read(p);
if p = ( or { or [ then push(stack,p);
loop;
if p = ) or } or ] then c = pop(stack);
if c != p then error!!
}
if (not empty(stack) ) then error!!
E.G.M. Petrakis
stacks, queues
[
(
(
(
(
{x + (y [a + b]) * c [(d + e)]}/(h (j (k [l n]))
stacks, queues
More Applications
Evaluation of arithmetic expressions:
convert infix to postfix or prefix
evaluate postfix or prefix expression
E.G.M. Petrakis
stacks, queues
10
stacks, queues
11
: * > +
A+(B*C) : equivalent
+(C*) : B*C to postfix
ABC*+ : postfix
(A+B)*C : () > *
E.G.M. Petrakis
stacks, queues
12
: +C-
postfix
(A+B)*(C-D) : AB+CD-*
$*C-D+E/F/(G+H) : AB$C*D-EF/GH+/+
A-B/(C*D$E) : ABCDE$*/-
A+B-C
: -+ABC
prefix
(A+B)*(C-D) : *+AB-CD
A$B*C-D+E/F/(G+H) : +-*$ABCD//EF+GH
A-B/(C*D$E) : -A/B*C$DE
E.G.M. Petrakis
stacks, queues
13
output operands
push operators to stack
higher precedence operators must be above lower
precedence operators
before pushing an operator into the stack check the
precedence of operators already in the stack
E.G.M. Petrakis
stacks, queues
14
push ( in stack
output A
push + in stack
output B
dont push ), pop all operators
output +, ignore (, )
* push in stack
output C
output *
E.G.M. Petrakis
stacks, queues
15
Example: (A + B)*C
Symbol
(
A
+
B
)
*
E.G.M. Petrakis
Postfix
A
A
AB
AB+
AB+
AB+C
AB+C*
stacks, queues
Stack
(
(
(+
(+
*
*
16
Algorithm
stack = NULL;
while (not end of input) {
symbol = read next symbol;
if (symbol == operand) output(symbol);
else {
}
while not_empty(stack) {
top symbol = pop(stack);
output(top symbol);
}
E.G.M. Petrakis
stacks, queues
17
Evaluation of Postfix
While (not end of expression) {
read symbols from left to right;
result = 0
if (symbol == operand) push (stack);
else {
operand1 = pop(stack);
operand2 = pop(stack);
result = operand1 operator operand2;
push(stack, result);
}
}
result = pop(stack);
E.G.M. Petrakis
stacks, queues
18
Postfix: 6 2 3 + - 3 8 2 / + * 2 $ 3 +
Symbol Operand1Operand2 Value stack Operand
6
6
6,2
2
6,2,3
3
6,5
5
3
2
+
1
1
5
6
1,3
1
5
6
3
1,3,8
1
5
6
8
1,3,8,2
1
5
6
2
1,3,4
4
2
8
/
1,7
7
4
3
+
7
7
7
1
*
7,2
7
7
1
2
49
49
2
7
$
49,3
49
2
7
3
E.G.M. Petrakis
52
3stacks, queues 52
49
+
19
Queue
Queue elements may only be inserted
at the rear and removed from the
front
E.G.M. Petrakis
stacks, queues
20
front
A
B
E.G.M. Petrakis
B
C
rear
C
stacks, queues
enqueue (queue, D)
dequeue (queue)
21
Array Implementation
If the elements are the first 43
2
n elements of array and the
x
1
front element is at position 0 0 x
enqueue requires (1) operations but,
dequeue requires (n) operations (all
elements must be shifted)
Condition of empty queue?
E.G.M. Petrakis
stacks, queues
22
front = 4
rear = 4
E.G.M. Petrakis
front = 4
rear = 0
input A
stacks, queues
23
4
3
2
1
0
C
B
front = 4
rear = 2
4
3
2
1
0
D
C
B
front = 4
rear = 3
4
3
2
1
0
E
D
C
B
E.G.M. Petrakis
front = 4
rear = 4
stacks, queues
24
4
3
2
1
0
4
3
2
1
0
D
C
B
Delete
front = 0
rear = 3
Delete B
D
C
E.G.M. Petrakis
front = 1
rear = 3
4
3
2
1
0
stacks, queues
E
D
C
Insert E, F
Circular list
front = 2
rear = 0
25
Queue ADT
public interface Queue {
public
public
public
public
public
// Queue ADT
void clear();
// Remove all Objects from queue
void enqueue(Object it); // Enqueue Object at rear of queue
Object dequeue();
// Dequeue Object from front of queue
Object firstValue();
// Return value of top Object
boolean isEmpty();
// Return TRUE if stack is empty
E.G.M. Petrakis
stacks, queues
26
size
front
rear
E.G.M. Petrakis
size
stacks, queues
27
stacks, queues
28
front
rear
E.G.M. Petrakis
stacks, queues
29
// Constructor
// Constuctor: Ignore sz
// Initialize queue
stacks, queues
30
} // classes LQueue
E.G.M. Petrakis
stacks, queues
31
Priority Queue
Ascending: elements in any order but
dequeue removes the minimum
Descending: dequeue removes the
maximum
E.G.M. Petrakis
stacks, queues
32
Example: Dequeue
5
4
3
2
1
5
4
3
2
1
D
G
front = 5
rear = 4
ascending
D
G
E.G.M. Petrakis
front = 5
rear = 4
5
4
3
2
1
stacks, queues
descending
front = 5
rear = 4
?
33
Array Implementation
Problem: dequeue creates
empty slots
Solution(1): move elements:
(n) operations on dequeue
enqueue
E.G.M. Petrakis
5
4
3
2
1
5
4
3
2
1
stacks, queues
D
G
5
4
3
2
1
E
D
5
4
3
2
1
E
D
34