这是我到目前为止所拥有的,但它不起作用。基本上跳到 else if(cnode == preposition)
。
void LinkedList::Delete(Node *PrePosition) {
Node *cnode = head;
Node *pnode = NULL;
while (cnode != NULL) {
if (cnode->value != NULL) {
if (pnode == NULL) {
// if there is not previous node
head = cnode->next;
}
else if (cnode == PrePosition) {
// if there is previous node
cout << endl << "Deleting: " << cnode << endl;
pnode->next = cnode->next;
}
}
else {
// don't delete
pnode = cnode;
}
cnode = cnode->next;
}
}
原文由 code_theworks 发布,翻译遵循 CC BY-SA 4.0 许可协议
1:从上一个节点中取出指针,指向要删除的节点之后的下一个节点
2:删除上一个节点指向当前节点的指针
3:删除从下一个节点指向当前节点的指针(如果是双向链表)