Project 2 Solution

$40.00 $34.00

This project will help you to understand how to implement elementary data structures using arrays and linked lists and will introduce you to generics and iterators. Problem1. (LinkedDeque.java) Introduction. A double-ended queue or deque (pronounced “deck”) is a generalization of a stack and a queue that supports adding and removing items from either the front…

4.5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:
Tags:

Description

4.5/5 – (2 votes)

This project will help you to understand how to implement elementary data structures using arrays and linked lists and will introduce you to generics and iterators.

Problem1. (LinkedDeque.java)

Introduction. A double-ended queue or deque (pronounced “deck”) is a generalization of a stack and a queue that supports adding and removing items from either the front or the back of the data structure. Create a generic iterable data type LinkedDeque<Item> in LinkedDeque.java that uses a linked list to implement the following deque API:

Corner Cases.

  1. Throw a java.lang.NullPointerException if the client attempts to add a null item

  1. Throw a java.util.NoSuchElementException if the client attempts to remove an item from an empty deque

  1. Throw a java.lang.UnsupportedOperationException if the client calls the remove() method in the iterator

  1. Throw a java.util.NoSuchElementException if the client calls the next() method in the iterator and there are no more items to return..

Performance Requirement. Your deque implementation must support each deque operation (including construction) in constant worst-case time and use space proportional to linear in the number of items currently in the deque. Additionally, your iterator implementation must support each operation (including construction) in constant worst-case time.

CS146 Project 2

Sample output:

$ java-algs4 LinkedDeque

false

(364 characters ) There is grandeur in this view of life , with its several powers , having been originally breathed into a few forms or into one ; and that , whilst this planet has gone cycling on according to the fixed law of gravity , from so simple a beginning endless forms most beautiful and most wonderful have been , and are being , evolved . ~ Charles Darwin , The Origin of Species true

Problem2. (ResizingArrayRandomQueue.java)

Introduction. A random 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 iterable data type ResizingArrayRandomQueue<Item> in ResizingArrayRandomQueue.java that uses a resizing array to implement the following random queue API:

The order of two or more iterators to the same randomized queue must be mutually independent; each iterator must maintain its own random order.

Corner Cases.

  1. Throw a java.lang.NullPointerException if the client attempts to add a null item

  1. Throw a java.util.NoSuchElementException if the client attempts to sample or dequeue an item from an empty randomized queue

  1. Throw a java.lang.UnsupportedOperationException if the client calls the remove() method in the iterator

  1. Throw a java.util.NoSuchElementException if the client calls the next() method in the iterator and there are no more items to return

Performance requirements. Your randomized queue implementation must support each randomized queue operation (besides creating an iterator) in constant amortized time and use

CS146 Project 2

space proportional to linear in the number of items currently in the queue. That is, any sequence of M randomized queue operations (starting from an empty queue) must take at most cM steps in the worst case, for some constant c. Additionally, your iterator implementation must support next() and hasNext() in constant worst-case time and construction in linear time; you may use a linear amount of extra memory per iterator.

Sample output:

$ seq 1 100 | java-algs4 ResizingArrayRandomQueue

5050

0

5050

Problem3. (Subset.java)

Introduction. Write a client program Subset.java that takes a command-line integer k , reads in a sequence of strings from standard input using StdIn.readString() , and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 k N , where N is the number of string on standard input. The running time of the program must be linear in the size of the input. You may use only a constant amount of memory plus either one LinkedDeque or ResizingArrayRandomQueue object of maximum size at most N. For an extra challenge, limit the maximum size to k.

Sample output:

$ java Subset 8

  1. BB BB BB BB BB CC CC <ctrl-d>

BB CC AA BB BB BB

Project 2 Solution
$40.00 $34.00