Like the title says I need help to display a doubly linked list in a forward order. No matter what I do, I can just get it to display in its regular reverse order.

Here's the code:

public class DoubleLinkedList
{  private Node h;  

    public DoubleLinkedList()
    {   h = new Node();  
    	h.back = null;
        h.l = null;
        h.next = null;
    }
public void showAll()
    {  Node p = h.next;
       while (p != null) 
       {   System.out.println(p.l.toString( ));
           p = p.next;
       }
    }
    
    public class Node
    {  private StudentListing l;
       private Node next;
       private Node back;
       public Node()
      {
      }
    }
}

And thanks for taking the time to help me.

PS: As you may have noticed I can't use the linkedlist class for this program.

What is the problem? You just start at the last node and work your way back. It is the same code as going forward except you use the 'back' Node instead of the 'next' Node and you stop when back == null and you start at the end instead of the front.

\you start at the end instead of the front.

That's what I'm having trouble whatever I try to do I get NullPointerException.

Oh. It's because when you're going forward, you said something like:

while (node != null) node = node.next()

Which guarantees that once you get out of that loop, "node" is equal to null. So you can use an additional Node variable to store the current Node (before you assign it to node.next()) or you can change your while loop so that it prints out node.next() and checks that node.next() != null instead of printing out Node.

Shoot me, please, I still cannot get it to work!

With this:

public void showAllForward()
  {  Node q = h;
     Node p = h.next;
     while (p != null)  //to get to the back of the list
        {  q = p;
           p = p.next;
        }
     while (q != null) //This SHOULD print out the list from the first node, right?
        {   System.out.println(q.l.toString( ));
            q = q.back;
        }
  }

I get the first node and nothing else. When fiddling with it I have also gotten the second node, second & last(or was that last & second?), NullPointerException, and first & NullPointerException. None of which is what I need. What am I still doing wrong?

Node temp = head; //head is the first node

//temp is now the last Node, head is still the first node
while (temp.next() != null) temp = temp.next();

..

Node temp = head; //head is the first node

//temp is now the last Node, head is still the first node
while (temp.next() != null) temp = temp.next();

..

Thanks your previous post helped me get to the last node. However, now I can't get it to go back through the list. I can print the first entry and then, shouldn't

temp=temp.back;

bring me to the second one? I get NullPointerException.

Thanks your previous post helped me get to the last node. However, now I can't get it to go back through the list. I can print the first entry and then, shouldn't

temp=temp.back;

bring me to the second one? I get NullPointerException.

Then you probably never initialized temp.back. Whenever you create a new Node (except for the first Node) you should set its node.back to the previous one.

Then you probably never initialized temp.back. Whenever you create a new Node (except for the first Node) you should set its node.back to the previous one.

How do you mean?

If you never set 'back' to anything, then it will always be null. I'm saying you must have forgotten to set it.

If you never set 'back' to anything, then it will always be null. I'm saying you must have forgotten to set it.

But I have tried setting it to something. I've tried doing temp.back=head, temp.back=head.back, temp.back=temp, temp=temp.back,temp.back=temp.back, temp=head, and temp=head.back to name a few. Followed by

System.out.println(q.l.toString( ));
  q=q.back;
  System.out.println(q.l.toString( ));
  q=q.back;
  System.out.println(q.l.toString( ));

And I usually get the first entry and then NullPointerException, repeats of the first entry, or NullPointerException.

Ok, I was finally able to get to my professor and he said the problem was in my insert method where, as BestJewSinceJC pointed out, I had to set temp.back. :( But it still doesn't work.

This is the regular insert and a sorted insert and the showAll method that I am using for now:

public boolean insert(Listing newListing)
    {  Node n = new Node();
        if(n == null) // out of memory
           return false;
        else
        { n.next = h.next;
          h.next = n;
          h.next.back = n; 
          n.back = h;
          n.l = newListing.deepCopy();
          //n.back = null;       
           return true;
        }
        
    }
    
    public boolean insertSorted(Listing newListing) 
    {  Node n = new Node(); 
        if(n == null) // out of memory
           return false;
        else
        { Node q = h;
          Node p = q.next;
         while (p!=null && newListing.compareTo(p.l.getName())>0)
         {  q=p;
            p=p.next;
         }
           n.next = q.next;
           q.next = n;
           q.next.back = n;
           n.back = q;
           n.l = newListing.deepCopy();
           return true;
        }
    }

public void show()
{ Node q= h;
 while (q.next!= null)
  { q=q.next;
  }
  System.out.println(q.l.toString( ));
  q=q.back;
  System.out.println(q.l.toString( ));
  q=q.back;
  System.out.println(q.l.toString( ));
 }

So, I insert B, A, and then D. I get B and then NullPointerException.

With the sorted insert I get D, B, and again NullPointerException.

Where am I still going wrong?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.