[英]Initializing Singly Linked List Java
I am trying to implement an abstract data type of NumList in class NumLinkedList as a singly linked list. 我正在尝试在类NumLinkedList中将NumList的抽象数据类型实现为单链接列表。
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. 当我也无法初始化数组ADT时,我相信列表ADT也被错误初始化了。 It's hard to find proper examples on Google, any references for ADT initialization? 很难在Google上找到合适的示例,是否有ADT初始化的参考?
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
. 问题是,您要将head
初始化为null
,但是随后,无论是在print
size
还是在size
,您都在执行currNode = head
,这也使其currNode.next
null
,然后在currNode.next
循环。 That'll throw a NullPointerException
right there. 那会在那里抛出NullPointerException
。 You need to go to all your methods and add special code for the limit case: 您需要转到所有方法并为极限情况添加特殊代码:
For print
: print
:
if ( head == null )
return;
For size
: 对于size
:
if ( head == null )
return 0;
Alternatively, you can simply replace your entire size
method with a simple return nItem;
另外,您可以简单地将整个size
方法替换为简单的return nItem;
statement. 声明。
For insert
: 对于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
. 顺便说一句,在您的insert
,您还需要在第一个if
内return
一个值,并且需要用else
替换第三个if
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.