Results 1 to 5 of 5
- 09-21-2011, 08:29 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
Adding tail node to a doubly linked list
The program works when I'm adding head nodes, so I thought it should be similar for the tail nodes, but it's been giving me problems. Here's the code for my node class and a section of the doubly linked list.
Java Code:public class Node { private int digit; private Node previous; private Node next; public Node() { digit = 0; previous = null; next = null; } public Node(int num, Node prev, Node next) { digit = num; setPrevious(prev); setNext(next); } public void setDigit(int num) { digit = num; } public void setPrevious(Node previousNode) { previous = previousNode; } public void setNext(Node nextNode) { next = nextNode; } public int getDigit() { return digit; } public Node getPrevious() { return previous; } public Node getNext() { return next; } }
For example, I have a linked list like so:Java Code:public class DoublyLinkedList { private Node head = new Node(); private Node tail = new Node(); private int length = 0; public DoublyLinkedList() { head.setPrevious(null); head.setNext(tail); tail.setPrevious(head); tail.setNext(null); } . . . public void insertHead(int num) { Node current = head; Node temp = new Node(num, null, current); current.setPrevious(temp); head = temp; length++; } public void insertTail(int num) { Node current = tail; Node temp = new Node(num, current, null); current.setNext(temp); tail = temp; length++; } }
1-->2-->3-->4-->5
I want to add '99' at the end so I call this statement:
list.insertTail(99);
Instead of putting the '99' at the end, it would always put a '0' instead.
1-->2-->3-->4-->5-->0
When I try to insert a head node, it does it properly, so I don't know why it's acting up for the tail node.
- 09-21-2011, 08:43 PM #2
Re: Adding tail node to a doubly linked list
Have you tried debugging your code by adding printlns to show the logic flow and the values of variables as they change?
There is no way for anyone to test your code segments.
- 09-21-2011, 08:51 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
Re: Adding tail node to a doubly linked list
Yeah, I tried doing that, but I haven't been able to figure it out yet. I'm mainly concerned if my insertTail() method looks logically correct.
- 09-21-2011, 08:53 PM #4
Re: Adding tail node to a doubly linked list
That is the way I solve most logic problems like this. Print out the data to see what is happening.
For example have you found out where the node with the 99 has gone and where the node with 0 comes from?
Can you write the steps the program should take to chain in a node at the tail in pseudo code?
And document what each of the steps in the insertTail method does.
For debugging, add a toString to the Node class that will return the three fields in the node.Last edited by Norm; 09-21-2011 at 08:58 PM.
- 09-21-2011, 10:48 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
help me with this ! circular doubly linked list
By someone in forum Advanced JavaReplies: 1Last Post: 04-11-2011, 10:35 AM -
Doubly linked list, reverse()
By hamed in forum New To JavaReplies: 4Last Post: 10-29-2010, 09:53 AM -
Doubly Linked List
By matin1234 in forum New To JavaReplies: 0Last Post: 06-02-2010, 05:58 AM -
doubly linked list insert
By ineedhelpwithjava in forum Advanced JavaReplies: 1Last Post: 03-20-2009, 02:05 PM -
Help with Doubly linked list
By Dr Gonzo in forum New To JavaReplies: 5Last Post: 12-06-2008, 07:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks