Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to create a class DoublyLinkedList which has a generic type E and subclasses the abstract LabTemplate class. In DoublyLinkedList, I need to implement

I need to create a class DoublyLinkedList which has a generic type E and subclasses the abstract LabTemplate class.

In DoublyLinkedList, I need to implement every abstract method defined in the LabTemplate class.

The class header for DoublyLinkedList should be as follows:

public class DoublyLinkedList extends LabTemplate{ }

I don't need to implement the node class.

When adding an element to the head or tail of a list: create a new node with the given element, set the head or tail instance variable to the new node, set the new nodes next

and previous instance variables to the appropriate values, and increase the size instance variable.

If a node does not have a next or pervious node (for example, the last node in the linked list does not have a next node), the value of its next or pervious instance variable should be null

If the linked list is empty, the head and tail instance variables should be null

If the linked list has a single element, the head and tail instance variables should both be set to the single node. But the nodes next and previous instance variables should both be null.

When removing a node, we delink the node from the linked list. For example, if we want to remove the head node: we should get the second node in the linked list,

set that node to the head variable, set the value of the new heads previous instance variable to null, set the value of the hold heads next instance variable to null,

and get the element stored in the old head and return it.

Here is the LabTemplate class

--------------------------------------------------------

public abstract class LabTemplate {

protected Node head; protected Node tail; protected int size; //Getter methods //Get the size of the linked list public abstract int getSize(); //Return a boolean indicating if the linked list is empty or not public abstract boolean isEmpty(); //Return the element in the first node of the linked list public abstract E getFirst(); //Return the element in the last node of the linked list public abstract E getLast(); //Get the element at a specific node in the linked list. If the specified node does not exist, throw a new IndexOutOfBoundsException public abstract E getElementAt(int index) throws IndexOutOfBoundsException; public Node getHeadNode(){ return head; } //Setter methods //No need to implement this methods but feel free if you want the extra challenge //public abstract void addElementAt(int index, E anElement); //public abstract E removeElementAt(int index); //Add a new node to the start of the linked list. You must update the head instance variable and make sure your new node both has the //value provided by the method parameter and is properly linked to other nodes. You must also increment the size instance variable by 1 public abstract void addFirst(E anElement); //Same as add first except we add to the tail of the linked list public abstract void addLast(E anElement); //Remove the last element in the LinkedList. Make sure to update the head instance variable, decrement the size instance variable by 1, //return the element of the removed node, and have the nodes remaining in the linked list linked to the correct locations public abstract E removeFirst(); //Same as removeFirst except we remove the last element of the linked list public abstract E removeLast(); }

Here is Node class

------------------------------------------------------------

public class Node {

//Instance Variables private E element; private Node next; private Node previous; //Parameterized Constructor public Node(E anElement, Node previousNode, Node nextNode) { element = anElement; next = nextNode; previous = previousNode; } //Getters and setters public E getElement() { return element; } public void setElement(E anElement) { element = anElement; } public Node getNextNode(){ return next; } public void setNextNode(Node nextNode) { next = nextNode; } public Node getPreviousNode(){ return previous; } public void setPreviousNode(Node nextNode) { previous = nextNode; } public String toString() { return element.toString(); } }

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

Students also viewed these Databases questions

Question

3. What should a contract of employment contain?

Answered: 1 week ago

Question

1. What does the term employment relationship mean?

Answered: 1 week ago