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 8983631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:53:17+00:00 2026-06-15T20:53:17+00:00

I am working with a link list. I have set my constructor to take

  • 0

I am working with a link list. I have set my constructor to take an array named ax with a set of already defined items. I also decided to have an input box which through a BtnAddTree_Click appends the new item to the list ax. But instead of appending to the list ax it creates a whole new separate list. How can I append items to the array list ax through my AddTree function?

            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;             
            }

        }
    }
}
  • 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-15T20:53:18+00:00Added an answer on June 15, 2026 at 8:53 pm

    It doesn’t seem to be creating a new separate list. I tested out the following code with yours:

    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);
      }
    }
    

    And got the following output, which seems correct.

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

    What is the expected behavior, if this is not correct? Clearly these items are all being added to the original list, since they’re present when I iterate through the list.

    By the way, I also added the following ToString function to your ListForTrees class; it makes debugging easier.

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

    Edit: I must comment that you may find it helpful to cleanup your code a bit in trying to understand where it is going wrong. For example, your ListForTrees(fruit_trees new_tree) constructor does the same exact thing as calling Add(new_tree) would. Also, think about three cases you have in Add, under else if (count != 0) — perhaps there’s a way they could elegantly combined into one general while loop? It makes it easier to analyze, and (potentially) less error-prone.

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

Sidebar

Related Questions

I am working with a link list. My constructor takes arrays with items in
i have a set of items (or a list of items, but i don't
I am currently working with a link list of items. I am aware of
I have items contained in an unordered list. I have hidden divs (using display:none;)
I have a problem set for my programming class which I'm working on. The
I am working on an Android Honeycomb with an ActionBar. I have set up
UPDATE1: Updated the JS fiddle link sorry set interval was not working as I
I'm working through an iPhone tutorial ( link text and it has me put
As we are working locally so our service link is: http://localhost:8012/webservice.asmx When we deploy
I'm working on some code to insert a link to a dupe in the

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.