In java. Write a class called GenDoubleLinkedList which is a generic double link
ID: 3784195 • 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 hasInstance 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
Explanation / Answer
package com.quad.generic;
public class GenDoubleLinkedList<T> {
private ListNode<T> head;
private ListNode<T> tail;
private int size;
public int getSize() {
return size;
}
/**
*
*/
public GenDoubleLinkedList() {
this.head = null;
this.size = 0;
}
@SuppressWarnings("hiding")
class ListNode<T>{
private T data;
private ListNode<T> nextLink;
private ListNode<T> prevLink;
/**
*
*/
public ListNode() {
nextLink=null;
prevLink=null;
data=null;
}
/**
* @param data
* @param nextLink
* @param prevLink
*/
public ListNode(T data, ListNode<T> nextLink,
ListNode<T> prevLink) {
super();
this.data = data;
this.nextLink = nextLink;
this.prevLink = prevLink;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public ListNode<T> getNextLink() {
return nextLink;
}
public void setNextLink(ListNode<T> nextLink) {
this.nextLink = nextLink;
}
public ListNode<T> getPrevLink() {
return prevLink;
}
public void setPrevLink(ListNode<T> prevLink) {
this.prevLink = prevLink;
}
}
public void goToNext(){
ListNode<T> temp = head;
while(temp != null){
temp = temp.getNextLink();
}
}
public void goToPrev() {
ListNode<T> temporary = tail;
while(temporary != null){
temporary = temporary.getPrevLink();
}
}
public T getDataAtCurrent (int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
} else {
ListNode<T> current = head;
for (int i = 0; i < index; i++) {
current = current.nextLink;
}
return current.data;
}
}
public ListNode<T> deleteCurrentNode(){
ListNode<T> temp = head;
if(head.nextLink == null){
tail = null;
}
else{
head.nextLink.prevLink = null;
head = head.nextLink;
}
return temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.