C#如何在特定索引的LinkedList中插入节点

C#如何在特定索引的LinkedList中插入节点,c#,linked-list,C#,Linked List,我有一个学校作业,该作业的一部分是在其他两个节点之间插入一个链表节点。这就是方法 public static void insert(LinkedList<Person> list, int index, Person data) { } publicstaticvoidinsert(LinkedList列表、int索引、个人数据) { } 我读过一些关于LinkedList的文章,我发现每个链接列表都需要一个头部和一个尾部。我只是不知道如何实现它,因为我们只允

我有一个学校作业,该作业的一部分是在其他两个节点之间插入一个链表节点。这就是方法

public static void insert(LinkedList<Person> list, int index, Person data)
    {

    }
publicstaticvoidinsert(LinkedList列表、int索引、个人数据)
{
}
我读过一些关于LinkedList的文章,我发现每个链接列表都需要一个头部和一个尾部。我只是不知道如何实现它,因为我们只允许使用
LinkedList
LinkedListNode
,而没有它们提供的大部分功能

我想我必须使用while循环才能到达给定的索引,但我不知道如何通过LinkedList移动

public static void insert(LinkedList<Person> list, int index, Person data)
    {
        int counter = 0;

        while (counter <= index)
        {
            if (counter == index)
            {

            }

            counter = counter + 1;
        }
    }
publicstaticvoidinsert(LinkedList列表、int索引、个人数据)
{
int计数器=0;

while(counter这是一个有趣的作业。我想知道你的老师是否知道.Net Framework的源代码是公开的。虽然你被禁止使用像
LinkedList.AddAfter这样的方法,但你可以自由地查看源代码,看看这个方法是如何实现的,比如说……简单地复制代码?;-)

看看:

在这里,您将看到此
AddAfter
-方法的实现:

public void AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode) {
    ValidateNode(node);
    ValidateNewNode(newNode);
    InternalInsertNodeBefore(node.next, newNode);
    newNode.list = this;
}
在这里,您可以看到它是如何完成的,并且可能会获得一些“灵感”。手动设置
count
version
会有问题。它们只能通过LinkedList中的函数进行设置。我想您将无法设置
count
-变量。但是,以这种方式添加新元素应该可以工作


编辑1:当然,您必须首先在所需索引处获取列表的当前元素。您可以通过调用
list.ElementAt(index)
来获取该元素。它将返回一个
LinkedListNode
节点。与新的Person节点一起,您可以调用
InternalInsertNodeBefore()
。正如您所见,您不需要任何循环


编辑2:我似乎不得不道歉。我还没有看到
下一个
上一个
-LinkedListNode的
值也是只读值。请参阅:

此外,Microsoft密封了该类。因此,无法通过新类继承该类并覆盖
Next
Previous
以添加setter方法。似乎您必须自己实现一个全新的LinkedList和LinkedListNode类。关于此主题,已经存在一个问题:

这是我考虑过的代码。但它不会起作用,因为
next
prev
head
是内部对象。它们有公共引用,如
next
Previous
First
,但它们是只读的

public static void insert(LinkedList<Person> list, int index, Person data)
{
    int currentIndex = 0;
    var currentNode = list.head;
    if (index >= 0)
    {
        while (currentNode != null && currentIndex <= index)
        {
            if (currentIndex == index)
            {
                data.next = currentNode;
                data.prev = currentNode.prev;
                currentNode.prev.next = data;
                currentNode.prev = data; 
            }
            currentIndex++;
            currentNode = currentNode.next;
        }
    }
}
publicstaticvoidinsert(LinkedList列表、int索引、个人数据)
{
int currentIndex=0;
var currentNode=list.head;
如果(索引>=0)
{

while(currentNode!=null&¤tIndex所以不允许您使用?不,我不允许使用此方法。如果您想要一个好的介绍,请观看此视频()然后观看此()。如您所述,您必须通过连续转到列表中的下一个节点(通过具有下一个属性),从head节点导航到您的索引,从而转到特定索引在你达到你的索引之前。那将非常有效,问题是,我甚至不允许使用任何复杂的内置函数。我的解决方案中没有使用复杂的内置函数。你指的是哪一个?list.ElementAt(index)好的-我知道如何处理这个问题,即使不使用
list.ElementAt(index)
我将更新我的答案-但你不应该忘记这是一项提高技能的任务。你应该自己尝试找到解决方案。我将在6小时内更新我的答案。这应该足够了。以下是一些提示:你需要一个循环;搜索“在c#中迭代链接列表”嘿@Fihii。似乎我错过了一些尴尬的事实。我认为不使用类的给定方法就不可能手动将新节点插入LinkedList。我完成了所有细节的回答。但是现在您应该对链表有足够的了解,以开发自己的列表和节点类来完成任务。
public static void insert(LinkedList<Person> list, int index, Person data)
{
    int currentIndex = 0;
    var currentNode = list.head;
    if (index >= 0)
    {
        while (currentNode != null && currentIndex <= index)
        {
            if (currentIndex == index)
            {
                data.next = currentNode;
                data.prev = currentNode.prev;
                currentNode.prev.next = data;
                currentNode.prev = data; 
            }
            currentIndex++;
            currentNode = currentNode.next;
        }
    }
}