Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Class implementing List using a doubly -linked list. To get practice creating Entry instances, you will complete the constructor which initializes the linked list so
Class implementing List using a doubly-linked list. To get practice creating Entry instances, you will complete the constructor which initializes the linked list so that it contains the elements in array . To practice traversing lists, you must also complete toArray() so that it returns a newly allocated array whose entries are the same (and in the same order) as the elements in the linked list . import java.util.AbstractList; /** * This class starts to define a doubly-linked list implementation of the List interface. * @paramType of data held in this collection */ @SuppressWarnings("unused") public class DLinkedList extends AbstractList { /** Reference to the first node in our linked list. This will be null if the list is empty. */ private Entry head; /** Reference to the last node in our linked list. This will be null if the list is empty. */ private Entry tail; /** Number of elements currently in the list. */ private int size; /** * Creates an empty list. */ public DLinkedList() { size = 0; head = null; tail = null; } /** * Creates a new linked list and initializes it so that it stores the elements from the given array in the identical * order. * * @param source Array holding the elements that should be stored in our new linked list. */ public DLinkedList(E[] source) { } /** * Allocates and returns a new array. The entries in this new array should be identical (and in the same order) as the * elements in the linked list. * * @return Array containing the elements in the linked list. */ @Override public Object[] toArray() { } /** * Returns the number of elements currently in this list. * * @return the number of elements in the list */ @Override public int size() { return size; } @Override public E get(int idx) { // Skipped on account of problem #2 throw new UnsupportedOperationException("Defining this method defeats the purpose of this homework. Sorry!"); } /** * Class which defines the Entry instances ("nodes") in this doubly-linked-based list. Note that this class does not * specify a generic type, but instead uses the element type from the outer class. * * @author Matthew Hertz */ private class Entry { /** Element stored with the current entry. */ private E element; /** Reference to the next entry in our linked list or null if this is the last link. */ private Entry next; /** Reference to the previous entry in our linked list or null if this is the first link. */ private Entry prev; /** Create a new, blank Entry. */ public Entry() { element = null; next = null; prev = null; } // Auto-generated getters and setters for students who prefer using them. private E getElement() { return element; } private void setElement(E element) { this.element = element; } private Entry getNext() { return next; } private void setNext(Entry next) { this.next = next; } private Entry getPrev() { return prev; } private void setPrev(Entry prev) { this.prev = prev; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
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