Week 5 – Optional Exercises

1. Write a class LimitStack that extends the LinkedStack class we saw in the lecture. A LimitStack is like a LinkedStack, but is limited to a fixed capacity, which is an integer passed to the LimitStack constructor. If a LimitStack is full (i.e. the number of values in the stack equals its capacity) and the caller attempts to push another value, print an error message and do not push the value onto the stack.

2. Write a class OddStream that implements the IntStream interface we saw in the tutorial. An OddStream should transform a source stream by including only odd integers in the output.

3. Write a class ChopStream that implements the IntStream interface. ChopStream should have a constructor that takes a source IntStream and an integer k. A ChopStream should include all values from the source stream until the value k is seen, at which point it should produce no more values. For example, if the input stream is the integers [1, 2, 3, 4, 5, …] and k = 4, then ChopStream will produce [1, 2, 3].

4. Write a class AppendStream that implements IntStream. AppendStream's constructor should take two IntStreams s and t. The class should represent all elements from s followed by all elements from t.

5. Write a class AlterStream that implements IntStream. AlterStream's constructor should take two IntStreams s and t. The class should reprsent a stream taking elements of s and t alternately until either stream reaches its endpoint. For example, if the input streams contain the numbers [1, 2, 3, 4, 5, ...] and [10, 11, 12, 13], AlterStream should produce [1, 10, 2, 11, 3, 12, 4, 13, 5].

6. Write a class TwoStackQueue that implements the Queue interface. Your class should have a constructor that takes two stacks, i.e. objects that implement the Stack interface, and should use those stacks to implement the queue.

7. Write a class implementing a double-ended queue of integers that supports these operations:

All of these operations should run in constant time. Use a doubly-linked list.