0

目前我正在学习如何在 C# 中构建自己的链表。我创建了一个函数,该函数AddTrees将一棵树添加到列表的末尾。用户通过文本框输入创建他们的树:tree_nametree_height和。我正在请求帮助,我的哪个没有在 current 和 ? 之间插入树?但是,它不是将其插入,而是将其添加到列表的顶部。tree_pricetree_instockInsertTreecurrent.next_tree

   namespace Tree_farm
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            public class TheTrees
            {
                private string tree_type = " ";
                private int tree_height = 0;
                public double tree_price = 0;
                private int tree_instock = 0;


                public TheTrees next_tree;

                public TheTrees(string newtree, int newheight, int newinstock, double newprice)
                {
                    tree_type = newtree;
                    tree_height = newheight;
                    tree_price = newprice;
                    tree_instock = newinstock;


                    next_tree = null;
                }

                public override string ToString()
                {
                    return tree_type + " " + tree_height + " " + tree_price + " " + tree_instock;
                }

            }


            public class ListForTrees
            {
                public TheTrees first_tree;
                public TheTrees last_tree;

                public int count = 0;

                public ListForTrees(TheTrees new_tree)
                {
                    first_tree = new_tree;
                    last_tree = new_tree;
                    count = 1;
                }

                public ListForTrees()
                {

                }

                public void InsertTree(TheTrees new_tree)
                {
                    TheTrees 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.next_tree = current;
                            }

                            new_tree.next_tree = current.next_tree;
                            current.next_tree = new_tree;
                        }

                        count++;
                    }
                }

 public void AddTree(TheTrees new_tree)
            {
                TheTrees 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++;
                }
            }

                public void ClearTrees()
                {
                    first_tree = null;
                    count = 0;
                }
            }

            ListForTrees mainlist = new ListForTrees();

            private void Form1_Load(object sender, EventArgs e)
            {

            }


            private void BtnInsertTree_Click(object sender, EventArgs e)
            {
                //Insert Code
                try
                {
                    int height = Convert.ToInt32(TxtTreeHeight.Text);
                    int stock = Convert.ToInt32(TxtTreeStock.Text);
                    double price = Convert.ToDouble(TxtTreePrice.Text);

                    TheTrees treeinsert = new TheTrees(TxtTreeName.Text, height, stock, price);

                    mainlist.InsertTree(treeinsert);
                }
                catch
                {
                    MessageBox.Show("Please check intput fields");
                }
            }


        private void BtnAddTree_Click(object sender, EventArgs e)
        {
            try
            {
                int height = Convert.ToInt32(TxtTreeHeight.Text);
                int stock = Convert.ToInt32(TxtTreeStock.Text);
                double price = Convert.ToDouble(TxtTreePrice.Text);

                TheTrees treeadd = new TheTrees(TxtTreeName.Text, height, stock, price);

                mainlist.AddTree(treeadd);
            }
            catch
            {
                MessageBox.Show("Please check intput fields");
            }
        }
        }
    }
4

1 回答 1

1

首先,我认为您的预期结果是错误的。看来您希望链接列表按价格顺序(从最低到最高)排序。如果是这样,赛普拉斯实际上应该排在列表的末尾,而不是在中间(80.00 > 50.00 > 2.00)。

其次,我相信问题在于您的 while 循环。

 while (new_tree.tree_price > current.next_tree.tree_price)
 {
      current.next_tree = current;
 }

我相信你想要做的是沿着你的链表走。您实际上在做的是通过将 current.next_tree 更改为指向自身来破坏您的链接列表。将其更改为以下是纠正算法的积极的第一步:

 while (new_tree.tree_price > current.next_tree.tree_price)
 {
      current = current.next_tree;
 }

编辑:应该注意的是,我没有收到您在原始帖子中遇到的相同错误。当我以您声称的相同顺序插入项目时,我会以正确的排序顺序获得列表。这是因为赛普拉斯正如预期的那样走到了尽头。你是如何得到你在问题中声称的结果的?

于 2012-12-09T04:39:39.823 回答