COS 226 Programming Assignment

Randomized Queues and Deques

Write a generic data type for a deque and a randomized queue. The goal of this assignment is to implement elementary data structures using arrays and linked lists, and to introduce you to generics and iterators.

Dequeue. A double-ended queue or deque (pronounced "deck") is a generalization of a stack and a queue that supports inserting and removing items from either the front or the back of the data structure. Create a data type Deque that supports the following API.

public class Deque<Item> implements Iterable<Item> {
   public Deque()                    // construct an empty deque
   public boolean isEmpty()          // is the deque empty?
   public int size()                 // return the number of items on the deque
   public void addFirst(Item item)   // insert the item at the front of the queue
   public void addLast(Item item)    // insert the item at the end of the queue
   public Item removeFirst()         // delete and return the first item in queue
   public Item removeLast()          // delete and return the last item in queue
   public Iterator<Item> iterator()  // return an iterator that examines the 
                                     // items in order from front to back
}

Throw a RuntimeException if the client attempts to remove an item from an empty deque.

Your deque implementation should support each deque operation in constant worst-case time and use space proportional to the number of items currently in the deque. Additionally, your iterator implementation should support each iteration operation (including construction) in constant worst-case time, and use a constant amount of extra space per iterator.

Randomized queue. A randomized queue is similar to a stack or queue, except that the item removed is chosen uniformly at random from items in the data structure. Create a generic data type RandomizedQueue that supports the following API.

public class RandomizedQueue<Item> implements Iterable<Item> {
   public RandomizedQueue()          // construct an empty randomized queue
   public boolean isEmpty()          // is the queue empty?
   public int size()                 // return the number of items on the queue
   public void enqueue(Item item)    // add the item
   public Item dequeue()             // delete and return a random item
   public Item sample()              // return (but do not delete) a random item
   public Iterator<Item> iterator()  // return an iterator that returns the items in random order
}

Throw a RuntimeException if the client attempts to dequeue or sample from an empty randomized queue.

Your randomized queue implementation should support each randomized queue operation (besides creating an iterator) in constant amortized time and use space proportional to the number of items currently in the queue. That is, any sequence of M randomized queue operations (starting from an empty queue) should take at most cM steps for some constant c. Additionally, your iterator implementation should support each iteration operation (excluding construction) in constant worst-case time, and use a linear amount of extra space per iterator.

Clients. Write a client program to solve each of the following problems. Use one variable of either type Deque or RandomizedQueue in each client. Use generics to avoid casting in your clients.

Deliverables. Submit the data types RandomizedQueue.java and Deque.java. Each data type should include its own main() that thoroughly tests the associated operations. You may not call any library functions other than those in our standard library and Integer.parseInt(). Also submit the client programs Subset.java and Palindrome.java. Finally, submit a readme.txt file and answer the questions.