Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:30:45+00:00 2026-06-15T18:30:45+00:00

Currently I am learning how to build my own linked list in C#. I

  • 0

Currently I am learning how to build my own linked list in C#. I have created a function called AddTrees that adds a tree to the end of the list. User create their trees through textboxes input:tree_name, tree_height, tree_price and tree_instock. I am requesting help with my InsertTree which is not inserting a tree between current and current.next_tree? But instead of instead of inserting it between, it adds it to the top of the list.

   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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:30:46+00:00Added an answer on June 15, 2026 at 6:30 pm

    First, I think your expected result is wrong. It appears you want the linked list sorted in order of price (lowest to highest). If so, Cypress should actually go at the end of your list, not in the middle (80.00 > 50.00 > 2.00).

    Secondly, I believe the issue rests in your while loop.

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

    I believe what you’re trying to do is walk down your linked list. What you’re actually doing is mucking up your linked list by changing current.next_tree to point to itself. Changing it to the following is a positive first step towards correcting your algorithm:

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

    EDIT: It should be noted that I do not get the same error that you get in your original post. When I insert items in the same order that you claim to, I get the list in correct sorted order. This is due to the fact that Cypress goes to the end, as expected. How did you get the result that you claim in the question?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am about to build a system that will have its own engine, as
im currently learning python (in the very begining), so I still have some doubts
im currently learning stacks in java and have a quick question. what will the
I am currently learning Javascript, and I'd like to create my own Lorem Ipsum
I'm currently learning d3.js, and as a task I am trying to build a
I am currently learning Zend Framework from a book called Appress Pro Zend Framework
I am currently learning Glassfish and I see that it is possible to add
I am currently trying to build Android just for learning purpose. But I am
I start learning packaging for several distros (currently Cygwin and Debian). They have requirement
I'm currently learning how to build a site in PHP MySQL. However, I seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.