Consider implementing a Deque using a layered implementation on a LinkedList. Co
ID: 3823481 • Letter: C
Question
Consider implementing a Deque using a layered implementation on a LinkedList. Consider the spec below for a Deque. A deque is a structure which allows insertion and deletion from either end. As appropriate the methds in the class should raise the exception EmptyDequeException.
public class Deque<T>
{
private LinkedList<T> Deque;
public Deque () {}
public T removeFirst() throws EmptyDequeException {}
public T getFirst() throws EmptyDequeException {}
pulic void addLast() {}
public T removeLast() throws EmptyDequeException {}
public boolean isEmpty ()
public String toString ()
public void clear () {}
public void addFirst (T data) {}
public T getLast() throws EmptyDequeException {}
public int size () {}
}
This needs to be written in java. thank you
specification below for a Deuwe using a layerel implementation on Linkedust. Consider the ncher end front or rearm Dolan. A Daque is a aucture will allows isiotinn and deletion from As appropriate the methods in the class should raise the exception public class Dogue T public Dequel) 0 Public TremaveFinto throws EmptyDequeExceptionll public T getFinu0 throw EmptyDoqueException 0 public void addLast0 l public T removel ast0 throws EntpryDequeExceptiln public boolean isi umpty0 public String toString) public void deur0 public void addFirst data public itt sireExplanation / Answer
Here is the code for the above scenario:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item>
{
private int size;
private Node first;
private Node last;
public Deque() {
}
public boolean isEmpty() {
return first == null;
}
public int size()
{
return size;
}
public void addFirst(Item item)
{
throwIfNull(item);
Node newFirst = new Node();
newFirst.item = item;
if (first != null) {
newFirst.next = first;
first.previous = newFirst;
}
first = newFirst;
if (last == null) last = first;
size++;
}
public Item removeFirst() {
throwIfEmpty();
Node oldFirst = first;
first = first.next;
if (first == null)
last = null;
else
first.previous = null;
size--;
return oldFirst.item;
}
public void addLast(Item item) {
throwIfNull(item);
Node newLast = new Node();
newLast.item = item;
if (last != null) {
newLast.previous = last;
last.next = newLast;
}
last = newLast;
if (first == null) first = last;
size++;
}
public Item removeLast() {
throwIfEmpty();
Node oldLast = last;
last = oldLast.previous;
if (last == null)
first = null;
else
last.next = null;
size--;
return oldLast.item;
}
private void throwIfEmpty() {
if (first == null)
throw new NoSuchElementException();
}
private void throwIfNull(Item item) {
if (item == null)
throw new NullPointerException();
}
public Iterator<Item> iterator() {
return new ItemsIterator();
}
private class ItemsIterator implements Iterator<Item> {
private Node current;
public ItemsIterator() {
current = first;
}
public boolean hasNext() {
return current != null;
}
public Item next() {
if (current == null)
throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
private class Node {
Item item;
Node next;
Node previous;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.