I am having issues with this DLList code. My instructor is very hard to understa
ID: 3670399 • Letter: I
Question
I am having issues with this DLList code. My instructor is very hard to understand, and he has issues conveying the material. I have provided the Tester class and the DLList class. Do only what is in Bold. Here are the instructions:
//Here is the DLList class:
public class DLList {
private class Node {
private int data;
private Node next, prev;
public Node(int d){
data = d;
next = prev = null;
}
public Node(int d, Node prev1, Node next1){
data = d;
prev = prev1;
next = next1;
}
}
private Node head, tail;
public DLList(){
head = tail = null;
}
public void addToTail(int data1) {
if (tail != null) {
tail = new Node(data1, tail, null);
tail.prev.next = tail;
} else
head = tail = new Node(data1);
}
public void displayList(){
Node tmp = head;
while(tmp != null){
System.out.print(tmp.data+ " ");
tmp = tmp.next;
}
System.out.println();
}
public void displayReverse(){
Node tmp = tail;
while(tmp != null){
System.out.print(tmp.data+ " ");
tmp = tmp.prev;
}
System.out.println();
}
private Node find(int target) {
Node position = head;
while (position != null) {
if (position.data ==target)
return position;
position = position.next;
}
return null; //target was not found
}
// Do not modify any code above this line.
// find the first number in the list.
// Insert the second parameter before the found node.
// Write insertBefore method here.
}
//Here is the Tester class:
public class Tester {
public static void main(String[] args) {
System.out.println("Programmed by . ");
DLList list1 = new DLList();
list1.addToTail(4);
list1.insertBefore(4, 2);
list1.displayList();
list1.displayReverse();
list1.addToTail(6);
list1.addToTail(8);
list1.insertBefore(999, 1);
list1.insertBefore(8, 7);
list1.displayList();
list1.displayReverse();
}
}
Explanation / Answer
DLList.java
public class DLList {
private class Node {
private int data;
private Node next, prev;
public Node(int d){
data = d;
next = prev = null;
}
public Node(int d, Node prev1, Node next1){
data = d;
prev = prev1;
next = next1;
}
}
private Node head, tail;
public DLList(){
head = tail = null;
}
public void addToTail(int data1) {
if (tail != null) {
tail = new Node(data1, tail, null);
tail.prev.next = tail;
} else
head = tail = new Node(data1);
}
public void displayList(){
Node tmp = head;
while(tmp != null){
System.out.print(tmp.data+ " ");
tmp = tmp.next;
}
System.out.println();
}
public void displayReverse(){
Node tmp = tail;
while(tmp != null){
System.out.print(tmp.data+ " ");
tmp = tmp.prev;
}
System.out.println();
}
private Node find(int target) {
Node position = head;
while (position != null) {
if (position.data ==target)
return position;
position = position.next;
}
return null; //target was not found
}
// Do not modify any code above this line.
// find the first number in the list.
// Insert the second parameter before the found node.
// Write insertBefore method here.
//THIS IS THE METHOD YOU WANTED
public void insertBefore(int num, int data)
{
Node current = head;
for (int count = 0; count < num && current.next != null; ++count)
{
current = current.next;
}
Node n = new Node(data);
n.next = current;
if (current.prev != null)
current.prev.next = n;
n.prev = current.prev;
current.prev = n;
}
}
----------------------------------------------------------------------------
Tester class
dlllist.java
public class dlllist {
public static void main(String[] args) {
System.out.println("Programmed by . ");
DLList list1 = new DLList();
list1.addToTail(4);
list1.insertBefore(4, 2);
list1.displayList();
list1.displayReverse();
list1.addToTail(6);
list1.addToTail(8);
list1.insertBefore(999, 1);
list1.insertBefore(8, 7);
list1.displayList();
list1.displayReverse();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.