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

Make JAVADOC for this code plz like comments or something. =====================

ID: 3755649 • Letter: M

Question

Make JAVADOC for this code plz like comments or something.

=====================

package sample;

/*

* Java Program to Implement Circular Doubly Linked List

*/

/**

* Class Node

* This class is used to represent a node in the circular doubly linked list

**/

class DLNode

{

   protected int data;

   protected DLNode next, prev;

   /* Constructor */

   public DLNode()

   {

       next = null;

       prev = null;

       data = 0;

   }

   /* Constructor */

   public DLNode(int data, DLNode next, DLNode prev)

   {

       this.data = data;

       this.next = next;

       this.prev = prev;

   }

   /* Function to set link to next node */

   public void setLinkNext(DLNode n)

   {

       next = n;

   }

   /* Function to set link to previous node */

   public void setLinkPrev(DLNode p)

   {

       prev = p;

   }

   /* Function to get link to next node */

   public DLNode getLinkNext()

   {

       return next;

   }

   /* Function to get link to previous node */

   public DLNode getLinkPrev()

   {

       return prev;

   }

   /* Function to set data to node */

   public void setData(int d)

   {

       data = d;

   }

   /* Function to get data from node */

   public int getData()

   {

       return data;

   }

}

/* Class linkedList */

public class CircularDoublyLinkedList {

   private DLNode head;

   private DLNode tail ;

   public int size;

   /* Constructor */

   public CircularDoublyLinkedList()

   {

       head = null;

       tail = null;

       size = 0;

   }

   /* Function to check if list is empty */

   public boolean isEmpty()

   {

       return head == null;

   }

   /* Function to get size of list */

   public int getSize()

   {

       return size;

   }

   /* Function to insert element at begining */

   public void addAtHead(int val)

   {

       DLNode nptr = new DLNode(val, null, null);

       if (head == null)

       {

           nptr.setLinkNext(nptr);

           nptr.setLinkPrev(nptr);

           head = nptr;

           tail = head;

       }

       else

       {

           nptr.setLinkPrev(tail);

           tail.setLinkNext(nptr);

           head.setLinkPrev(nptr);

           nptr.setLinkNext(head);

           head = nptr;

       }

       size++ ;

   }

   /*Function to insert element at tail */

   public void addAtTail(int val)

   {

       DLNode nptr = new DLNode(val, null, null);

       if (head == null)

       {

           nptr.setLinkNext(nptr);

           nptr.setLinkPrev(nptr);

           head = nptr;

           tail = head;

       }

       else

       {

           nptr.setLinkPrev(tail);

           tail.setLinkNext(nptr);

           head.setLinkPrev(nptr);

           nptr.setLinkNext(head);

           tail = nptr;

       }

       size++;

   }

   /* Function to insert element at position */

   public void addAtIndex(int val , int index)

   {

       DLNode nptr = new DLNode(val, null, null);

       if (index == 0)

       {

           addAtHead(val);

           return;

       }

       DLNode ptr = head;

       for (int i = 1; i < size; i++)

       {

           if (i == index)

           {

               DLNode tmp = ptr.getLinkNext();

               ptr.setLinkNext(nptr);

               nptr.setLinkPrev(ptr);

               nptr.setLinkNext(tmp);

               tmp.setLinkPrev(nptr);

           }

           ptr = ptr.getLinkNext();

       }

       size++ ;

   }

  

   /* Function to delete node at position */

   public void removeAtIndex(int index)

   {

       if (index == 0)

       {

           if (size == 1)

           {

               head = null;

               tail = null;

               size = 0;

               return;

           }

           head = head.getLinkNext();

           head.setLinkPrev(tail);

           tail.setLinkNext(head);

           size--;

           return ;

       }

       if (index == size-1)

       {

           tail = tail.getLinkPrev();

           tail.setLinkNext(head);

           head.setLinkPrev(tail);

           size-- ;

       }

       DLNode ptr = head.getLinkNext();

       for (int i = 2; i <= size; i++)

       {

           if (i == index)

           {

               DLNode p = ptr.getLinkPrev();

               DLNode n = ptr.getLinkNext();

               p.setLinkNext(n);

               n.setLinkPrev(p);

               size-- ;

               return;

           }

           ptr = ptr.getLinkNext();

       }

   }

   /* Function to return a string representation of the circular doubly linked list*/

   public String toString()

   {

       System.out.print(" Circular Doubly Linked List = ");

       DLNode ptr = head;

       if (size == 0)

       {

           System.out.print("empty ");

           return "";

       }

       if (head.getLinkNext() == head)

       {

           return head.getData() + " -> "+ptr.getData()+ " ";

       }

      

       String res = head.getData() + " -> ";

       ptr = head.getLinkNext();

       while (ptr.getLinkNext() != head)

       {

           res += ptr.getData()+ " -> ";

           ptr = ptr.getLinkNext();

       }

       res += ptr.getData()+ " -> ";

       ptr = ptr.getLinkNext();

       res += ptr.getData()+ " ";

       return res;

   }

}

Explanation / Answer

package sample;

/*

* Java Program to Implement Circular Doubly Linked List

*/

/**

* Class Node

* This class is used to represent a node in the circular doubly linked list

**/

// Strcture of Doubly linked list

class DLNode

{

protected int data;

protected DLNode next, prev; // pointer to next and prev node

/* Constructor */

public DLNode()

{

next = null;

prev = null;

data = 0;

}

/* Constructor */

public DLNode(int data, DLNode next, DLNode prev)

{

this.data = data;

this.next = next;

this.prev = prev;

}

/* Function to set link to next node */

// set next node for this node

public void setLinkNext(DLNode n)

{

next = n;

}

/* Function to set link to previous node */

// set prev node for this node

public void setLinkPrev(DLNode p)

{

prev = p;

}

/* Function to get link to next node */

// getting next link node for linked list

public DLNode getLinkNext()

{

return next;

}

/* Function to get link to previous node */

// getting prev node for linked doubly list

public DLNode getLinkPrev()

{

return prev;

}

/* Function to set data to node */

// set data in node

public void setData(int d)

{

data = d;

}

/* Function to get data from node */

// get node value

public int getData()

{

return data;

}

}

/* Class linkedList */

public class CircularDoublyLinkedList {

private DLNode head; // head node

private DLNode tail ; // tail node

public int size; // size of linkedList

/* Constructor */

public CircularDoublyLinkedList()

{

head = null;

tail = null;

size = 0;

}

/* Function to check if list is empty */

public boolean isEmpty()

{

// if doubly linked list is empty then set head null

return head == null;

}

/* Function to get size of list */

public int getSize()

{

return size;

}

/* Function to insert element at begining */

public void addAtHead(int val)

{

DLNode nptr = new DLNode(val, null, null);

// if the list is empty create single node

// circular and doubly list

if (head == null)

{

nptr.setLinkNext(nptr);

nptr.setLinkPrev(nptr);

head = nptr;

tail = head;

}

// if list is not empty

else

{

nptr.setLinkPrev(tail);

tail.setLinkNext(nptr);

// update next and previous pointers of start and last node

head.setLinkPrev(nptr);

nptr.setLinkNext(head);

// update head

head = nptr;

}

size++ ;

}

/*Function to insert element at tail */

// function to insert node with value

public void addAtTail(int val)

{

DLNode nptr = new DLNode(val, null, null);

if (head == null)

{

// set next link

nptr.setLinkNext(nptr);

nptr.setLinkPrev(nptr);

// set head

head = nptr;

// set tail

tail = head;

}

else

{

// pointer points to last node

nptr.setLinkPrev(tail);

// set next of new node

tail.setLinkNext(nptr);

// update start pointer

head.setLinkPrev(nptr);

nptr.setLinkNext(head);

tail = nptr;

}

size++;

}

/* Function to insert element at position */

public void addAtIndex(int val , int index)

{

DLNode nptr = new DLNode(val, null, null);

if (index == 0)

{

// if you want to add at index 0 (first element ) then use addAtHead method to add at head

addAtHead(val);

return;

}

DLNode ptr = head;

for (int i = 1; i < size; i++)

{

if (i == index)

{

// if your node is index then get next link node

DLNode tmp = ptr.getLinkNext();

// set next link node

ptr.setLinkNext(nptr);

// now change for previous node as well

nptr.setLinkPrev(ptr);

nptr.setLinkNext(tmp);

tmp.setLinkPrev(nptr);

}

// node get next node link

ptr = ptr.getLinkNext();

}

size++ ;

}

  

/* Function to delete node at position */

public void removeAtIndex(int index)

{

// if you want to remvoe at starting node

if (index == 0)

{

// if you want to remove starting node and there is only one node in list then head should be null and tail should be null

if (size == 1)

{

head = null;

tail = null;

size = 0;

return;

}

head = head.getLinkNext();

head.setLinkPrev(tail);

tail.setLinkNext(head);

size--;

return ;

}

// if you are dealing with last element

if (index == size-1)

{

get prev node of last node

tail = tail.getLinkPrev();

tail.setLinkNext(head);

// set tail and head

head.setLinkPrev(tail);

size-- ;

}

DLNode ptr = head.getLinkNext();

// now corener cases handle now deal with size between min and max

for (int i = 2; i <= size; i++)

{

if (i == index)

{

DLNode p = ptr.getLinkPrev();

DLNode n = ptr.getLinkNext();

p.setLinkNext(n);

n.setLinkPrev(p);

size-- ;

return;

}

ptr = ptr.getLinkNext();

}

}

/* Function to return a string representation of the circular doubly linked list*/

public String toString()

{

System.out.print(" Circular Doubly Linked List = ");

DLNode ptr = head;

if (size == 0)

{

// if size is 0 then it is empty

System.out.print("empty ");

return "";

}

if (head.getLinkNext() == head)

{

if it is head then get head data

return head.getData() + " -> "+ptr.getData()+ " ";

}

  

String res = head.getData() + " -> ";

ptr = head.getLinkNext();

while (ptr.getLinkNext() != head)

{

res += ptr.getData()+ " -> ";

ptr = ptr.getLinkNext();

}

res += ptr.getData()+ " -> ";

ptr = ptr.getLinkNext();

res += ptr.getData()+ " ";

return res;

}

}

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