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

This is a homework assignment that has to be done in JAVA. Objective: Write a cl

ID: 3782957 • Letter: T

Question

This is a homework assignment that has to be done in JAVA.

Objective:

Write a class Called GenDoubleLinkedList which is a generic double linked list. This link list is similar to the single linked list that was shown in class except that each node in addition to having data and nextLink (which was originally called link) now has prevLink.

The class GenDoubleLinkedList needs to have the following:

-Internal class ListNode which has:

- Instance Variables:

- data of type T

- nextLink of type ListNode

- prevLink of type ListNode

- Constructors:

- Default

- Parameterized

Instance Variables:

- head of type ListNode which always points to the beginning of the linked list

- current of type ListNode which moves around pointing to one of the nodes

Constructor:

- A default constructor that initializes head to an empty ListNode and sets current to point at the head.

Methods:

- goToNext – This moves the current node forward in the list by one node. It doesn’t move forward if that node is null

- goToPrev – This moves the current node backwards in the list by one node. It doesn’t move backwards if that node is null.

- getDataAtCurrent – returns the data at the current node as long as the current isn’t null

- setDataAtCurrent – takes in a parameter and sets the data at the current node to that value as long as current is not null

- insertNodeAfterCurrent – creates a new node based on data that is passed in by a parameter and puts that node after the current position

- deleteCurrentNode – removes the current node from the list by resetting the links

- showList – prints out the contents of the list line-by-line

- inList – returns a true or false value based on whether or not the data passed in via a parameter is in the list

Insert Node After Current

Delete Current Node

Head Current

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

public class GenDoubleLinkedList<T extends Comparable<T>> {

  

   private class ListNode{

       T data;

       ListNode nextLink;

       ListNode prevLink;

      

       public ListNode(T data) {

           this.data = data;

           nextLink = null;

           prevLink = null;

       }

   }

  

  

   // instance variables

   private ListNode head;

   private ListNode current;

  

   public GenDoubleLinkedList() {

       head = new ListNode(null);

       current = head;

   }

  

   public void goToNext(){

       if(current != null && (current == head && head.nextLink != null))

           current = current.nextLink;

       else if(current != null && current != head)

           current = current.nextLink;

   }

  

   public void goToPrev(){

       if(current != null && current != head)

           current = current.prevLink;

   }

  

   public T getDataAtCurrent(){

       if(current == null || current == head)

           return null;

      

       return current.data;

   }

  

   public void setDataAtCurrent(T data){

       if(current != null && current != head){

           current.data = data;

       }

   }

  

   public void insertNodeAfterCurrent(T data){

       if(current == null)

           return;

      

       ListNode newNode = new ListNode(data);

       newNode.nextLink = current.nextLink;

       if(current.nextLink != null)

       current.nextLink.prevLink = newNode;

       current.nextLink = newNode;

       newNode.prevLink = current;

      

   }

  

   public void deleteCurrentNode(){

       if(current == null || current == head)

           return;

      

       // if current node is pointing to last node of list

       if(current.nextLink == null){

           current.prevLink.nextLink = null;

           current.prevLink = null;

           current = null;

       }else{

           ListNode temp = current.nextLink;

           current.prevLink.nextLink = temp;

           temp.prevLink = current.prevLink;

          

           current.nextLink = null;

           current.prevLink = null;

          

           current = temp;

       }

   }

  

   public void showList(){

      

       ListNode temp = head.nextLink;

       while(temp != null){

           System.out.print(temp.data+" ");

           temp = temp.nextLink;

       }

       System.out.println();

   }

  

   public boolean inList(T data){

       ListNode temp = head.nextLink;

       while(temp != null){

           if(temp.data.compareTo(data) == 0)

               return true;

           temp = temp.nextLink;

       }

      

       return false;

   }

  

}

public class TestGenDoubleLinkedList {

  

   public static void main(String[] args) {

      

       GenDoubleLinkedList<Integer> list = new GenDoubleLinkedList<>();

      

       list.insertNodeAfterCurrent(4);

       list.insertNodeAfterCurrent(5);

       list.insertNodeAfterCurrent(6);

       list.showList();

      

       list.goToNext();

       list.insertNodeAfterCurrent(7);

       list.showList();

      

       list.goToPrev();

       list.insertNodeAfterCurrent(8);

       list.showList();

      

      

   }

}

/*

Sample run:

6 5 4

6 7 5 4

8 6 7 5 4

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote