如何删除链表中的节点? [英] How to delete a node in a linked list?

查看:76
本文介绍了如何删除链表中的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止的内容,但是没有用.基本上会跳到 else if(cnode == preposition).

This is what I have so far, but it's not working. Basically skips to 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;
    }
}

推荐答案

1:从上一个节点获取指针,并将其指向您要删除的指针之后的下一个指针

1: Take the pointer from the previous node and point it to the next one after the one you want to delete

2:删除前一个节点到当前节点的指针

2: Delete the pointer from the previous node to the current node

3:删除下一个节点到当前节点的指针(如果它是双向链接列表)

3: Delete the pointer from the next node to the current node (if it is a doubly-linked list)

这篇关于如何删除链表中的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆