I need to create two more methods to InsertNodeAfterCurrent
I need to create two more methods to InsertNodeAfterCurrent and DeleteNodeAfterCurrent (Hopefully I dont need more) in the code given below. Then I need a driver file which demonstrates each method working. If you read through the link it should explain everything. I\'m having alot of trouble! Thanks in advanced! Please answer in typed message and not a handwritten picture!
My current code is:
Solution
HI, Friend Please find my implementation.
Please let me know in case of any issue.
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;
}
}
########## Driver Class #############
public class TestGenDoubleLinkedList {
public static void main(String[] args) {
GenDoubleLinkedList<Integer> list = new GenDoubleLinkedList<>();
list.insertNodeAfterCurrent(4);
list.insertNodeAfterCurrent(5);
list.insertNodeAfterCurrent(6);
list.showList();
list.goToNext();
list.insertNodeAfterCurrent(7);
list.showList();
list.goToPrev();
list.insertNodeAfterCurrent(8);
list.showList();
list.goToNext();
list.insertNodeAfterCurrent(8);
list.showList();
list.deleteCurrentNode();
list.showList();
}
}
/*
Sample run:
6 5 4
6 7 5 4
8 6 7 5 4
8 8 6 7 5 4
8 6 7 5 4
*/




