IN JAVA (NEED ASAP) /** * A simple linked deque structure of T objects. Only the * T value contained in the deque is visible
IN JAVA (NEED ASAP)
/** * A simple linked deque structure of T objects. Only the * T value contained in the deque is visible through the standard * deque methods. Extends the DoubleLink class, which already * defines the front node, rear node, length, isEmpty, and iterator. * * @param * this data structure value type. */ public class DoubleDeque extends DoubleLink {
/** * Adds a value to the front of a deque. * * @param value * value to add to the front of the deque. */ public void addFront(final T value) { // your code here return; }
/** * Adds a value to the rear of a deque. * * @param value * value to add to the rear of the deque. */ public void addRear(final T value) { // your code here return; }
/** * Returns the value at the front of a deque. * * @return the value at the front of the deque. */ public T peekFront() { // your code here }
/** * Returns the value at the rear of a deque. * * @return the value at the rear of the deque. */ public T peekRear() { // your code here }
/** * Removes and returns the value at the front of a deque. * * @return the value that has been removed. */ public T removeFront() { // your code here }
/** * Removes and returns the value at the rear of a deque. * * @return the value that has been removed. */ public T removeRear() { // your code here } }
-----------------------------------------------------------------DOUBLE LINK CLASS---------------------------------------------------------------------------
import java.lang.reflect.Array; import java.util.Iterator; import java.util.NoSuchElementException;
/** * The abstract base class for doubly-linked data structures. Provides * attributes and implementations for getLength, isEmpty, toArray, and iterator * methods. The front
attribute is the first node in any * doubly-linked list and rear
the last node. * @param
/** * Creates an Iterator for the outer class. An iterator allows a program to * walk through the data in a data structure by using the hasNext and next * methods. Typical code: * *
Iterator* * It also allows the user of the enhanced for loop: * *iter = deque.iterator(); while(iter.hasNext()){ T data = iter.next(); ... } *
for(T data : deque){ ... } ** * (Replace T with a concrete class such as String or Integer.) */ private class DoubleLinkIterator implements Iterator
/* * (non-Javadoc) * * @see java.util.Iterator#hasNext() */ @Override public boolean hasNext() { return this.current != null; }
/* * (non-Javadoc) * * @see java.util.Iterator#next() */ @Override public T next() { T result = null;
if (this.current == null) { throw new NoSuchElementException(); } else { result = this.current.getValue(); this.current = this.current.getNext(); } return result; } }
// First node of linked deque. protected DoubleNode
// Number of elements currently stored in linked deque. protected int length = 0; // Last node of linked deque. protected DoubleNode
/** * Returns the current number of elements in the linked structure. * * @return the value of length. */ public final int getLength() { return this.length; }
/** * Determines whether the deque is empty or not. * * @return true if deque is empty, false otherwise. */ public final boolean isEmpty() { return this.front == null; }
/* * (non-Javadoc) * * @see java.lang.Iterable#iterator() */ @Override public final Iterator
/** * Returns an array of data from a deque. Not thread safe as it assumes * contents of deque are not changed by an external thread during the copy * loop. If data elements are added or removed by an external thread while * the data is being copied to the array, then the declared array length may * no longer be valid. * * @return an array of data of type T. Returns null if the deque is empty. */ @SuppressWarnings("unchecked") public final T[] toArray() { T[] a = null;
if (this.front != null) { // Create an array of data based upon the class of the head data. a = (T[]) Array.newInstance(this.front.getValue().getClass(), this.length); final Iterator
for (int i = 0; i < this.length; i++) { a[i] = iter.next(); } } return a; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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