在已按降序排列的鏈表中插入新值,保持序列不增特性。核心步驟:
當前節點值 < 新值的位置插入。/// 降序插入
/// 待插入數值
public void InsertUnAscending(int InsertValue)
{
if (IsNull())
{
Append(InsertValue);
return;
}
MoveFirst();
if (InsertValue > GetCurrentValue())
{
Insert(InsertValue);
return;
}
while (true)
{
if (InsertValue > GetCurrentValue())
{
Insert(InsertValue);
break;
}
if (IsEOF())
{
Append(InsertValue);
break;
}
MoveNext();
}
}
IsNull():判斷鏈表為空。MoveFirst():當前指針移至頭節點。GetCurrentValue():獲取當前節點數值。Insert(value):在當前節點前插入。Append(value):尾部追加。IsEOF():判斷是否到達尾部。MoveNext():當前指針後移。同理可實現升序插入(InsertAscending),只需調整比較方向。