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

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Convert linkedlist to doubly linked list


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


    import java.awt.Color;

    public class LinkedList {

    ListNode first;


    public LinkedList(){

    }

    public LinkedList(ListNode n){
    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. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    What is the question?
  4. #3
  5. 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??
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Add an extra link that points to the previous node in the list.

    Please edit your post and wrap the code in code tags.

IMN logo majestic logo threadwatch logo seochat tools logo