【问题标题】:Item after current item in list列表中当前项目之后的项目
【发布时间】:2012-12-11 18:13:56
【问题描述】:

我目前正在处理项目的链接列表。我知道LinkedList<T>(),但出于学习目的,我自己实现了这一点。我创建了一个Add 函数,它将一个项目附加到列表的末尾。现在我正在努力使用我的 Insert 函数,该函数应该在当前指向的项目之后附加一个项目。相反,它在调用Insert();Cannot evaluate expression because the current thread is in a stack overflow state 时显示错误。任何想法如何在当前指向的项目之后插入项目? (我在名为labelSpecificTree 的标签中显示当前指向的项目)

代码

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }    
            public FruitTrees Insert(int Position)
            {                    
                FruitTrees current = First;

                for (int i = 0; i < Position && current != null; i++)
                {
                    current = current.Next;
                }
                return current;    
            }                

    }
}

【问题讨论】:

  • 您确实需要努力将代码减少到可以复制问题的最小可能示例,而不是仅仅转储整个事情并说“修复它”。
  • 嗯,不会为收盘而战,但作为线索,摆脱所有索引的东西。找到您要插入的项目,创建新项目,将其设置在您找到的项目的旁边,然后找到新项目。

标签: c# linked-list


【解决方案1】:

我不确定我是否理解您的要求。

但是如果你需要在给定的列表项之后插入一个项目:

public Insert(Item newItem, Item refItem) {
  newItem.Next = refItem.Next;
  refItem.Next = newItem;
}

【讨论】:

    猜你喜欢
    • 2021-05-05
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多