Objective: Write a class called GenDoublLinkedList which is a generic double lin
ID: 638479 • Letter: O
Question
Objective:
Write a class called GenDoublLinkedList 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. Download the driver (DO NOT MODIFY THE DRIVER!) (http://www.cse.sc.edu/~shephejj/csce146/Labs/Lab02Driver/DoubleLinkedListTester.java) and write the following classes:
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
Explanation / Answer
//GenDoubleLinkedList class
public class GenDoubleLinkedList<T>
{
class ListNode
{
T data;
ListNode nextLink,prevLink;
public ListNode()
{
data=null;
this.nextLink=null;
this.prevLink=null;
}
public ListNode(T data,ListNode next,ListNode prev)
{
this.data=data;
this.nextLink=next;
this.prevLink=prev;
}
}
ListNode head,current;
public GenDoubleLinkedList()
{
this.head=new ListNode();
this.current=this.head;
}
public void goToNext()
{
if(this.current.nextLink!=null)
{
this.current=this.current.nextLink;
}
}
public void goToPrev()
{
if(this.current.prevLink!=null)
{
this.current=this.current.prevLink;
}
}
public T getDataAtCurrent()
{
T result = null;
if(this.current.data!=null)result =this.current.data;
return result;
}
public void setDataAtCurrent(T data)
{
this.current.data=data;
}
public void insertNodeAfterCurrent(T data)
{
ListNode n=new ListNode(data,this.current.nextLink,this.current);
if(this.current!=null)
{
n.nextLink=this.current.nextLink;
this.current.nextLink=n;
}
}
public void deleteCurrentNode()
{
this.current.prevLink.nextLink=this.current.nextLink;
this.current=this.current.nextLink;
}
public void showList()
{
ListNode copy=this.head;
while(copy!=null)
{
System.out.println(copy.data);
copy=copy.nextLink;
}
}
public boolean inList(T data)
{
ListNode copy=this.head;
while(copy!=null)
{
if(copy.data==data)return true;
copy=copy.nextLink;
}
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.