[英]Linked List Implementation in C#
我正在用C#创建链接列表,我一直在使用一个网站来学习https://www.codeproject.com/Articles/1104980/Linked-List-Implementation-in-Csharp
该站点有我尝试的代码,但我有问题。 我正在使用的代码是
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条目。 我认为,这可能是由于潮流造成的。 我认为当前以为是头节点而不是最后一个节点,因此当我在末尾添加时,它在开始时添加并删除所有节点。 这可能是我遇到问题的原因。
如果我仅使用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()
添加节点时,当前节点不会改变并且始终指向head
因此当您调用AddAtLast()
它将成为head.Next
指向newNode
然后使当前节点成为新节点。 您将丢失head
曾经指向的所有节点。
要添加到链表的末尾,必须遍历该表,直到到达node.next == null
(即最后一个节点)的节点,然后将该节点添加到那里。 或者,创建一个字段Node Last
并在每次添加到列表末尾时对其进行更新,使其始终指向最后一个节点。 然后,您可以将新节点追加到Last
。
以下内容将更新当前内容,使其指向最后一个节点,然后在末尾添加一个节点。
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++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.