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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:18:31+00:00 2026-06-15T21:18:31+00:00

I am currently working/experimenting with lists. I am able to determine with function GetNextTree

  • 0

I am currently working/experimenting with lists. I am able to determine with function GetNextTree the position of each item in my list such as: First, Next and Last. I have an already made list from an array ax but now I am trying to implement an insert button that will take values tree_type, tree_height, tree_price, tree_instock and create the item. Since I can point anywhere in my list, the insert will be intended to add an item after the currently pointed to item. That is where my question is: How can I add a new item after the currently pointed to item?

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


    public fruit_trees next_tree;

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


        next_tree = null;
    }

    public string GetTreeType
    {
        get { return tree_type;
            }
    }

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

    }

}

public class ListForTrees
{
    private fruit_trees first_tree;
    public fruit_trees First_tree
    {
        get
        {
            return first_tree;
        }
    }

    public fruit_trees last_tree;
    public int current;
    public int count = 0;



    public ListForTrees(fruit_trees new_tree)
    {
        first_tree = new_tree;
        last_tree = new_tree;
        count = 1;
        current = 0;
    }

    public ListForTrees(IEnumerable trees)
    {
        current = 0;
        foreach (fruit_trees t in trees)
        {
            this.AddTree(t);
        }
    }

    public fruit_trees GetNextTree()
    {
        //current = 0;
        fruit_trees ft = first_tree;

        if (current == count)
        {

            current = 0;
        }

        int i = 0;
        while (i != current)
        {
            ft = ft.next_tree;
            i++;

        }

        return ft;
    }

}

ListForTrees mainlist = new ListForTrees();
private void BtnInsertTree_Click(object sender, EventArgs e)
{

    try
    {
        int height = Convert.ToInt32(TxtTreeHeight.Text);
        int stock = Convert.ToInt32(TxtTreeStock.Text);
        double price = Convert.ToDouble(TxtTreePrice.Text);

        fruit_trees treeadd = new fruit_trees(TxtTreeName.Text, height, price, stock);
        mainlist.AddTree(treeadd);

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

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)
    {
        TxtOutput.AppendText(current.ToString() + Environment.NewLine);
        current = current.next_tree;
    }

}

private void ShowFirstItem_Click(object sender, EventArgs e)
{
    //Show Next Item
    labelSpecificTree.Text = mainlist.First_tree.GetTreeType;
}

private void ShowNextItem_Click(object sender, EventArgs e)
{

    fruit_trees obj = mainlist.GetNextTree();
    if (obj.next_tree == null)
    {
        labelSpecificTree.Text = mainlist.First_tree.GetTreeType.ToString();
    }
    else
    {
        mainlist.current++;
        labelSpecificTree.Text = obj.next_tree.GetTreeType.ToString();
    }

}

private void ShowLastItem_Click(object sender, EventArgs e)
{
    // Show First Item
    labelSpecificTree.Text = mainlist.last_tree.GetTreeType;

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

    So I’m slightly confused by what’s going on here. You’re implementing your own ArrayList like class which is not really a list at all… I don’t really see the point of doing such things in C# but alright… Here’s the method;

      fruit_trees[] InsertNode(fruit_trees current, fruit_trees nNode)
      {
           fruit_trees[] temp = new fruit_trees[ax.Length + 1];
           int i = 0;
    
           while (ax[i] != current)
           {
                 temp[i] = ax[i];
                 i++;
           }
           temp[i+1] = nNode;
           while (i < ax.Length)
           {
                temp[i+1] = ax[i];
           }
    
           return temp; 
      }
    

    call this like such;

      ax = InsertNode(current, nNode);
    

    What we’re doing is making a new list with one extra slot, copying each fruit_tree over til we hit the node we want to insert after, then we copy the new tree in. After that we copy over the rest of the old array. Then we return the new array.

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

Sidebar

Related Questions

I am currently working through Michael Hartl's Rails Tutorial while experimenting with some other
Currently working for the first time with JSON and with little experience of jQuery.
I'm experimenting to get an Angular.js application working with Node.js. Currently I have the
I am working with lists. I am now experimenting with determining the positions of
I'm currently working with jQuery 1.4.2 and jQuery UI 1.8.2. I'm experiencing some weird
Currently working with converting SQLException error messages into messages that are more useful for
Currently working with the following package structure: /package __init__.py final.py /write __init__.py write.py /data
Currently working in the deployment of an OFBiz based ERP, we've come to the
Currently working on a VBScript to automate some of the dirty PST ingestion work
Currently working on a site built in Django and i'm getting an issue when

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.