zak100
- 462
- 11
| Hi, I can't traverse the doubly linked list. My code is:
Java:
|
It is displaying :
Testing1
in JOptionPaneDialog(...)
Zulfi.
Last edited by a moderator:
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.
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.
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.
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.
| Hi, I can't traverse the doubly linked list. My code is:
Java:
|
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:zak100 said:It is displaying :
Testing1
in JOptionPaneDialog(...)
newNode = new Student(id, name);
head = newNode;
tail = newNode;
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();
}
}
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.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.