Data Structures and Algorithms
with Object-Oriented Design Patterns in Java
in the following ways:
.
so that it accepts expressions
written in prefix (Polish) notation.
Hint: See Exercise
.
to use a stack of infix expressions.
The expressions can be represented as instances of
the Str class defined in Program
.
A binary operator should pop two strings from the stack
and then push a string which is formed by concatenating
the operator and its operands in the correct order.
For example,
suppose the operator is ``*''
and the two strings popped from the stack
are "(b+c)" and "a".
Then the result that gets pushed onto the stack
is the string "a*(b+c)".
so that it immediately evaluates the infix expression.
That is, create an infixCalculator method
in the style of Program
.(, ), [, ], , and .
We say that S is balanced if it has one of the following forms:
).public class MultipleStack
implements Container
{
public MultipleStack (int numberOfStacks);
public void push (Object object, int whichStack);
public Object pop (int whichStack);
// ...
};
.
,
the DequeAsArray class extends the QueueAsArray class.
This is the design paradigm known as
generalization .
The alternative paradigm is specialization
in which the QueueAsArray extends the DequeAsArray class.
Redesign the DequeAsArray and QueueAsArray
components of the class hierarchy using specialization.
and then do a breadth-first traversal of the tree
in reverse (see Exercise
).
For example, the expression
Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.