在ListInsertHead(Node *pNode)和ListInsertTail(Node *pNode)函數(shù)中,可以直接對pNode進行鏈接操作,而不用新建newNode節(jié)點,如下代碼所示,可以嗎?
bool?ListInsertHead(Node?*pNode) { pNode->next=m_pList->next; m_pList->next=pNode; if(m_pList-next==pNode) { return?true; } else { ????????return?false; } } bool?ListInsertTail(Node?*pNode) { Node?*currentNode=m_pList; while(currentNode->next!=NULL) { currentNode=currentNode->next; } currentNode->next=pNode; pNode->next=NULL; if(currentNode->next==pNode) { return?true; } else { ????????return?false; } }
2017-04-27
直接將傳入的結點作為鏈表中新添加的結點內存,是不安全的。因為傳入的結點內存是有可能在鏈表外被釋放掉的,如果被釋放掉,則鏈表就會斷開失效;而申請一個新的結點內存作為鏈表的結點內存,則該內存只有在鏈表中才可以被釋放掉,這樣保證了鏈表內存是安全釋放的;
2016-08-11
不可以啊 看Head函數(shù)中,第一行語句已經(jīng)改變了pNode的指針域,接著的第二行語句中pNode就不是你之前傳進來的結點。。。。就像交換a與b 的值,需要定義一個temp值
2016-08-04
不可以,調用完會銷毀