Question
Please answer in Java. The code that needs to be modified is below. Thank you. Question: Implement Doubly Linked List add method which add an
Please answer in Java. The code that needs to be modified is below. Thank you.
Question: Implement Doubly Linked List add method which add an element to a specific position. - Its an instance method that takes a position and an element, then adds the element to this specific position and shifts the element currently at this position and any subsequent elements to the right. It throws an exception if the position is out of bound. It traverses the list from header if the position is closer to the header and traverses the list from trailer otherwise.
CODE:
class DLinkedList
private static class DNode
private E element;
private DNode
private DNode
public DNode(E e){
this(e, null, null);
}
public DNode(E e, DNode
element = e;
prev = p;
next = n;
}
public E getE(){
return element;
}
public DNode
return prev;
}
public DNode
return next;
}
public void setE(E e){
element = e;
}
public void setPrev(DNode
prev = p;
}
public void setNext(DNode
next = n;
}
}
private DNode
private DNode
private int size;
public DLinkedList(){
header = new DNode
trailer = new DNode
header.setNext(trailer);
size = 0;
}
public void print(){
DNode
while (temp != trailer){
System.out.print(temp.getE().toString() + ", ");
temp = temp.getNext();
}
System.out.println();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started