0

現在、C# で独自のリンク リストを作成する方法を学習しています。AddTreesリストの最後にツリーを追加するという関数を作成しました。ユーザーは、テキスト ボックスの入力を使用してツリーを作成します: tree_nametree_heighttree_priceおよびtree_instockInsertTree現在との間にツリーを挿入していない私の助けを求めてい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");
            }
        }
        }
    }
4

1 に答える 1

1

まず、期待される結果が間違っていると思います。リンクされたリストを価格順 (最低から​​最高) に並べ替えたいようです。もしそうなら、Cypress は実際にはあなたのリストの真ん中ではなく最後に行くべきです (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 に答える