简体   繁体   中英

Initializing Singly Linked List Java

I am trying to implement an abstract data type of NumList in class NumLinkedList as a singly linked list.

public class NumLinkedList implements NumList
{

Node head;
int nItem;

private class Node
{
    public Node next;
    public double value;
    public Node(double i, Node j)
    {
        value = i;
        next = j;

    }

}

public NumLinkedList()
{
    head = null;
    nItem = 0;

}

is my initialization and I am having trouble with the following methods.

public void print()
{
    Node currNode = head;
    while(currNode.next != null)
    {
        currNode = currNode.next;
        System.out.println(currNode.value);
    }

}

public int size()
{
    int size = 0;
    Node currNode = head;
    while(currNode.next != null)
    {
        currNode = currNode.next;
        size++;
        nItem++;

    }
    return size;

}

public void insert(int i, double value)
{
    if( i < 0)
    {
        System.out.println("Error. out of bounds.");
    }
    if ( i > size())
    {
        Node currNode = head;
        for(int j = 0; j < i; j++)
        {
            currNode = currNode.next;
        }
        currNode.next = new Node(value,null);
    }

    if ( i <= size())
    {
        Node currNode = head;
        for(int j = 0; j < i-1; j++) // this moves CurrNode to the i-1th node
        {
            currNode = currNode.next;
        }
        currNode.next = new Node(value,currNode.next);
    }

    nItem++;
}

when I run my testing code,

public static void main (String[] args)
{
    NumLinkedList test;
    test = new NumLinkedList();

    //System.out.println("this is how many initial items the initialized list has");
    //System.out.println(test.size());
    test.insert(1, 0.1);
    System.out.println("have tried to insert value 0.1 @ position 1, that is the first element in list.");
    test.print();
    System.out.println("tried print(). did it work?");
    test.insert(4, -1);

I get an error in the

test.insert(1, 0.1);

, referring to

if ( i > size())

and

while(currNode.next != null)

as I failed initializing my array ADT as well, I believe that my list ADT is also mis-initialized. It's hard to find proper examples on Google, any references for ADT initialization?

Your problem isn't in the initialization, which is fine, but in the methods themselves.

The problem is that you're initializing your head to null , but then, both in print and in size , you're doing currNode = head , which also makes it null , and then looping on currNode.next . That'll throw a NullPointerException right there. You need to go to all your methods and add special code for the limit case:

For print :

if ( head == null )
    return;

For size :

if ( head == null )
    return 0;

Alternatively, you can simply replace your entire size method with a simple return nItem; statement.

For insert :

if ( head == null )
{
    head = new Node(value,null);
    return;
}

As an aside, in your insert , you also need a return inside your first if , and you need to replace your third if with an else .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM