删除链接列表中的项目

时间:2015-02-13 07:21:54

标签: c++ linked-list

我已经实现了一个链表,但我发现删除链表中的最后一个元素总是失败。我多次检查了我的代码,但没有发现逻辑错误。从头部和中间节点删除工作正常。以下是我的代码:

#include<iostream>
using namespace std;
template<typename T>
class Node{ //
private:    
    T val;
    Node *next;
    Node *prev;
public:
    Node(T value){
        val = value;
        next = nullptr;
        prev = nullptr;
    }

    ~Node(){   
        if(prev != nullptr){
            prev->next = next;
        }
        if(next != nullptr){
            next->prev= prev;
        }
    }

    T& getVal(){  //
        return val;
    }

    Node* getNext(){
        return next;
    }

    Node* getPrev(){
        return prev;
    }

//insert node after this node
void insert(Node *n){
    if(next != nullptr){
        next->prev = n;
    }
    n->next = next;

    n->prev = this;
    next = n;
}

void deleteNode(){
    if(prev != nullptr && next != nullptr){
        prev->next = next;
        next->prev = prev;
        }
    }   
};

//build a linked list with deletion and push back function
template<typename T>
class LList{
private:
    Node<T> *head;
    Node<T> *tail;
public:
    LList(){
        head = nullptr;
        tail = nullptr;
    }

    ~LList(){
        if(nullptr != head){
            while(head->getNext() != nullptr){ //
                delete head->getNext();
            }
            delete head;
        }
    }



    void push_back(T val){  
        Node<T>* n = new Node<T>(val);
        if(head == nullptr){
            head = n ;
            tail = head;
        }
        else{
            tail->insert(n);
            tail = n;
        }
    }

    void deleteItem(T item){
        Node<T> *node = head;
        //delete head
        if(node->getVal() == item){
            head = node->getNext(); 
            node->deleteNode();
            return;
        }
        //delete middle and tail
        while(node->getVal() != item && node->getNext() != nullptr ){
        node = node->getNext();
        }

        if(node->getVal() == item && node == tail){
            tail = node->getPrev(); 
            node->deleteNode();
            return;
        }
        if(node->getVal() == item && node != tail){
            node->deleteNode();
            return;
        }
        else {
            cout<<"didnt find the item "<<item<<endl;

        }

    }

    void print(){
        Node<T> *node = head;
        while(node->getNext() != nullptr){
            cout<<node->getVal()<<endl;
            node = node->getNext();
        }
        cout<<node->getVal()<<endl;

    }
};


int main(){
    LList<double> list;
    list.push_back(3.13);
    list.push_back(2.8);
    list.push_back(23);
    list.push_back(4);
    list.print();
    list.deleteItem(3.13);
    list.deleteItem(2);
    list.deleteItem(4);
    list.print();
    return 0;
}

1 个答案:

答案 0 :(得分:3)

这是:

void deleteNode(){
    if(prev != nullptr && next != nullptr){
        prev->next = next;
        next->prev = prev;
        }
    }   
};

对于最后一个元素,我猜next等于nullptr,因此整个条件都失败了。 我不明白的是,为什么删除第一个元素也不会失败,你可能应该为此检查你的代码。

[编辑] 这是我修复此错误的解决方案,它与节点的析构函数基本相同:

void deleteNode(){
    if(prev != nullptr){
        prev->next = next;
    } 

    if(next != nullptr){
        next->prev= prev;
    }
};

我认为所有案件都在此考虑:

Head := prev == null & next->prev = nullptr (assuming prev for this node was nullptr before)
Middle := prev->next = next & next->prev = prev
Tail := prev->next = null (assuming as above) & next == null
Head&Tail := prev == null & next == null (Only element of the list is deleted, so no references to be changed)

[/编辑]