Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

here is LabTemplatre : here is Node: Doubly Linked List Lab Create a class \"Dou

ID: 3880500 • Letter: H

Question

here is LabTemplatre :

here is Node:

Doubly Linked List Lab Create a class "DoublyLinkedList" which has a generic type E and subclasses the abstract "LabTemplate" class. In "DoublyLinkedList", you must implement every abstract method defined in the "LabTemplate" class. You do not need to implement any other methods. The class header for "DoublyLinkedList" should be as follows public class DoublyLinkedList extends LabTemplate) You'll notice that the "LabTemplate" class defines instance variables of type Node. The node class is provided for you. You do not need to implement your own node class. Some points about the lab: * 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 head's 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 When you have completed your implementation of the "DoublyLinkedList" class, use the provided test cases to test your work. When complete, submit your "DoublyLinkedList" class to Blackbaord. If you create a testing class for your own use, you don't need to submit that.

Explanation / Answer

public class DoublyLinkedList<E> extends LabTemplate<E>{

  

   @Override

   public int getSize() {

       // TODO Auto-generated method stub

       if(head==null)

           return 0;

      

       int count = 0;

       Node temp = head;

       while (temp != null){

           count = count + 1;

           temp = temp.getNextNode();

       }

      

       return count;

      

   }

   @Override

   public boolean isEmpty() {

       // TODO Auto-generated method stub

       return head==null;

   }

   @Override

   public E getFirst() {

       // TODO Auto-generated method stub

       if(head==null)

       return null;

      

       return (E) head.getElement();

   }

   @Override

   public E getLast() {

       // TODO Auto-generated method stub

       if(head==null)

           return null;

      

       Node temp = head;

       while (temp.getNextNode() != null)

           temp = temp.getNextNode();

      

       return (E) temp.getElement();

      

      

   }

   @Override

   public E getElementAt(int index) throws IndexOutOfBoundsException {

       // TODO Auto-generated method stub

      

       if(head==null)

           return null;

       else if(index < 0 || index >= getSize() )

           throw new IndexOutOfBoundsException();

       else{

      

       int count = 0;

      

       Node temp = head;

      

       while (count < index && temp != null){

           count = count + 1;

           temp = temp.getNextNode();

       }

      

      

       return (E) temp.getElement();

       }

      

   }

   @Override

   public void addFirst(Object anElement) {

       // TODO Auto-generated method stub

      

       Node newNode = new Node(anElement,null, null);

       newNode.setNextNode(head);

       if (head != null)

           head.setPreviousNode(newNode);

       head = newNode;

      

   }

   @Override

   public void addLast(Object anElement) {

       // TODO Auto-generated method stub

      

       Node newNode = new Node(anElement,null, null);

       if (head == null) {

           newNode.setNextNode(head);

           head = newNode;

           return;

       }

       Node temp = head;

       while (temp.getNextNode() != null)

           temp = temp.getNextNode();

       temp.setNextNode(newNode);

       newNode.setPreviousNode(temp);

      

   }

   @Override

   public E removeFirst() {

       // TODO Auto-generated method stub

       Object o = null;

       if (head != null) {

           if (head.getNextNode() == null) {

               o = head.getElement();

               head = null;

           } else {

               o = head.getElement();

               head.getNextNode().setPreviousNode(null);

               head = head.getNextNode();

           }

       }

       return (E) o;

   }

   @Override

   public E removeLast() {

       // TODO Auto-generated method stub

       Object o = null;

       if (head == null) {

           return null;

       }

       Node temp = head;

       Node prev = null;

       while (temp.getNextNode() != null) {

           prev = temp;

           temp = temp.getNextNode();

       }

       if (prev == null){

      

       o = head.getElement();

       head = null;

       }

       o = prev.getNextNode().getElement();

       prev.setNextNode(null);

       return (E) o;

      

   }

       }

==============================================================
Thanks, let me know if there is any concern.