[英]How to delete a node pointer
這是功課。 我還沒有看到任何可以直接回答這個問題的東西,因此我很難解決它。 我必須創建一個最大堆的鏈接節點實現,並且在刪除值后很難刪除節點。
我的代碼:
template<class ItemType>
BinaryHeapNode<ItemType>* LinkedMaxHeap<ItemType>::getLastNode()
{
BinaryHeapNode<ItemType>* lastNode = rootPtr->getRightSiblingPtr();
BinaryHeapNode<ItemType>* prevLastNode = rootPtr;
while(lastNode != nullptr)
{
prevLastNode = lastNode;
lastNode = lastNode->getRightSiblingPtr();
}
return prevLastNode;
}
template<class ItemType>
bool LinkedMaxHeap<ItemType>::removeValue(ItemType value)
{
BinaryHeapNode<ItemType>* tempNode = rootPtr;
for (int i = 0; i < itemCount; i++)
{
if(tempNode->getItem() == value)
{
tempNode->setItem(getLastNode()->getItem());//set item
delete getLastNode(); //delete last node
getLastNode() = nullptr; //set last node null
getLastNode()->setRightSiblingPtr(nullptr); //last node should be different
itemCount--; //set it's sibling to null
heapRebuild(tempNode);
}
tempNode = tempNode->getRightSiblingPtr();
}
return true;
}
我的問題是與getLastNode()= nullptr。 VS告訴我getLastNode()不是左值。 這對我來說沒有意義,因為getLastNode返回指向BinaryHeapNode的指針,但是它不能將該指針設置為nullptr嗎?
我認為這可能是我的指針邏輯問題(充其量是搖晃的),所以我認為更改getLastNode()以僅返回一個節點會有所幫助。 那沒有。 因此,我嘗試弄亂&運算符並返回最后一個節點的地址。 不用說我還沒有找到解決方案。 如果有人可以提供某種指導,將不勝感激。 我只是不太確定為什么它不起作用。
編輯:
根據arynaq提到的內容編輯了代碼。 錯誤消失了,但是現在我遇到了很多鏈接器錯誤,在測試之前必須修復。 這段代碼會做我想要的嗎? 我覺得這只是要刪除nodeToDelete而不是擺脫堆中的節點。
template<class ItemType>
bool LinkedMaxHeap<ItemType>::removeValue(ItemType value)
{
BinaryHeapNode<ItemType>* tempNode = rootPtr;
BinaryHeapNode<ItemType>* nodeToDelete = getLastNode();
for (int i = 0; i < itemCount; i++)
{
if(tempNode->getItem() == value)
{
tempNode->setItem(nodeToDelete->getItem());
delete &nodeToDelete;
nodeToDelete = nullptr;
getLastNode()->setRightSiblingPtr(nullptr);
itemCount--;
heapRebuild(tempNode);
}
tempNode = tempNode->getRightSiblingPtr();
}
return true;
}
好的,我將通過解釋一些有關指針的方法來幫助您。 希望這會澄清一些誤解,並幫助您完成任務。
當您像這樣獲得指針的副本時: mypointer* p = get_pointer();
然后刪除它, 即刪除內存。 但是,當您將nullptr
分配給此局部變量時,它不會影響指針的“源”。
這是一個詳細的示例,顯示了哪里可能出錯。 如果您從未將v[0]
設置為nullptr
。
#include <iostream>
#include <vector>
struct Object {
~Object() {
std::cout << "Object destructor." << std::endl;
}
int val = 42;
};
struct OtherObj {
int val = 322;
};
void print_vec(const std::vector<Object*>& v) {
for (const auto& x : v) {
std::cout << x << std::endl;
}
}
int main(int, char**) {
// Init vector and print addresses.
std::vector<Object*> v(2);
print_vec(v);
// Init objects in vector and printit.
for (auto& x : v) {
x = new Object();
}
print_vec(v);
// Get a copy of a pointer and delete that. All good so far.
Object* pointer_to_delete = v[0];
delete pointer_to_delete;
// Assign nullptr to the temporary local pointer.
// Does nothing to the pointer in the vector.
pointer_to_delete = nullptr;
// Print the vector to prove it.
print_vec(v);
// On a non debug build, the memory will still have the last value.
// Careful! Cause of headaches here. This should be set to nullptr.
std::cout << v[0]->val << std::endl; // "No problem", certainly not nullptr.
// Now that we allocate a new object, v[0] will be overwritten.
OtherObj* bad_bad_boy = new OtherObj();
// Print the address of the new object, to show it was created at
// the old v[0] address.
std::cout << bad_bad_boy << std::endl;
// Bad things ensue...
std::cout << v[0]->val << std::endl;
return 0;
}
lang的輸出是:
0x0
0x0
0x7ffa21c026c0
0x7ffa21c026d0
Object destructor.
0x7ffa21c026c0
0x7ffa21c026d0
42
0x7ffa21c026c0
322
如您所見,僅將本地指針設置為nullptr
是不夠的! 我希望這可以為您解決一些問題:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.