CS 201: Recursive Queue

This is an assignment to be done individually. You can share ideas and thoughts with other people in the class, but the code that you submit should be your own.

Implement a queue of Strings recursively. Specifically, create a class called RQueue that implements the following interface:

public interface QueueInt
{
    // Add item to the queue
    void offer(String item);

    // Return next item to come out of queue, without changing it.
    // If the queue is empty, return null
    String peek();

    // Remove the next item from the queue. Return true unless the
    // queue is empty; if it is empty, return false
    boolean remove();

    // Prints the contents of the queue to the screen in some cleanly
    // readable way.
    void display();
}

Your queue must be implemented as a recursive data structure. Here is a class definition to get you started:

public class RQueue implements QueueInt
{
    private String front;
    private String back;
    private RQueue inside;

    ...

The idea is that a queue can be thought of as a data structure containing a front, a back, and a queue within. For example, if my queue contained the Strings "first", "second", and "third" (offered in that order), I can think of that as a queue whose front is "first", whose rear is "third", and whose inside is another queue containing all of the inside values (which, in this case, is just "second").

You'll need to handle special cases regarding when your queue is empty, has only one item, and so on. You're free to do this as you wish, but I have hints here if you want to look. If you'd prefer the challenge of figuring it out yourself, don't look.

Part 1

Get offer, peek, and display working.

Part 2

Resubmit part 1, with remove working as well.

Additional for fun

If you're looking for an extra challenge, submit your queue again as a generic class, i.e. one that doesn't work just for Strings, but instead takes a type parameter.