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 generic data type Deque that implements 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 public void addLast(Item item) // insert the item at the end public Item removeFirst() // delete and return the item at the front public Item removeLast() // delete and return the item at the end public Iterator<Item> iterator() // return an iterator over items in order from front to end public static void main(String[] args) // unit testing }
Throw a java.lang.NullPointerException if the client attempts to add a null item; throw a java.util.NoSuchElementException if the client attempts to remove an item from an empty deque; throw a java.lang.UnsupportedOperationException if the client calls the remove() method in the iterator.
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 the operations next() and hasNext() (plus 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 implements 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 independent iterator over items in random order public static void main(String[] args) // unit testing }
Throw a java.lang.NullPointerException if the client attempts to add a null item; throw a java.util.NoSuchElementException if the client attempts to sample or dequeue an item from an empty randomized queue; throw a java.lang.UnsupportedOperationException if the client calls the remove() method in the iterator.
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 in the worst case, for some constant c. Additionally, your iterator implementation should support construction in time linear in the number of items and it should support the operations next() and hasNext() in constant worst-case time; you may use a linear amount of extra memory per iterator. The order of two or more iterators to the same randomized queue should be mutually independent; each iterator must maintain its own random order.
Clients. Write a client program to solve each of the following problems.
Subset must complete in linear time in the size of the input. You may use a Deque or a RandomizedQueue as well as a constant amount of extra memory.
% echo A B C D E F G H I | java Subset 3 % echo AA BB BB BB BB BB CC CC | java Subset 8 C BB G AA A BB CC % echo A B C D E F G H I | java Subset 3 BB E BB F CC G BB
Subset should implement the following API.
public class Subset { public static void main(String[] args) }
Palindrome must complete in linear time in the size of the input. You may use a Deque or a RandomizedQueue as well as a constant amount of extra memory.
% echo AAAACGTTTT | java Palindrome % echo AAAACTTTT | java Palindrome true false % echo AGCTAGCT | java Palindrome % echo agctagct | java Palindrome true false % echo GC | java Palindrome % echo AGCTTCGA | java Palindrome true false % echo | java Palindrome % echo A | java Palindrome true false
Palindrome should obey the following API:
public class Palindrome { public static void main(String[] args) }
Queueing theory provides a way to mathematically analyze lines (a.k.a. queues in other hemispheres). We have provided a ShoppingSim class which provides a interactive graphical simulation of one common queueing theory model known as the M/M/C model.
Your class should implement the following API. You may use a RandomizedQueue, a Deque, or any of the classes listed on the booksite under the Fundamentals section. Total memory usage must be linear in the number of customers. All methods should complete in constant time.
public class CheckoutLine { public CheckoutLine(double checkerSpeed) // checker public double timeToProcessNextItem() // already provided for you public Customer processItem() // item is removed from oldest customer's cart public void getInLine(Customer newCustomer) // a new customer is added to the line public Customer leaveLine() // most recent customer is removed from the line public int length() // returns length of the line public void draw(int thisLine, int totalLines) // provided for you }
Details about these methods can be found inside the incomplete CheckoutLine.java file. Once you have completed the CheckoutLine class, you can run the simulation using:
java ShoppingSim 10lines.ssconf
If the simulation runs too fast, then create a new .ssconf file with a larger deltaT.
The .ssconf file can also be used to specify other model parameters. Feel free to create (and submit) new .ssconf files.
Challenge for the bored. Improve the ShoppingSim simulator. For example, you might consider adding additional visualization or interactivity features. Particularly good modifications will be worth a small amount of extra extra credit.
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 stdlib.jar and Integer.parseInt(). Also submit the client programs Subset.java and Palindrome.java. If you complete the extra credit, then submit CheckoutLine.java, in addition to any other files you may have modified in the shopping simulator. Finally, submit a readme.txt file and answer the questions.