IN JAVA. Write a class called GenDoubleLinkedList which is a generic double link
ID: 3882037 • Letter: I
Question
IN JAVA. 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 Finally write a driver that demonstrates each of the methods is working with a double linked list of integers and strings.
Explanation / Answer
/***************************************GenDoubleLinkedList.java************************************/
package java;
/**
* The Class GenDoubleLinkedList.
*
* @param <T> the generic type
*/
public class GenDoubleLinkedList<T> {
/**
* The Class ListNode.
*
* @param <T> the generic type
*/
static class ListNode<T> {
/** The data. */
T data;
/** The next link. */
ListNode<T> nextLink;
/** The prev link. */
ListNode<T> prevLink;
/**
* Instantiates a new list node.
*/
public ListNode() {
this.nextLink = null;
this.prevLink = null;
this.data = null;
}
}
/** The head. */
private ListNode<T> head;
/**
* Instantiates a new gen double linked list.
*/
public GenDoubleLinkedList() {
head = null;
}
/**
* Go to next.
*
* @param currentNode the current node
* @return the list node
*/
public ListNode<T> goToNext(ListNode<T> currentNode) {
if (currentNode != null) {
return currentNode.nextLink;
}
return null;
}
/**
* Go to prev.
*
* @param currentNode the current node
* @return the list node
*/
public ListNode<T> goToPrev(ListNode<T> currentNode) {
if (currentNode != null) {
return currentNode.prevLink;
}
return null;
}
/**
* Gets the data at current.
*
* @param currentNode the current node
* @return the data at current
*/
public T getDataAtCurrent(ListNode<T> currentNode) {
if (currentNode != null) {
return currentNode.data;
}
return null;
}
/**
* Sets the data at current.
*
* @param currentNode the current node
* @param data the data
* @return the list node
*/
public ListNode<T> setDataAtCurrent(ListNode<T> currentNode, T data) {
if (currentNode != null) {
currentNode.data = data;
return currentNode;
}
return null;
}
/**
* Insert node after current.
*
* @param currentNode the current node
* @param data the data
*/
public void insertNodeAfterCurrent(ListNode<T> currentNode, T data) {
if (currentNode != null) {
ListNode<T> newnode = new ListNode<>();
newnode.data = data;
if (goToNext(currentNode) != null) {
newnode.nextLink = goToNext(currentNode);
currentNode.nextLink.prevLink = newnode;
currentNode.nextLink = newnode;
newnode.prevLink = currentNode;
} else {
currentNode.nextLink = newnode;
newnode.prevLink = currentNode;
}
} else {
this.head = new ListNode<>();
this.head.data = data;
}
}
/**
* Insert at front.
*
* @param data the data
* @return the list node
*/
public ListNode<T> insertAtFront(T data) {
if (this.head == null) {
this.head = new ListNode<>();
this.head.data = data;
this.head.nextLink = null;
this.head.prevLink = null;
return this.head;
} else {
ListNode<T> newnode = new ListNode<>();
newnode.data = data;
newnode.prevLink = null;
newnode.nextLink = this.head;
this.head = newnode;
return this.head;
}
}
/**
* Delete current node.
*
* @param currentNode the current node
*/
public void deleteCurrentNode(ListNode<T> currentNode) {
if (currentNode != null) {
ListNode<T> prevNode = currentNode.prevLink;
if (prevNode != null) {
if (goToNext(currentNode) != null) {
prevNode.nextLink = goToNext(currentNode);
currentNode.nextLink.prevLink = prevNode;
} else {
prevNode.nextLink = null;
}
} else {
this.head = goToNext(currentNode);
}
}
}
/**
* Show list.
*/
public void showList() {
ListNode<T> temp = this.head;
while (temp != null) {
System.out.print(getDataAtCurrent(temp) + " ");
temp = goToNext(temp);
}
System.out.println();
}
}
/*******************************************DoublyLinkedListDriver.java*************************************************/
package java;
import java.GenDoubleLinkedList.ListNode;
import GenDoubleLinkedList.ListNode;
/**
* The Class DoublyLinkedListDriver.
*/
public class DoublyLinkedListDriver {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
// Integer type
GenDoubleLinkedList<Integer> obj = new GenDoubleLinkedList<>();
ListNode<Integer> node;
obj.insertNodeAfterCurrent(null, 7);
node = obj.insertAtFront(4);
obj.insertNodeAfterCurrent(node, 8);
node = obj.insertAtFront(3);
obj.insertNodeAfterCurrent(node, 9);
obj.showList();
obj.deleteCurrentNode(node);
System.out.println("After deleton of node " + node.data);
obj.showList();
// String Type
GenDoubleLinkedList<String> obj1 = new GenDoubleLinkedList<>();
ListNode<String> node1;
obj1.insertNodeAfterCurrent(null, "Rahul");
node1 = obj1.insertAtFront("Krishna");
obj1.insertNodeAfterCurrent(node1, "Ramesh");
node1 = obj1.insertAtFront("Sachin");
obj1.insertNodeAfterCurrent(node1, "Dravid");
obj1.showList();
obj1.deleteCurrentNode(node1);
// obj1.deleteCurrentNode(node1.nextLink);
System.out.println("After deleton of node " + node1.data);
obj1.showList();
}
}
/**************************************output***********************************/
3 9 4 8 7
After deleton of node 3
9 4 8 7
Sachin Dravid Krishna Ramesh Rahul
After deleton of node Sachin
Dravid Krishna Ramesh Rahul
Thanks a lot. Please let me know if you have any doubt.