实现Linked-List的问题 [英] Problem to implement Linked-List
本文介绍了实现Linked-List的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是实现Linked-list ,,,,,这是我的代码:
Hi ,
Whats my problem to implement Linked-list ,,,,, It is my code :
public class Node
{
public Node next;
public Object data;
public class LinkedList
{
private Node head;
public string printAllNodes()
{
Node cur = head;
int Score = 0;
while (cur.next != null)
{
// Console.WriteLine(cur.data);
Score += 10;
cur = cur.next;
}
return Score.ToString();
}
public void Add(int data)
{
Node toAdd = new Node();
toAdd.data = data;
Node current = head;
current.next = toAdd;
}
}
}
我使用时
:
when I use :
Add(int data)
它给了我一个错误>>> 你调用的对象是空的。 。
为什么?!我该如何解决?!
It give me an Error >>> "Object reference not set to an instance of an object." .
why ?! how can I solve it ?!
推荐答案
你从未向头部分配任何东西
- 所以无论你在哪里尝试使用它,直接或通过cur
或当前
,它是空的,你得到一个空引用错误。
试试这个:
You never assigned anything tohead
- so wherever you try to use it, directly or viacur
orcurrent
, it is empty and you get a null reference error.
Try this:
public class LinkedList
{
private Node head = new Node();
这篇关于实现Linked-List的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文