Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 * data type for base data structure. */ public abstract class DoubleLink implements Iterable {

/** * 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 iter = deque.iterator();

while(iter.hasNext()){ T data = iter.next(); ... } *

* * It also allows the user of the enhanced for loop: * *
 for(T data : deque){ ... } * 
* * (Replace T with a concrete class such as String or Integer.) */ private class DoubleLinkIterator implements Iterator { // current is initialized to beginning of linked deque. private DoubleNode current = DoubleLink.this.front;

/* * (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 front = null;

// Number of elements currently stored in linked deque. protected int length = 0; // Last node of linked deque. protected DoubleNode rear = null;

/** * 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 iterator() { return new DoubleLinkIterator(); }

/** * 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 iter = this.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

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

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions

Question

Define marketing.

Answered: 1 week ago

Question

What are the traditional marketing concepts? Explain.

Answered: 1 week ago

Question

Define Conventional Marketing.

Answered: 1 week ago

Question

Define Synchro Marketing.

Answered: 1 week ago

Question

LO1 Identify why performance management is necessary.

Answered: 1 week ago