C# 链表在C语言中的实现#
我正在用c#创建链表,我一直在使用一个网站学习 这个网站有我尝试的代码,我有一个问题。我使用的代码是C# 链表在C语言中的实现#,c#,linked-list,C#,Linked List,我正在用c#创建链表,我一直在使用一个网站学习 这个网站有我尝试的代码,我有一个问题。我使用的代码是 public class Node { public Node Next; public object Value; } public class LinkedList { private Node head; private Node current;//This will have latest node public int Count; } pu
public class Node
{
public Node Next;
public object Value;
}
public class LinkedList
{
private Node head;
private Node current;//This will have latest node
public int Count;
}
public LinkedList()
{
head = new Node();
current = head;
}
public void AddAtLast(object data)
{
Node newNode = new Node();
newNode.Value = data;
current.Next = newNode;
current = newNode;
Count++;
}
public void AddAtStart(object data)
{
Node newNode = new Node() { Value = data};
newNode.Next = head.Next;//new node will have reference of head's next reference
head.Next = newNode;//and now head will refer to new node
Count++;
}
代码是从网站上复制的,所以应该没有错误。但我的问题是,当我使用AddAtStart将项目添加到列表的开头,然后使用AddAtLast将项目添加到列表的末尾时,AddAtLast会删除我以前添加的所有节点,现在只存储新的AddAtLast条目。我想这可能是因为电流。我认为current认为它是头节点,而不是最后一个节点,所以当我在末尾添加时,它会在开头添加并删除所有节点。这可能是我的问题的原因吗
如果我只使用AddAtStart,一切都很好,所有节点都被添加了,我可以有很多节点,但是当使用AddAtLast时,一切都消失了
编辑我忘了我的代码和网站不一样,我道歉。我将用我正在使用的代码进行编辑
public class LinkedList
{
private Node head;
private Node current;//This will have latest node
public int Count;
public LinkedList()
{
head = new Node();
current = head;
}
public void PrintAllNodes()
{
//Traverse from head
Console.Write("Head ->");
Node curr = head;
while (curr.Next != null)
{
curr = curr.Next;
Console.Write(curr.Value);
Console.Write("->");
}
Console.Write("NULL");
}
public void AddAtStart(object data)
{
Node newNode = new Node() { Value = data};
newNode.Next = head.Next;//new node will have reference of head's next reference
head.Next = newNode;//and now head will refer to new node
Count++;
}
public void AddAtLast(object data)
{
Node newNode = new Node();
newNode.Value = data;
current.Next = newNode;
current = newNode;
Count++;
}
}
导致此问题的原因是需要修复类
LinkedList
的结构。您必须将其构造函数和方法移动到其声明中,如下所示:
public class Node
{
public Node Next;
public object Value;
}
public class LinkedList
{
private Node head;
private Node current;//This will have latest node
public int Count;
public LinkedList()
{
head = new Node();
current = head;
}
public void AddAtLast(object data)
{
Node newNode = new Node();
newNode.Value = data;
current.Next = newNode;
current = newNode;
Count++;
}
public void AddAtStart(object data)
{
Node newNode = new Node() { Value = data };
newNode.Next = head.Next; //new node will have reference of head's next reference
head.Next = newNode; //and now head will refer to new node
Count++;
}
}
您的
current
节点在构造函数中设置为head
,然后仅在AddAtLast()
中更新当您使用
AddAtStart()
current添加节点时,所发生的情况不会发生变化,并且始终指向head
,因此当您调用AddAtLast()
时,它会生成head。接下来指向newNode
,然后将新节点设置为current。您将丢失头部
用于指向的所有节点
要添加到链接列表的末尾,必须遍历列表,直到到达node.next==null
(即最后一个节点)的节点,然后将该节点添加到该节点。或者,创建一个字段节点Last
,并在添加到列表末尾时将其更新为始终指向最后一个节点。然后,您只需将新节点附加到Last
下面将更新current,使其指向最后一个节点,然后在末尾添加一个节点
public void AddAtLast (object data) {
Node newNode = new Node();
newNode.Value = data;
while (_current.Next != null) {
current = current.Next; // Traverse current until it points to the last node
}
current.Next = newNode;
current = newNode;
Count++;
}
哦,我忘了,我道歉我忘了我实际上是按照你说的方式做的。我从网站而不是我的代码复制到所有我的评论都不会被复制过来。但我拥有linkedlist类中的所有内容,我得到了我的问题。我的类链接列表不是公开的,可能是因为这个问题。我将尝试在代码中添加缩进。我假设因为您说它编译并运行,所以您在这里复制它是一个错误,LinkedList
类的构造函数以及AddAtLast()
和AddAtStart()
方法应该在LinkedList
类中?是的,这是错误的,我添加了正确的代码为edit now是的,我认为current=head可能就是问题所在。谢谢你的帮助,我希望我能从这里把它修好。非常感谢。