我试图使'Add(int n)'方法将节点添加到基于节点的列表。该列表已排序,并且我想将该节点添加到正确的位置,以便在添加节点后仍将对其进行排序。c#将值添加到基于排序节点的列表
例如:
当前节点列表的值:1 - 2 - 2 - 3 - 5 值来添加:2 结果:1 - 2 - 2 - 2 - 3 - 5
我提出节点列表 我的代码:一类叫做
class NodeList
{
private int head;
private NodeList tail;
public NodeList(int head, NodeList tail)
{
this.head = head;
this.tail = tail;
}
public NodeList Add(int n)
{
NodeList nl = new NodeList(head, tail);
NodeList result = null;
if (nl.head > n)
result = new NodeList(n, nl);
else
{
//cant figure this part out
}
return result;
}
}
添加一个节点时,“n”是小于在节点基于列表的第一个元素就是容易弄清楚,但我不能似乎想出如何如果不是这样,那就做吧。
其他信息:
该列表可以包含重复项。 类NodeList不能有比我包括的更多的实例变量。
尝试使用列表。每当用户调用Add(int n)时,添加值并对其进行排序。 –