- #1
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:
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.
In order to traverse a doubly linked list in Java, you will need to use a loop to iterate through each node in the list. First, set a temporary variable equal to the head of the list. Then, use a while loop to check if the temporary variable is not equal to null. Inside the loop, you can access the data of the current node and move to the next node by setting the temporary variable to the next node in the list.
If you are having trouble traversing a doubly linked list in Java, it could be due to a few possible reasons. One common issue is not properly setting the previous and next pointers when adding or removing nodes from the list. Another possible issue is not properly handling edge cases, such as an empty list or a list with only one node.
Technically, it is possible to use a for loop to traverse a doubly linked list in Java. However, it is not recommended as the size of the list could change during iteration, causing unexpected behavior. It is best to use a while loop instead to ensure proper traversal.
In a singly linked list, you can only traverse the list in one direction (forward), while in a doubly linked list, you can traverse in both directions (forward and backward). This is because each node in a doubly linked list contains a reference to both the previous and next nodes, while in a singly linked list, each node only contains a reference to the next node.
No, there are no built-in functions specifically for traversing a doubly linked list in Java. You will need to write your own code to properly traverse the list. However, there are built-in functions for manipulating and accessing data in a linked list, such as add(), remove(), and get(). These functions can be used to assist in traversal.