如何在不将任何参数传递给类函数的情况下从单个链接列表中删除一个节点(两个节点之间)?

例如,我有一个6个节点的列表,其中一个头节点,而我想从一个类函数中删除其中两个(不事先知道它们的地址或位置),我该怎么做?

void WordList::deleteNode(){
Node *temp;
temp=head;
if(temp->count<=10)
{
//delete this node... not sure how though
}
else
temp=temp->next;
}

其中WordList是我的类,Node是我的结构,其中包含一个单词,一个计数和一个指针。
我想删除计数器为10或更少的任何节点。

最佳答案

您的编辑具有先验信息,该位指出“计数器
用于删除单链列表中符合该条件的元素的伪代码:

def delLessThanTen:
    # Delete heads meeting criteria, stop when list empty.

    while head != NULL and head->count <= 10:
        temp = head->next
        free head
        head = temp
    if head == NULL:
        return

    # Head exists, with count > 10, process starting there (we check
    #    NEXT element for criteria then delete if met).

    ptr = head
    while ptr->next != NULL:
        # If next in list meets criteria, delete it, otherwise advance.

        if ptr->next->count <= 10:
            temp = ptr->next->next
            free ptr->next
            ptr->next = temp
        else:
            ptr = ptr->next

    return

关于c++ - 如何从链表中删除节点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3732399/

10-12 00:31
查看更多