Need help with Data Structures. Make sure it\'s JAVA ORIGINAL and PLEASE MAKE IT
ID: 3755362 • Letter: N
Question
Need help with Data Structures. Make sure it's JAVA ORIGINAL and PLEASE MAKE IT JAVADOC FORMAT AS WELL. DON'T ANSWER THIS QUESTION IF YOU'RE GONNA COPY/PASTE SOMEONE'S CODE. JAVA ORIGINAL in JAVADOC format PLZ
Circular Linked-list implementation Objective: Design and implement a circular doubly linked-list Synopsis: A circular doubly linked-list is a doubly linked-list is a doubly liked-list where the next reference of last element leads to the first element of the list and the previous reference of the first element leads to the last element in the list. Circular doubly linked-list is often implemented as a sorted list because adding element in the correct order make more sense in this data structure Requirements: You are to design a circular doubly-linked list class with the standard add, remove, and find(contains) capabilities. Here is a list of items that must be described in details 1. A general description of your class, its required components, and how the class functions 2. Each data member, including access modification, name, data type, and justification of existence (do you absolutely need them??!!) 3. Each method including access modification, name, expected return data, input parameters exactly how the method operates, and justification of existence. Please note, constructors, and getters/setters are methods. All of these items can be submitted as a single header file with doc comments Note: While your required implementation is very minimal, you may want to take a look at the standard Java linked-list at http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html for some guidelinesExplanation / Answer
Please find the code below.
CODE
=====================
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;
}
}