-1

I am trying to Implement a Linked List Using Java. The code I have used is as follow

public class LinkNode
{
private int data;
public LinkNode next;
public LinkNode (int data)
{
this.data = data;   
}
public void setData(int data)
{
this.data = data;   
}
public int getData()
{
return this.data;   
}
public void setNext(LinkNode next)
{
 this.next = next;  
}
public LinkNode getNext()
{
    return this.next;
}

public static void main (String [] args)
{

LinkNode Node1 = new LinkNode(3);
LinkNode Head = Node1;
LinkNode Node2 = new LinkNode(4);
LinkNode Node3 = new LinkNode(5);
LinkNode Node4 = new LinkNode(6);
Head.setNext(Node1);
Node1.setNext(Node2);
Node2.setNext(Node3);
Node3.setNext(Node4);
int iCounter =0;
LinkNode currentNode= Head;
while (currentNode.getNext()!=null)
{
    int data = currentNode.getData();
    System.out.println(data);
    currentNode = currentNode.getNext();
    iCounter=iCounter+1;

}
System.out.println("No Of Nodes are"+iCounter);
}
}

The Problem here I am getting No of Nodes 3

The code is not counting the last Node that is Node4.

The out put is as follow

3
4
5
No Of Nodes are3

Please let me know what is the problem in the code.

Arun
  • 875
  • 3
  • 10
  • 17
  • check this answer: [http://stackoverflow.com/questions/10042/how-do-i-implement-a-linked-list-in-java][1] [1]: http://stackoverflow.com/questions/10042/how-do-i-implement-a-linked-list-in-java – Claudiu Sep 06 '13 at 09:44
  • I do not like this question. Instead of posting your application, you should divide it to reduce the problem to the problem you have. Is your problem creating LinkedList in java or something more specific, that is not specific to the LinkedList? – Val Sep 06 '13 at 10:49

3 Answers3

1

To make Head point to Node1 write

Head = Node1;

If you write Head=null it means that Head doesn't point to any node, and you get a null pointer exception because you then try to get the next node from a node that doesn't exist.

The second problem is that you exit the loop when currentNode.getNext() returns null. The getNext() method returns null when you have reached the last node of the list; if you exit the loop then you won't count the last node. Change the loop condition into:

while (currentNode != null)

And please don't edit the question to ask followup questions. Nobody is notified when a question is edited, so you won't get new answers. It also makes the site less useful for future visitors. Post a new "question" for each question that you have.

Joni
  • 108,737
  • 14
  • 143
  • 193
0

Head, should not be null. Instead the data in head should be null, otherwise you have no way to find next.

Jim Jeffries
  • 9,841
  • 15
  • 62
  • 103
0

Here is implementation of Singly Linked List I've developed years ago:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author sergiizagriichuk
 */
public class Node<T> {

    private T value;
    private Node<T> next;

    public Node(T value) {
        this.value = value;
    }

    public static <T> Node<T> createLinkedListFromArray(T... array) {

        if (checkIfArrayIsNullOrEmpty(array)) return new Node<T>(null);

        Node<T> head = new Node<T>(array[0]);

        createLinkedList(array, head);

        return head;
    }

    private static <T> boolean checkIfArrayIsNullOrEmpty(T[] array) {
        return array == null || array.length == 0;
    }

    private static <T> void createLinkedList(T[] array, Node<T> head) {

        Node<T> node = head;

        for (int index = 1; index < array.length; index++) {
            T t = array[index];
            node.setNext(new Node<T>(t));
            node = node.getNext();
        }
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public Node<T> getNext() {
        return next;
    }

    public void setNext(Node<T> next) {
        this.next = next;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Node node = (Node) o;
        return value != null && value.equals(node.value);

    }

    @Override
    public int hashCode() {
        return value.hashCode();
    }

    @Override
    public String toString() {
        List ret = createList();
        return Arrays.toString(ret.toArray());
    }

    private List createList() {
        Node root = this;
        List ret = new ArrayList();
        while (root != null) {
            ret.add(root.getValue());
            root = root.getNext();
        }
        return ret;
    }
}

And some Tests:

/**
 * @author sergiizagriichuk
 */
public class NodeTest {
    @Test
    public void testCreateList() throws Exception {
        Node<Integer> node = Node.createLinkedListFromArray(1, 2, 3, 4, 5);
        Assert.assertEquals(Integer.valueOf(1), node.getValue());
        Assert.assertEquals(Integer.valueOf(2), node.getNext().getValue());
    }

    @Test
    public void testCreateListSize() throws Exception {
        Integer[] values = new Integer[]{1, 2, 3, 4, 5};
        int size = values.length - 1;

        Node<Integer> node = Node.createLinkedListFromArray(values);

        int count = 0;

        while (node.getNext() != null) {
            count++;
            node = node.getNext();
        }

        Assert.assertEquals(size, count);
    }

    @Test
    public void testNullNode() throws Exception {
        Node<Integer> nullNode = new Node<Integer>(null);

        assertNullNode(nullNode);
    }

    @Test
    public void testNullArray() throws Exception {
        Node<Integer> nullArrayNode = Node.createLinkedListFromArray();
        assertNullNode(nullArrayNode);

    }

    @Test
    public void testSetValue() throws Exception {

        Node<Integer> node = new Node<Integer>(null);

        assertNullNode(node);

        node.setValue(1);

        Assert.assertEquals(Integer.valueOf(1), node.getValue());
    }

    private void assertNullNode(Node<Integer> nullNode) {
        Assert.assertNotNull(nullNode);
        Assert.assertNull(nullNode.getValue());
    }

}

Try to use or redevelop for your situation

Sergii Zagriichuk
  • 5,389
  • 5
  • 28
  • 45