1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
2
Rep Power
0

How to I convert this to a doubly linked list??

import java.awt.Color;

ListNode first;

}

first=n;
}
public ListNode makeNullNode(){
return new ListNode();
}

public void insert(ListNode n){
if (n == null){
throw new NullPointerException("Attempt to insert a null node failed");
}
if(first==null){
first =n;
}
else{
n.setNext(first);
first =n;
}
}
public void insertBefore(ListNode successor, ListNode n){
if (n == null){
throw new NullPointerException("Attempt to insert a null node failed.");
}
else if(first==null){
first =n;
}
else if(first==successor){
n.setNext(first);
first =n;
}
else{
ListNode a = first;

while(a!=null){
if(a.getNext()==successor){
n.setNext(a.getNext());
a.setNext(n);
return;
}
a=a.getNext();
}
throw new NullPointerException("Attempt to insert failed; missing target.");

}

}

public void delete(ListNode n){
if (n==null){
return;
}

if(n==first){

first=first.getNext();
return;
}
ListNode a = first;
while(a!=null){
if (a.getNext()==n){
a.setNext(a.getNext().getNext());
}
a=a.getNext();
}

}
public ListNode getFirst(){
return first;
}

public ListNode find(int d){
ListNode a = first;
return findNext(a,d);
}
public ListNode findNext(ListNode n, int d){

if (n==null){
return null;
}
ListNode a = n.getNext();

while(a!=null){
if(a.getData()==d){
return a;
}
a=a.getNext();
}
return null;

}

public boolean isEmpty(){
if (first==null){
return true;
}
else{
return false;
}
}

}

class ListNode {
private ListNode next;
private int data;

ListNode(){
set(null, 0);
}

ListNode(int i){
set(null, i);
}

ListNode(ListNode n, int i){
set(n, i);
}
private void set(ListNode n, int i){
next = n;
data = i;
}

public ListNode getNext(){
return next;
}
public void setNext(ListNode n){
next=n;
}

public boolean isEqual(ListNode l){
if(l==this){
return true;
}
else if(l.data==this.data){
return true;
}
else {
return false;
}
}

public int getData(){
return data;
}
public ListNode makeNode(int n){
return new ListNode (n);

}
public ListNode setNode(int n){
data = n;
return this;
}
}
2. What is the question?
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Posts
2
Rep Power
0
How do I convert the linked list to a doubly linked list??
4. Add an extra link that points to the previous node in the list.