Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 implements Iterable { ...

// 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 iterator() { ... } // An iterator, doesn't implement remove() since it's optional. private class DequeIterator implements Iterator { ... DequeIterator() { ... }

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 deque = new LinkedDeque(); String quote = "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"; int r = StdRandom.uniform(0, quote.length()); for (int i = quote.substring(0, r).length() - 1; i >= 0; i--) { deque.addFirst(quote.charAt(i)); } for (int i = 0; i < quote.substring(r).length(); i++) { deque.addLast(quote.charAt(r + i)); } StdOut.println(deque.isEmpty()); StdOut.printf("(%d characters) ", deque.size()); for (char c : deque) { StdOut.print(c); } StdOut.println(); double s = StdRandom.uniform(); for (int i = 0; i < quote.length(); i++) { if (StdRandom.bernoulli(s)) { deque.removeFirst(); } else { deque.removeLast(); } } StdOut.println(deque.isEmpty()); } }

$ 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions

Question

What is the role of communication (Chapter 4) in leadership?

Answered: 1 week ago