Question

I can't figure why the addition to the tail of this LinkedList class is not working and is simply ignored in the output.

Here's a simple Node class:

public class IntNode {

private int val;
private IntNode next;

public IntNode() {
    this.val = 0;
    IntNode next = null;
}

public IntNode(int val) {
    this.val = val;
    this.next = null;
}

public IntNode next() {
    return this.next;
}

public int getVal() {
    return this.val;
}

public void setNextNode(int val) {
    this.next = new IntNode(val);
}
public void setNextNode(IntNode a)
{
    this.next = new IntNode(a.getVal());
}

public void setVal(int val) {
    this.val = val;
}

public String toString() {
    StringBuffer buff = new StringBuffer();
    return toString(this, buff);
}

private String toString(IntNode node, StringBuffer buff) {
    if (node == null) {
        return buff.toString();
    }

    buff.append(node.val);
    if (node.next != null) {
        buff.append(", ");
    } else {
        buff.append(".");
    }

    return toString(node.next(), buff);
}

}

And here's the linked list for it:

public class LinkedList {

private IntNode header;
private IntNode trailer;
private int listSize;

public LinkedList()
{
    this.header = null;
    this.trailer = null;
    this.listSize = 0;
}
public LinkedList(IntNode a, IntNode b)
{
    this.header = a;
    this.trailer = b;
    this.header.setNextNode(this.trailer);
    this.listSize = 2;
}
public void addNode(IntNode a)
{
    this.trailer.setNextNode(a.getVal());
    this.trailer = this.trailer.next();
    this.listSize++;
}
public String toString()
{
    return this.header.toString();
}
public static void main(String args[])
{
    LinkedList lst = new LinkedList(new IntNode(1), new IntNode(2));
    lst.addNode(new IntNode(3));
    lst.addNode(new IntNode(4));
    System.out.println(lst.toString());
}

}

The output of the main method is: 1, 2. Why is the addition method not working?

Was it helpful?

Solution

Your problem is that the LinkedList constructor sets header to a and trailer to b, but then calls header.setNextNode(this.trailer);

IntNode's method setNextNode() method discards the node that it is passed and instead creates a node with the same value as the one that it had passed in.

This means that at the end of your LinkedList constructor you have the header assigned to a which has a next node value of something other than b that has the same value as b, while trailer is set to b.

You should change your setNextNode() method to not discard the node it's handed in, as follows:

public void setNextNode(IntNode a) {
    this.next = a;
}

OTHER TIPS

In your constructor with two IntNodes, your header is not pointing to your trailer. Instead it points to a new IntNode(trailer.val). You should change

public void setNextNode(IntNode a)
{
    this.next = a;
}

In the constructor of the LinkedList class you have the code this.header.setNextNode(this.trailer);. This will not set the next node of the head to the trailer, but set the next node of the head to another node with the value of the trailer. When the trailer's next node is set, the head is not affected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top