我試圖使'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)時,添加值並對其進行排序。 –