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会删除我以前添加的所有节点,直到现在才存储新的AddAtLasts条目。我想这可能是因为电流。我认为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()电流不变且始终指向head的节点时,会发生这种情况,因此当调用AddAtLast()时,会使head.Next指向newNode,然后使current成为新节点。您将丢失head用来指向的所有节点。

要添加到链接列表的末尾,您必须遍历列表,直到到达node.next == null(即最后一个节点(所在的节点,然后在那里添加节点。或者,创建一个字段Node 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++;
}

相关内容

  • 没有找到相关文章

最新更新