C++ 我不编译。iostream库提供对NULL的支持,我不知道nullptr。至于您第一次回答的最后一部分:我试图说有三个节点:前一个节点、当前节点和下一个节点。如果下一个节点(curr->next)不是null ptr,则将上一个节点指向该节点(prev->
C++ 我不编译。iostream库提供对NULL的支持,我不知道nullptr。至于您第一次回答的最后一部分:我试图说有三个节点:前一个节点、当前节点和下一个节点。如果下一个节点(curr->next)不是null ptr,则将上一个节点指向该节点(prev->,c++,c++11,C++,C++11,我不编译。iostream库提供对NULL的支持,我不知道nullptr。至于您第一次回答的最后一部分:我试图说有三个节点:前一个节点、当前节点和下一个节点。如果下一个节点(curr->next)不是null ptr,则将上一个节点指向该节点(prev->next=curr->next),并删除当前节点。我不确定如何将previous定义为null/nullptr。我想这是我需要解决的问题。nullptr是在c++11中添加的。我提到它是因为问题被标记了。如果它不起作用,那就意味着它没有被设置为
我不编译。iostream库提供对NULL的支持,我不知道nullptr。至于您第一次回答的最后一部分:我试图说有三个节点:前一个节点、当前节点和下一个节点。如果下一个节点(curr->next)不是null ptr,则将上一个节点指向该节点(prev->next=curr->next),并删除当前节点。我不确定如何将previous定义为null/nullptr。我想这是我需要解决的问题。
nullptr
是在c++11中添加的。我提到它是因为问题被标记了。如果它不起作用,那就意味着它没有被设置为编译c++11。哇,太棒了,我不知道我能做一些像“prev->next->prev=prev”的事情;事实是,你根本不需要tmp。你需要做2个赋值,prev需要跳过curr,然后,它现在指向的东西,需要指向它。或者第二个可以在if语句中,check prev->next现在为null\u ptr,这将减少对上面列表的完整单独结尾的需要。这就是我尝试使用tmp指针的原因,但不知道如何做到这一点。您需要删除中间的节点,并确保->下一个的下一个点和->下一个点。
class node {
public:
// default constructor
node() {name = ""; prev = NULL; next = NULL;};
// default overloaded
node(string s) {name = s; prev = NULL; next = NULL;};
// item in the list
string name;
// links to prev and next node in the list
node * next, * prev;
};
tmp->prev = prev;
void linkedList::remove(string s)
{
bool found = false;
node * curr = getTop(), * prev = NULL;
node * tmp = new node();
while(curr != NULL)
{
// match found, delete
if(curr->name == s)
{
found = true;
// found at top
if(prev == NULL)
{
node * temp = getTop();
setTop(curr->next);
getTop()->prev = NULL;
delete(temp);
} // end if
else
{
// determine if last item in the list
if (curr->next = NULL)
{
// prev node points to next node
prev->next = curr->next;
// delete the current node
delete(curr);
} // end if
// if not last item in list, proceed as normal
else
{
// prev node points to next node
prev->next = curr->next;
// set the next node to its own name
tmp = prev->next;
// set prev-link of next node to the previous node (aka node before deleted)
tmp->prev = prev;
// delete the current node
delete(curr);
} // end else
} // end else
} // end if
// not found, advance pointers
if(!found)
{
prev = curr;
curr = curr->next;
} // end if
// found, exit loop
else curr = NULL;
} // end while
if(found)
cout << "Deleted " << s << endl;
else
cout << s << " Not Found "<< endl;
} // end remove
Node *cur = find("abcd");
Node *prev = cur->prev;
prev->next = cur->next;
Node *n = cur->next;
n->next = cur->prev;
cur->next = NULL; //or nullptr
cur->prev = NULL; //or nullptr
delete cur;
if (curr->next = NULL) { ...
if (curr->next == nullptr) { ...
prev->next = curr->next;
prev->next = curr->next;
prev->next->prev = prev;
delete (curr);
void linkedList::remove(const std::string& s)
{
node* current = getTop(); // get head node
while (current != nullptr) // find the item you are trying to remove
{
if (current->name == s)
{
break; // when you find it, break out of the loop
}
}
if (current != nullptr) // if found, this will be non-null
{
if (current->prev) // if this is not the head node
{
current->prev->next = current->next;
}
else
{
// update head node
}
if (current->next) // if this is not the tail node
{
current->next->prev = current->prev;
}
else
{
// update tail node
}
// at this point, current is completely disconnected from the list
delete current;
}
}