Professor Abdul-Quader
Lesson 3
next
, hasNext
, and remove
.
Node
class is an inner class. Why?@Test
above.Stack:
Queue:
Exercise: Describe (pseudocode / English / to each other) implementations in which all the operations listed above are
Warning: never use the built-in java.util.Stack class. There are better implementations: in particular, ArrayDeque and LinkedList.
Some problems to think about. Try to find the best algorithms to solve these problems. I don’t expect you to do all of these now, or even to implement any of them, but do your best to come up with an algorithm that would solve it (at least on paper). Then we can discuss some of our solutions on Thursday.
Given a singly LinkedList, how might you implement a reverse method? Moreover, how might you do this in-place: that is, without using a secondary list or additional storage?
Given a singly LinkedList, how might you find the middle element in one loop? (ie in N steps, not in 2N steps).
Suppose we have an array of Iterators. Implement an Iterator which “collates” these. That is, suppose we have iterators for the following lists:
Then the result of
Iterator<Integer> iter = new CollatedIterator(iterArray);
while (iter.hasNext()) {
int i = iter.next();
System.out.println(i);
}
would be 0, 3, 5, 1, 4, 6, 2, 7, 8, 9 (on separate lines, without commas).
The following strings of brackets are perfectly matched:
The following are not:
Devise an algorithm which, given a string of brackets, determines if the brackets are perfectly matched.
Devise an algorithm which, given an input