Question
This is java. Problem. (Deque) A double-ended queue or deque (pronounced deck) is a generalization of a stack and a queue that supports adding and
This is java.
Problem. (Deque)
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 data type Deque that implements the following API:
public class Deque- implements Iterable
- { 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) // add the item to the front public void addLast(Item item) // add the item to the end public Item removeFirst() // remove and return the item from the front public Item removeLast() // remove and return the item from the end public Iterator
- iterator() // return an iterator over items in order from front to end public static void main(String[] args) // unit testing (required) }
Corner cases. 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; 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 deque implementation must support each deque operation (including construction) in constant worst-case time and use space 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. Hint: you'll want to use a doubly-linked list.
import edu.princeton.cs.algs4.StdOut; import edu.princeton.cs.algs4.StdRandom; import java.util.Iterator; import java.util.NoSuchElementException;
// Deque implementation using a linked list. public class LinkedDeque
// Helper doubly-linked list class. private class Node { private Item item; private Node next; private Node prev; }
// Construct an empty deque. public LinkedDeque() { ... }
// Is the dequeue empty? public boolean isEmpty() { ... }
// The number of items on the deque. public int size() { ... }
// Add item to the front of the deque. public void addFirst(Item item) { ... }
// Add item to the end of the deque. public void addLast(Item item) { ... }
// Remove and return item from the front of the deque. public Item removeFirst() { ... }
// Remove and return item from the end of the deque. public Item removeLast() { ... }
// An iterator over items in the queue in order from front to end. public Iterator
public boolean hasNext() { ... }
public void remove() { throw new UnsupportedOperationException(); }
public Item next() { ... } }
// A string representation of the deque. public String toString() { StringBuilder s = new StringBuilder(); for (Item item : this) { s.append(item + " "); } return s.toString().substring(0, s.length() - 1); } // Test client. [DO NOT EDIT] public static void main(String[] args) { LinkedDeque
$ java 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started