Can't Traverse a Doubly Linked List in Java

  • Context: Java 
  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Java List
Click For Summary

Discussion Overview

The discussion revolves around a user's difficulty in traversing a doubly linked list in Java. Participants explore potential issues in the user's code, suggest debugging techniques, and share alternative implementations of a doubly linked list.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The user expresses an inability to traverse the doubly linked list and shares their code for review.
  • One participant suggests that the issue may stem from how the head and tail are set in the create4ElementList method, indicating that both are pointing to the same initial node.
  • Another participant points out that the user is not accessing the correct head and tail variables in the create4ElementList method.
  • The user questions why the initialization of head does not seem to work as expected.
  • One participant provides an example implementation of a doubly linked list, including methods for appending, inserting, and displaying elements.
  • Another participant recommends using a debugger to step through the code and inspect variable values, emphasizing the importance of learning debugging skills.
  • The user expresses frustration with using the debugger in Netbeans, indicating difficulty with setting breakpoints and reading variable values.
  • A later reply reassures the user that setting breakpoints in Netbeans is straightforward and encourages them to invest time in learning debugging techniques.

Areas of Agreement / Disagreement

Participants express various viewpoints on the user's code issues, with no consensus reached on the exact problem. There is a general agreement on the importance of debugging skills, but individual experiences with debugging tools differ.

Contextual Notes

Participants note potential issues with variable scope and initialization in the user's code, but do not resolve these concerns. The discussion includes multiple perspectives on debugging practices and tools.

Who May Find This Useful

Readers interested in Java programming, particularly those working with data structures like linked lists, as well as those looking to improve their debugging skills in development environments like Netbeans.

zak100
Messages
462
Reaction score
11
Hi,
I can't traverse the doubly linked list.
My code is:
Java:
class Student{
   int id;
   String name;
   Student next, prev;
   Student(int id, String name){
      this.id = id;
      this.name = name;
      next = null;
      prev = null;
   }
}

public class DLLAddtoTail {
   Student head =null;
   Student tail = null;
   /**
   * @param args the command line arguments
  */

   public static void main(String[] args) {
      // TODO code application logic here
      DLLAddtoTail obj = new DLLAddtoTail();
      obj.create4ElementList();
      obj.displayFromHead();
   }

   void create4ElementList(){
      Student head=null;
      Student tail = null;
      Student newNode;
      int id =0;
      String name = "CE" +id;
      newNode = new Student(id, name);
      head = newNode;
      tail = newNode;
      for(int i=1; i<4; ++i){
         id =i;
         name = "CE" +id;
         newNode = new Student(id, name);
         tail.next = newNode;
         newNode.prev=tail;
         tail = newNode;
      }
   }

   void displayFromHead(){
      JOptionPane.showMessageDialog(null, "Testing1");
      tail = head;
      while(tail!=null){
         JOptionPane.showMessageDialog(null, "Testing2");
         JOptionPane.showMessageDialog(null, "id = "+tail.id+"name="+tail.name);
         tail = tail.next;
      }
   }
}
Some body please guide me what's the problem. I am using Netbeans 8.1 and I am working on Windows 10.

It is displaying :

Testing1
in JOptionPaneDialog(...)

Zulfi.
 
Last edited by a moderator:
Technology news on Phys.org
zak100 said:
It is displaying :

Testing1
in JOptionPaneDialog(...)
The string Testing1 is displayed by the first call to showMessagedialog() in your displayFromHead() method. Since your code isn't printing Testing2, it must mean that tail == head, so presumably your code is not setting tail correctly. I would look at your code in create4ElementList(), particularly these three lines:
Java:
newNode = new Student(id, name);
 head = newNode;
 tail = newNode;
This code creates a Student object named newNode, and then sets both head and tail to point to it. Also, I'm pretty sure you have a problem in the for loop. It would be very helpful for you to draw pictures of your linked list, and what happens at each step.
 
You are not accessing the head and tail variables you think in the create4ElementList method.
 
Hi,
Thanks for your response. Maybe "head = null" so I can't traverse. But I can't figure out why? I have initialized head in line#34. Why this initialization not working?
dll1.jpg
I am uploading the image
 
Hi,
I have solved this problem.

Zulfi.
 
Example java code for doubly linked list, with append (at end), insert (before beginning), showf (show list in forward order), and showr (show list in reverse order) functions.

Code:
class DLNode<E>{
    DLNode next;
    DLNode prev;
    E element;
    DLNode(){
        element = null;
    }
    DLNode(E e){
        element = e;
    }
}
class DLList<E>{
    DLNode head;
    DLNode tail;
    int size;
    DLList(){
        head = null;
        tail = null;
        size = 0;
    }
    public void append(E element) {
        DLNode newNode = new DLNode (element);
        size++;
        if(head == null){
            head = newNode;
            tail = newNode;
            return;
        }
        newNode.prev = tail;
        tail.next = newNode;
        tail = newNode;
    }
    public void insert(E element) {
        DLNode newNode = new DLNode (element);
        size++;
        if(head == null){
            head = newNode;
            tail = newNode;
            return;
        }
        head.prev = newNode;
        newNode.next = head;
        head = newNode;
    }
    public void showf(){
        if(head == null || tail == null)
            return;
        DLNode node = head;
        while(node != null){
            System.out.println(node.element);
            node = node.next;
        }
        System.out.println();
    }
    public void showr(){
        if(head == null || tail == null)
            return;
        DLNode node = tail;
        while(node != null){
            System.out.println(node.element);
            node = node.prev;
        }
        System.out.println();
    }
}
 
Find a good Youtube video on how to use breakpoints and this little guy called the debugger. It'll let you step through your program line by line and see exactly what your variables are doing.
242301
 
  • Like
Likes   Reactions: Mark44
Hi,
I tried this debugger several times but its always a head ache. I like to learn it but break points setting and reading variable's values becomes a problem for me. Also I am working with Netbeans and not with Eclipse.

Zulfi.
 
zak100 said:
I like to learn it but break points setting and reading variable's values becomes a problem for me. Also I am working with Netbeans and not with Eclipse.
It's really worth the time you spend to learn at least these two skills. I haven't written any Java code for more than 20 years, but I would bet that in either Netbeans or Eclipse, setting a breakpoint is just a matter of clicking the mouse on the line you want the debugger to stop on. And being able to watch how your variables change as you step through your program one line at a time takes a lot of the guesswork out of programming.

If you're going to write code, one of the absolute best uses of your time is to learn how to use a debugger, even if only for setting breakpoints, single stepping through code, and inspecting variables.
 
  • Like
Likes   Reactions: rbelli1
  • #10
For Netbeans, you click on the line and press ctrl+F8 to toggle line break on or off (or right click and use the pop-up menu). Then press ctrl+F5 to run the program in debug mode. The variables show up at the bottom of the screen when it stops at a break point.
 

Similar threads

Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K