Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is java. Deque. A double-ended queue or deque (pronounced \"deck\") is a ge

ID: 3592848 • Letter: T

Question

This is java.

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, based on the LinkedQueue.java code from the previous lab, that implements the following API:

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. It is possible to implement such a deque with a circular array (beyond the scope of this class), but here, we'll do it with a doubly-linked list (in which each node has a "next" and "prev" link). If you completed your Deque implementation in the previous lab, you may reuse it here.

Your Deque implementation should include an iterator that supports each operation (including construction) in constant worst-case time. Your main function should include code that tests this with a sequence of add and remove actions.

Explanation / Answer

// Java program to demonstrate working of

// Deque in Java

import java.util.*;

public class DequeExample

{

    public static void main(String[] args)

    {

        Deque deque = new LinkedList<>();

        // We can add elements to the queue in various ways

        deque.add("Element 1 (Tail)"); // add to tail

        deque.addFirst("Element 2 (Head)");

        deque.addLast("Element 3 (Tail)");

        deque.push("Element 4 (Head)"); //add to head

        deque.offer("Element 5 (Tail)");

        deque.offerFirst("Element 6 (Head)");

        deque.offerLast("Element 7 (Tail)");

        System.out.println(deque + " ");

        // Iterate through the queue elements.

        System.out.println("Standard Iterator");

        Iterator iterator = deque.iterator();

        while (iterator.hasNext())

            System.out.println(" " + iterator.next());

        // Reverse order iterator

        Iterator reverse = deque.descendingIterator();

        System.out.println("Reverse Iterator");

        while (reverse.hasNext())

            System.out.println(" " + reverse.next());

        // Peek returns the head, without deleting

        // it from the deque

        System.out.println("Peek " + deque.peek());

        System.out.println("After peek: " + deque);

        // Pop returns the head, and removes it from

        // the deque

        System.out.println("Pop " + deque.pop());

        System.out.println("After pop: " + deque);

        // We can check if a specific element exists

        // in the deque

        System.out.println("Contains element 3: " +

                        deque.contains("Element 3 (Tail)"));

        // We can remove the first / last element.

        deque.removeFirst();

        deque.removeLast();

        System.out.println("Deque after removing " +

                            "first and last: " + deque);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote