从双圆链表中删除具有相同值的所有元素

时间:2017-03-28 20:08:42

标签: c++ data-structures doubly-linked-list

我有双重循环链表

struct Element {
    int data;
    Element *next;
    Element *previous;
};

struct List {
    Element *head;
};

任务是删除具有相同数据的元素/节点。例如,如果列表有1,1,3,4,5,1,并且在解析函数值= 1后,它应该如下3,4,5。

bool removeAllValue(List &l, int value) {
    if(!l.head)
        return false;
    else if(l.head->next == l.head && l.head->data == value){
        l.head = NULL;
        return true;
    }
    Element *p = l.head;
    do{
        if(p->data == value)
            removeElement(l, p);
        p = p->next;
    }while(p!=l.head);
    return true;

上面是通过元素迭代的函数,找到具有给定值/数据的元素并调用以下函数来删除元素。

void removeElement(List &l, Element *element){
    Element *previous = element->previous;
    Element *next = element->next;
    Element *head = l.head;
    if(next == head){ //if element is tail of the list
        l.head->previous = element->previous; // relinking head's previous to new tail
        element->previous->next = head;
    }else if(element == head){ // if element is head of the list
        l.head = next; // relinking head to tail
        l.head->previous = previous; // relinking tail to head
    }else{
        previous->next = next;
        next->previous = previous;
    }
    delete element;

}

我尝试了不同的方法,但它总是给我错误。问题在于重新调整尾部和头部,正如我想的那样,但我无法找到修复它的方法。

2 个答案:

答案 0 :(得分:0)

问题在于重新安排操作很少

void removeElement(List &l, Element *element){
    Element *previous = element->previous;
    Element *next = element->next;
    Element *head = l.head;
    if(element->next == element)
        l.head = NULL;
    else if(next == head){ //if element is tail of the list
        Element *newTail = previous;
        newTail->next = l.head;
        l.head->previous = newTail;
    }else if(element == head){ // if element is head of the list
        l.head = next; // relinking head
        l.head->previous = previous; // relinking tail to head
        previous->next = l.head; // relinking head to tail
    }else{
        previous->next = next;
        next->previous = previous;
    }
    delete element;
}

正如Marek Fekete注意到,每次删除元素时,我都没有改变迭代指针。所以我只是从头开始迭代所有列表,每次都删除一个元素。

bool removeAllValue(List &l, int value) {
    if(!l.head)
        return false;
    else if(l.head->next == l.head && l.head->data == value){
        l.head = NULL;
        return true;
    }
    Element *p = l.head;
    bool end = false;
    while(!end){
        do{
            if(p->data == value){
                Element *tmp = p;
                p = p->next;
                removeElement(l, tmp);
                break;
            }else
                p = p->next;
            if(p==l.head || l.head == NULL){
                end = true;
                break;
            }
        }while(p);
    }
    return true;
}

答案 1 :(得分:0)

我认为你可以简化它。不需要以removeElement的方式处理删除尾部/头部/中间情况,只需要在移除前部头部时重置列表的头部。我宁愿确定列表中的下一个元素和最后一个项目删除检测。像这样:

Element* removeElement(List &l, Element *element) {
    Element *newnext;
    Element *previous = element->previous;
    Element *next = element->next;
    Element *head = l.head;

    previous->next = next;
    next->previous = previous;
    newnext = next;

    if (element == head) { // if element is head of the list
        l.head = next; // set the head correctly
    }

    if (element == newnext)  // detect if last element in the list was deleted
        l.head = newnext = nullptr;
    else if (newnext == head) // detect if next is at the head again
        newnext = nullptr;

    delete element;
    return newnext;
}

然后你不必从头部重新迭代每个tiem你删除一个元素,你不必处理单个项目列表作为一个特例:

bool removeAllValue(List &l, int value) {
    if (!l.head)
        return false;

    Element *p = l.head;
    do {
        if (p->data == value)
        {
            p = removeElement(l, p);
        }
        else
        {
            p = p->next;
            if (p == l.head)
                p = nullptr;
        }
    } while (p);
    return true;
}