a. Open the reading on linear structures, the reading on stacks, and the reading on queues in separate tabs.
b. Fork and clone the repo.
Read through LinearStructureExpt.java
and LinkedStackExpt.java
.
Summarize what the stack should look like at each step. (A piece
of paper might help.) Note that the info
method will print
information on the stack (is it empty? is it full? what elements
are in the structure?) and the clear
method will repeatedly call
get
until the structure is empty.
Run LinkedStackExpt
and see if you get the output that
you expect.
Skim through ReportingLinearStructure.java
. Summarize the main
approach of the class. What ideas from the class might you apply
in other situations? (Pick at least one or two.)
The file ArrayBasedStack.java
has at least one significant bug.
Identify and correct any bugs you identify.
One useful application of stacks is matching things. For example, we can match the parens in a Scheme expression as follows:
Step through the characters in the expression
When you encounter an open paren or open square bracket, push it
on the stack
When you encounter a close paren or close square bracket, pop
the corresponding opening character off the stack. If the two
characters don't match, issue an error.
If you encounter a closing character with an empty stack, that
close paren or bracket is mismatched.
If the stack is not empty, there are unmatched open or closed parens.
Implement this algorithm. (You might also add support for braces
and angle brackets.) That is, write and experiment with a static
method, checkMatching(String str)
, that checks whether the parens,
square brackets, and potentially, other characters, match correctly.
You may find the following information useful.
'('
is character 40, ')'
is character 41.'<'
is character 60, '>'
is character 62.'['
is character 91, ']'
is character 93.'{'
is character 123, '}'
is character 125.The strategy for implementing a linked stack is relatively straightforward.
There are a few more subtleties, but those are the basics.
a. Sketch (that is, write down approximate code on paper, rather
than writing code on the computer) implementations of the five basic
linear-structure methods: put
, get
, peek
, isEmpty
, and
isFull
.
b. The file LinkedStack.java
provides an implementation of linked
stacks. Compare your notes to that one and suggest anything you’ve
found by comparing the two. (It may be that you realize you missed
something. It may be that you realize that our implementation is
incorrect.)
The strategy for implementing a linked queue is also relatively straightforward.
a. Sketch (that is, write down approximate code on paper, rather
than writing code on the computer) implementations of the five basic
linear-structure methods: put
, get
, peek
, isEmpty
, and
isFull
.
b. The file LinkedQueue
has a partial implementation of this approach.
Finish that implementation.
If you find that you finish the lab early, you might consider undertaking one or more of the following problems. You will likely want to consider them in order.
Revise your answer from the previous exercise to store the indices of matching symbols. That is, you will need to push both symbol and index. Use the indices to provide better error messages (e.g., you can say where the mismatch occurs in the string).
How can you store two kinds of values in stack? One option is to make it a stack of Objects, and alternately push Character and Integer objects. Another option is to create a simple class that groups a character and an integer.
Extend your answer from the previous extra problem to provide a nice picture of the matching parens. For example, for each pair of matching parens, you might draw a line underneath, as in the following.
(oh (boy) (I am having) ((so) much) fun matching (parens))
+---+
+-----------+
+--+
+---------+
+------+
+--------------------------------------------------------+