我試圖將新節點插入已排序的整數鏈表中,但嘗試添加到長度超過兩個節點的列表時遇到問題。我使用下面的代碼來做到這一點將新節點插入到已排序的鏈接列表中時出現問題
// Initialise place-holder pointers at first and second nodes
TempPrevious = Head;
TempNext = Head -> Next;
do // Iterate until the right spot is found
{
// Does the new node fit between the currently selected nodes?
if ((NewNode -> Element > TempPrevious -> Element) &&
(NewNode -> Element < TempNext -> Element))
{
NewNode -> Next = TempNext;
TempPrevious -> Next = NewNode;
return true;
}
// Does the new node fit in further along the list?
else if (NewNode -> Element > TempNext -> Element)
{
// Has the end of the list already been reached?
if (TempNext -> Next == NULL)
{
TempNext -> Next = NewNode;
return true;
}
// Or are there still more nodes to come?
else if (TempNext -> Next != NULL)
{
TempPrevious = TempNext;
TempNext = TempNext -> Next;
}
}
} while (TempNext -> Next != NULL);
我已經佔了一個空列表,單個節點列表,以及一個兩節點列表,以及在列表的開始插入新節點多於兩個元素,所有這些部分都可以正常工作。我已將問題確定爲所提供的代碼中的最後一個else if
,因爲似乎指針TempNext
和TempPrevious
未與每個迭代的do-while
循環一起移動。我得出這個結論構建包含以下元素列表和觀察PrintList()
函數的輸出後:
Input: 1,2,3,4,5
Output: 1,2,0
Input; 5,4,3,2,1
Output: 1,2,3,4,5
我看過了我的代碼和運行這些測試,但無法找到在邏輯的任何故障。任何人都可以看到我要去哪裏嗎?
全list :: Insert()
功能
// Insert new element
template <class Type>
bool list<Type> :: Insert (const Type& NewElement)
{
Node *NewNode;
Node *TempNext;
Node *TempPrevious;
NewNode = new Node;
NewNode -> Element = NewElement;
if (Empty()) // If the list is empty
{
Head = NewNode;
return true;
}
else if (Head -> Next == NULL) // If there is only a single node in the list
{
// If the element is less than or equal to the new one
if (Head -> Element <= NewNode -> Element)
{
Head -> Next = NewNode;
return true;
}
// If the element is greater than the new one
else if (Head -> Element > NewNode -> Element)
{
NewNode -> Next = Head;
Head = NewNode;
return true;
}
}
// Multi-node lists - the list has at least two existing nodes
// Initialise place-holder pointers at first and second nodes
TempPrevious = Head;
TempNext = Head -> Next;
// Does the new node go at the start?
if (NewNode -> Element < TempPrevious -> Element)
{
NewNode -> Next = TempPrevious;
Head = NewNode;
return true;
}
do // Iterate until the right spot is found
{
// Does the new node fit between the currently selected nodes?
if ((NewNode -> Element > TempPrevious -> Element) &&
(NewNode -> Element < TempNext -> Element))
{
NewNode -> Next = TempNext;
TempPrevious -> Next = NewNode;
return true;
}
// Does the new node fit in further along the list?
else if (NewNode -> Element > TempNext -> Element)
{
// Has the end of the list already been reached?
if (TempNext -> Next == NULL)
{
TempNext -> Next = NewNode;
return true;
}
// Or are there still more nodes to come?
else if (TempNext -> Next != NULL)
{
TempPrevious = TempNext;
TempNext = TempNext -> Next;
}
}
} while (NewNode -> Next != NULL);
delete TempNext, TempPrevious;
}
作業?如果不是,則默認通知:根據情況使用STL列表或向量類。 – EmeryBerger 2011-03-19 16:44:02
對不起,忘記標記爲:P – 2011-03-19 16:45:35
您是否嘗試過繪製鏈接列表的圖表,並逐行瀏覽代碼,並更改圖表以符合代碼的功能?當你有圖片時,鏈表更容易理解(我無法想象出鏈接列表算法,沒有繪製圖片!) – 2011-03-19 16:50:39