C# 在链接列表中插入项目
目前我正在学习如何用C#构建自己的链表。我创建了一个名为C# 在链接列表中插入项目,c#,C#,目前我正在学习如何用C#构建自己的链表。我创建了一个名为AddTrees的函数,该函数根据文本框输入将树添加到列表的末尾:tree\u name、tree\u height、tree\u price和tree\u instock。我请求帮助,了解如何修改我的函数,以实现在当前和当前之间插入树的方法。下一棵树 示例 public class TheTrees { private string tree_type = " "; private int tree_height = 0;
AddTrees
的函数,该函数根据文本框输入将树添加到列表的末尾:tree\u name
、tree\u height
、tree\u price
和tree\u instock
。我请求帮助,了解如何修改我的函数,以实现在当前和当前之间插入树的方法。下一棵树
示例
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 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;
}
}
公共类树
{
私有字符串树_type=“”;
私有int树的高度=0;
公共双树价格=0;
私有int树_instock=0;
公共树木旁边的树;
公共树(字符串newtree、int newheight、int newinstock、double newprice)
{
树类型=新树;
树高=新高度;
树价格=新价格;
树_instock=新instock;
next_tree=null;
}
公共重写字符串ToString()
{
返回树类型+“”+树高度+“”+
树木价格+“”+树木指数;
}
}
公共类ListForTrees
{
公共树木第一棵;
公共树木最后一棵树;
公共整数计数=0;
公共列表树(新树)
{
第一棵树=新的树;
最后一棵树=新的树;
计数=1;
}
公共ListForTrees()
{
}
公共无效添加树(树的新树)
{
当前树=第一棵树;
如果(计数=0)
{
第一棵树=新的树;
最后一棵树=新的树;
计数=1;
}
否则如果(计数!=0)
{
if(new_tree.tree_price=last_tree.tree_price)
{
最后一棵树。下一棵树=新的树;
最后一棵树=新的树;
}
其他的
{
while(new_tree.tree_price>current.next_tree.tree_price)
{
current=current.next_树;
}
new_tree.next_tree=current.next_tree;
current.next_tree=新_tree;
}
计数++;
}
}
公共树木()
{
第一棵树=null;
计数=0;
}
}
我不知道你为什么会有ListForTrees
类,因为你有一个链表
var oak = new TheTrees("Oak", 6, 2.00, 6);
var cypress = new TheTrees("Cypress", 20, 80.00, 2);
var evergreen = new TheTrees("Evergreen", 25, 50.00, 6);
//add evergreen after cypress
oak.next_tree = evergreeen;
//insert cypress
cypress.next_tree = oak.next_tree;
oak.next_tree = cypress;
这有点帮助,但不是真的。树是从用户输入文本框创建的,可能我不理解这个问题。我当然不明白是什么让这个解决方案不适用的文本框。另一方面,我真的不明白你为什么想要一个链表,而不是一个普通的
列表
。如果我能理解你还想要什么,我很乐意添加一些更有用的细节。@tallseth这很可能是一个家庭作业问题-你为什么要实现自己的链表?这是“学习指针工作原理”课程中的典型作业。因此,OP可能没有使用内置类的选项,因为这样做他们什么也学不到这很有道理,谢谢@J.Steen。我想自学的一个问题是,我不认识老师强加给我的坏主意