Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何删除双链接列表中的第一个节点?_C++ - Fatal编程技术网

C++ 如何删除双链接列表中的第一个节点?

C++ 如何删除双链接列表中的第一个节点?,c++,C++,我的列表结构如下: struct Node { Node *next; Node *prev; T datum; }; Node *first; // points to first Node in list, or 0 if list is empty Node *last; // points to last Node in list, or 0 if list is empty 我已尝试执行以下操作: void pop_front() { //

我的列表结构如下:

struct Node {
    Node *next;
    Node *prev;
    T datum;
};
Node *first;   // points to first Node in list, or 0 if list is empty
Node *last;    // points to last Node in list, or 0 if list is empty
我已尝试执行以下操作:

void pop_front()
{
     //copied from lecture slides
     assert(!empty());
     Node *victim = first;
     first = first->next;
     if(first != 0)
     {
         first->prev = 0;
     }
     delete victim;
     victim=0;
}
问题是,当我删除受害者行时,它给了我一个内存泄漏。我不知道怎么了

编辑:这是我添加节点的方式:

 //MODIFIES: this
    //EFFECTS:  inserts i into the front of the list
    void pushit_tofront(const T &datum)
    {
        //if list is empty, update both the first and last element
        //copied from lecture slides
        Node *p = new Node;
        p->datum = datum;
        p->next = first;
        if(empty())
        {
            first = last = p;
        }
        else
        {
            first = p;
        }

    }

关于学术诚信的问题,我不打算发布解决方案,因为这是一项受欢迎的任务

不过,我会给你一些建议:)

任何时候你在做一个双链表的变异,你(在你的例子中)最多要担心六个指针

curr->next = ?
curr->prev = ?
next->prev = ?
next->next = ?
head = ?
tail = ?

如果您确定所有这些指针都得到了正确的管理,则可能会解决当前的问题。

尝试将此代码用于节点

 class Node {
   public:
        int get() { return object; }; // returns the value of the element
        void set(int object) { this->object = object; }; // set the value of the element

        Node* getNext() { return nextNode; }; // get the address of the next node
        void setNext(Node* nextNode) // set the address of the next node
         { this->nextNode = nextNode; };

        Node* getPrev() { return prevNode; }; // get the address of the prev node
        void setPrev(Node* prevNode) // set the address of the prev node
         { this->prevNode = prevNode; };

 private:
        int object; // it stores the actual value of the element
        Node* nextNode; // this points to the next node
        Node* prevNode; // this points to the previous node
     };
双链接列表看起来像这样,它是未完成的。但您只需在其中添加remove函数

class DoublyList
{
public:
        List();
        void add (int addObject);
        int get();
        bool next();
        void start();
        void remove();


private:
        int size;
        Node * headNode;
        Node * currentNode;
        Node * lastCurrentNode;
};
双链接列表删除它将从任何位置删除。您已经询问了第一个节点和最后两个是否条件对您有帮助

DoublyList::remove(){
    Node *removeNode= currentNode;

    if((currentNode->getNext()!=null)&&(currentNode->getprev())!=headNode)){ // it will remove the node in middle
    lastCurrentNode->setNext(currentNode->getNext());
    (currentNode->getNext())->setPrev(currentNode->getPrev);
    currentNode= currentNode->getNext();
    }

    if((currentNode->getprev())!=headNode)&&(currentNode->getNext()==null)){   // it will remove the node if it is last node
    lastCurrentNode->setNext(null);
    currentNode= lastCurrentNode;
    lastCurretnNode= currentNode->getPrev();
    }

    if((currentNode->getprev())==headNode)&&(currentNode->getNext()==null)){ //if it is at the start and next node is null
        headNode->setNext(null);
    lastCurrentNode= headNode;
    currentNode= headNode;
    }

        if((currentNode->getprev())==headNode)&&(currentNode->getNext()!=null)){  //if it is at the start and next node is not null
        headNode->setNext(currentNode->getNext());
        (currentNode->getNext())->setPrev(headNode);
    lastCurrentNode= headNode;
    currentNode= currentNode->getNext();
    }



    delete removeNode;
    size--;
}

我认为您正在尝试使用双链接列表制作堆栈。但你的问题并不清楚。因此,我编写了所有条件下的remove方法。我没有测试这段代码。你必须自己做。如果有任何错误,你可以与我联系。

你怎么知道这会给你带来内存泄漏?它是这么说的:“对象0x100102c80的错误:被释放的指针没有分配***在malloc\u error\u break中设置一个断点以进行调试”主题外:建议将
0
s替换为
nullptr
被害人=0没有多大用处。它即将超出范围。如何添加节点?您不初始化p->prev或first->prev。因为您基本上是正确的,所以不是downvoter。我建议进行一次编辑,以最大限度地改进:它很可能是一个未初始化的指针,而不是空指针。您可以
删除
空指针<代码>删除
检查空指针,但不执行任何操作。它也可能是一些未下载的代码中的bug,首先会践踏
。没有办法确定。这就是我想的,这就是为什么我建议管理所有指针。我猜海报被否决是因为我没有给他们密码。