0

我正在使用链接列表。我已将构造函数设置为采用一个以ax一组已定义项命名的数组。我还决定有一个输入框,它通过 aBtnAddTree_Click将新项目附加到列表中ax。但它不是附加到列表中ax,而是创建了一个全新的单独列表。如何ax通过我的AddTree函数将项目附加到数组列表中?

            public ListForTrees(IEnumerable<fruit_trees> trees)
            {
                foreach (fruit_trees t in trees)
                {
                    this.AddTree(t);
                }
            }



            public void AddTree(fruit_trees new_tree)
            {
                fruit_trees current = first_tree;

                if (count == 0)
                {
                    first_tree = new_tree;
                    last_tree = new_tree;
                    count = 1;
                }

                else if (count != 0)
                {
                    if (new_tree.tree_price <= first_tree.tree_price)
                    {
                        new_tree.next_tree = first_tree;
                        first_tree = new_tree;
                    }
                    else if (new_tree.tree_price >= last_tree.tree_price)
                    {
                        last_tree.next_tree = new_tree;
                        last_tree = new_tree;
                    }
                    else
                    {
                        while (new_tree.tree_price > current.next_tree.tree_price)
                        {
                            current = current.next_tree;
                        }
                        new_tree.next_tree = current.next_tree;
                        current.next_tree = new_tree;
                    }
                    count++;
                }
            }

        }

        ListForTrees mainlist = new ListForTrees();

        private void BtnGo_Click(object sender, EventArgs e)
        {
            fruit_trees[] ax = {   new fruit_trees("cherry", 48, 12.95, 3),
                                             new fruit_trees("pine", 36, 9.95, 8),
                                             new fruit_trees("oak", 60, 14.95, 2),
                                             new fruit_trees("peach", 54, 19.95, 3),
                                             new fruit_trees("pear", 36, 11.85, 2),
                                             new fruit_trees("apple", 62, 13.45, 5)
                                         };

            mainlist = new ListForTrees(ax);
            fruit_trees current = mainlist.first_tree;
            while (current != null)
            {                   
                current = current.next_tree;             
            }

        }
    }
}
4

1 回答 1

2

它似乎没有创建一个新的单独列表。我用你的代码测试了以下代码:

public class TreeTester
{
  public static void Main(string[] args)
  {
    var list = new ListForTrees(
        new[] { new fruit_trees("tree10",10,10,10), new fruit_trees("tree2",2,2,2) });

    list.AddTree( new fruit_trees("tree3",3,3,3) );     // middle
    list.AddTree( new fruit_trees("tree1",1,1,1) );     // first
    list.AddTree( new fruit_trees("tree50",50,50,50) ); // last
    list.AddTree( new fruit_trees("tree5",5,5,5) );     // middle
    Console.Write(list);
  }
}

并得到以下输出,这似乎是正确的。

tree1 1 1 1
tree2 2 2 2
tree3 3 3 3
tree5 5 5 5
tree10 10 10 10
tree50 50 50 50

如果不正确,预期的行为是什么?显然,这些项目被添加到原始列表中,因为当我遍历列表时它们就存在了。

顺便说一句,我还在ToString您的课程中添加了以下功能ListForTrees;它使调试更容易。

public override string ToString()
{
  string s = "";
  for (var tree=first_tree; tree!=null; tree = tree.next_tree)
    s += tree + "\n";
  return s;
}

编辑:我必须评论说,你可能会发现清理你的代码有助于了解它出错的地方。例如,您的ListForTrees(fruit_trees new_tree)构造函数执行与调用相同的操作Add(new_tree)。另外,考虑一下您在 Add 中的三个案例else if (count != 0)- 也许有一种方法可以将它们优雅地组合成一个通用的 while 循环?它使分析变得更容易,并且(可能)更不容易出错。

于 2012-12-13T06:08:06.570 回答