Question: java question public interface Deque { void addFirst(T item); void addLast(T item); T removeFirst(); T removeLast(); T getFirst(); T getLast(); boolean contains(T item); int size();

java question

java question public interface Deque { void addFirst(T item); void addLast(T item);

T removeFirst(); T removeLast(); T getFirst(); T getLast(); boolean contains(T item); int

public interface Deque { void addFirst(T item); void addLast(T item); T removeFirst(); T removeLast(); T getFirst(); T getLast(); boolean contains(T item); int size(); String toString(); } 
 /* * test program for deques */ public class Deques { public static void main (String [] args) { try { Deque words = new ArrayDeque(); //Deque words = new LinkedDeque(); // adding to front (empty list) System.out.println(" adding 'hello' to front"); words.addFirst("hello"); System.out.println(words); // adding to front System.out.println(" adding 'well' to front"); words.addFirst("well"); System.out.println(words); // adding to end System.out.println(" adding 'world' to end"); words.addLast("world"); System.out.println(words); // retrieving from front System.out.println(" getting first item"); System.out.println(words.getFirst()); // retrieving from end System.out.println(" getting last item"); System.out.println(words.getLast()); // removing first item System.out.println(" removing first item"); words.removeFirst(); System.out.println(words); // removing last item System.out.println(" removing last item"); words.removeLast(); System.out.println(words); // removing last item (list of one) System.out.println(" removing last item"); words.removeLast(); System.out.println(words); // removing last item (empty list) System.out.println(" trying to remove last item"); System.out.println(words.removeLast()); // adding to end (empty list) System.out.println(" adding 'hello' to end"); words.addLast("hello"); System.out.println(words); // removing first item (list of one) System.out.println(" removing first item"); words.removeFirst(); System.out.println(words); // removing first item (empty list) System.out.println(" trying to remove first item"); System.out.println(words.removeFirst()); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } } } 

 /* * ArrayDeque * * array implementation of a deque * * add/remove/retrieve first item at both ends */ public class ArrayDeque implements Deque { public static final int DEFAULT_CAPACITY = 10; private T [] collection; private int size; public ArrayDeque() { this(DEFAULT_CAPACITY); } @SuppressWarnings("unchecked") public ArrayDeque(int capacity) { if (capacity  index; i--) { collection[i-1] = collection[i]; } } } 

/* * LinkeaDeque * * linked implementation of a deque * * add/remove/retrieve first item at both ends */ public class LinkedDeque implements Deque { private class Node { private T data; private Node next; public Node(T item) { data = item; next = null; } } private Node head; private Node tail; private int size; public LinkedDeque() { head = null; tail = null; size = 0; } public void addFirst (T item) { } public void addLast (T item) { } public T removeFirst() { } public T removeLast() { } public T getFirst() { return head.data; } public T getLast() { return tail.data; } public boolean contains(T item) { Node current = head; for (int i = 0; i  

In class we implemented two specialized types of list: a stack (items added/removed from top) and a queue (items added to end, removed from front). A double-ended queue (deque) allows items to be added/removed from both ends (and thus is underlyingly like a double-ended stack). Write the following methods for both ArrayDeque and LinkedDeque void addFirst (T item); void addLast (T item); T removeFirst ); T removeLast 0 Then compile and run Deques.java. The output should look like this adding 'hello' to front hello] adding well' to front [well, hello] adding world' to end [well, hello, world] getting first item well getting last item world removing first item hello, world] removing last item hello]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!