链接列表:在current和current.next之间插入项目

时间:2012-12-09 04:26:51

标签: c#

目前我正在学习如何在C#中构建自己的链表。我创建了一个名为AddTrees的函数,它将一个树添加到列表的末尾。用户通过文本框输入创建树:tree_nametree_heighttree_pricetree_instock。我正在请求帮助InsertTree,而不是在当前和current.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");
            }
        }
        }
    }

1 个答案:

答案 0 :(得分: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;
 }

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