課程
/后端開發(fā)
/C++
/數據結構探險之線性表篇
為什么要將currentNode賦給currentNodeBefore
2017-03-24
源自:數據結構探險之線性表篇 3-6
正在回答
順序表刪除元素,是所有后面的元素往前移動一格。單鏈表刪除節(jié)點方便的地方就在這,他不需要所有后面節(jié)點前移,而是通過前驅節(jié)點連結到后繼節(jié)點,斷開了被刪除節(jié)點之間的連結。具體實現語句currentNodeBefore->next=currentNode->next;//第i-1個節(jié)點與第i+1個節(jié)點連接,自然斷開與第i個節(jié)點的連接。
京飛
瀟慕粉
刪除第i個結點需要找到第i和i-1兩個結點,所以將currentNode賦給currentNodeBefore,是為了遍歷結點,當currentNode為第i個結點的時候,currentNodeBefore是第i-1個結點,然后才能將i-1結點的next指向i+1結點,才能刪除i結點、
bool?List::ListDelete(int?i,Node?*pNode)??
{??
????if(i<0?||?i>=m_iLength) ??
????????return?false;??
????Node?*currentNode=m_pList;??//保存頭節(jié)點??
????Node?*currentNodeBefore=NULL; ? ?//頭節(jié)點前一個節(jié)點不存在,為NULL
????for(int?k=0;k<=i;k++)????????//查找第i個節(jié)點??
????{??
????????currentNodeBefore=currentNode;???//找到第i-1個節(jié)點??
????????currentNode=currentNode->next;???//循環(huán)結束代表currentNode就是第i節(jié)點 ?
????}??
????currentNodeBefore->next=currentNode->next;//第i個節(jié)點指針域賦給第i-1節(jié)點指針域??
????pNode->data=currentNode->data;?//刪掉節(jié)點的數據域賦給pNode輸出??
????delete?currentNode;???//釋放刪除節(jié)點的內存??
????currentNode=NULL;??
????m_iLength--;????????//節(jié)點長度減1??
????return?true;??
} ?
舉報
線性表的主體順序表和鏈表,讓學員能夠將知識融會貫通學以致用
1 回答為什么要currentNodeBefore=currentNode
1 回答為什么還需要刪除頭結點
1 回答關于currentNodeBefore=currentNode的問題
1 回答為什么在定義ListDelete()的時候要delete currentNode?
1 回答在delete currentnode的時候會出錯是為什么呢
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-04-12
順序表刪除元素,是所有后面的元素往前移動一格。單鏈表刪除節(jié)點方便的地方就在這,他不需要所有后面節(jié)點前移,而是通過前驅節(jié)點連結到后繼節(jié)點,斷開了被刪除節(jié)點之間的連結。具體實現語句currentNodeBefore->next=currentNode->next;//第i-1個節(jié)點與第i+1個節(jié)點連接,自然斷開與第i個節(jié)點的連接。
2017-03-29
刪除第i個結點需要找到第i和i-1兩個結點,所以將currentNode賦給currentNodeBefore,是為了遍歷結點,當currentNode為第i個結點的時候,currentNodeBefore是第i-1個結點,然后才能將i-1結點的next指向i+1結點,才能刪除i結點、
2017-03-24
bool?List::ListDelete(int?i,Node?*pNode)??
{??
????if(i<0?||?i>=m_iLength) ??
????????return?false;??
????Node?*currentNode=m_pList;??//保存頭節(jié)點??
????Node?*currentNodeBefore=NULL; ? ?//頭節(jié)點前一個節(jié)點不存在,為NULL
????for(int?k=0;k<=i;k++)????????//查找第i個節(jié)點??
????{??
????????currentNodeBefore=currentNode;???//找到第i-1個節(jié)點??
????????currentNode=currentNode->next;???//循環(huán)結束代表currentNode就是第i節(jié)點 ?
????}??
????currentNodeBefore->next=currentNode->next;//第i個節(jié)點指針域賦給第i-1節(jié)點指針域??
????pNode->data=currentNode->data;?//刪掉節(jié)點的數據域賦給pNode輸出??
????delete?currentNode;???//釋放刪除節(jié)點的內存??
????currentNode=NULL;??
????m_iLength--;????????//節(jié)點長度減1??
????return?true;??
} ?