I just need a seperate driver file for this that demonstrate
I just need a seperate driver file for this that demonstrates that each of the methods is working with a double linked list of integers and strings. This is in java....
public class GenDoubleLinkedList<T extends Comparable<T>> {
private class ListNode{
T data;
ListNode nextLink;
ListNode prevLink;
public ListNode(){
data = null;
this.nextLink = null;
this.prevLink = null;
}
public ListNode(T data) {
this.data = data;
nextLink = null;
prevLink = null;
}
}
// instance variables
private ListNode head;
private ListNode current;
public GenDoubleLinkedList() {
head = new ListNode();
current = 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 ans = null;
if(this.current.data!=null)
ans=this.current.data;
return ans;
}
public void setDataAtCurrent(T data){
if(current != null && this.current.data!=null){
this.current.data = data;
}
}
public void insertNodeAfterCurrent(T data){
ListNode i= new ListNode(data);
if(this.current != null){ // current is not null
i.nextLink=this.current.nextLink; // setting nextLink of new node to next of current
i.prevLink = current; // setting previous link of new node to current
if(current.nextLink != null) // if nextLink of current is not null, then set previous link of next link
current.nextLink.prevLink = i; // of current to new node
this.current.nextLink=i; // setting nextLink of current to new node
}
}
public void deleteCurrentNode(){
// if current is null or current point to empty node then do nothing
if(current == null || this.current.data == null)
return;
// if current node is pointing to last node of list
if(current.nextLink == null){
current.prevLink.nextLink = null;
current.prevLink = null;
current = null;
}else{
ListNode temp = current.nextLink;
current.prevLink.nextLink = temp;
temp.prevLink = current.prevLink;
current.nextLink = null;
current.prevLink = null;
current = temp;
}
}
public void showList(){
ListNode temp = head.nextLink;
while(temp != null){
System.out.print(temp.data+\" \");
temp = temp.nextLink;
}
System.out.println();
}
public boolean inList(T data){
ListNode temp = head.nextLink;
while(temp != null){
if(temp.data.compareTo(data) == 0)
return true;
temp = temp.nextLink;
}
return false;
}
}
Solution
package chegg;
public class Driver {
public static void main(String[] args) {
// instance of Integer data type
GenDoubleLinkedList<Integer> integerData = new GenDoubleLinkedList<Integer>();
// call insertNodeAfterCurrent() method
integerData.insertNodeAfterCurrent(1);
integerData.insertNodeAfterCurrent(2);
integerData.insertNodeAfterCurrent(3);
integerData.insertNodeAfterCurrent(4);
// call showList() method
integerData.showList();
// instance of String data type
GenDoubleLinkedList<String> stringData = new GenDoubleLinkedList<String> ();
// call insertNodeAfterCurrent() method
stringData.insertNodeAfterCurrent(\"USA\");
stringData.insertNodeAfterCurrent(\"IND\");
stringData.insertNodeAfterCurrent(\"JAPAN\");
stringData.insertNodeAfterCurrent(\"RUSSIA\");
//call showList() method
stringData.showList();
}
}
=====================================================================
ouput :-
4 3 2 1
RUSSIA JAPAN IND USA
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.



