繁体   English   中英

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

[英]How do I delete the first node in a doubly-linked list?

我的清单的结构如下:

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;
        }

    }

尝试将此代码用于节点

 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
     };

双向链接列表看起来像这样,尚未完成。 但您只需在其中添加删除功能。

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方法。 我尚未测试此代码。您必须自行完成。如果有任何错误,您可以与我联系。

关于学术诚信问题,我不会发布解决方案,因为这是一项很普遍的任务。

但是,我会给您一些提示:)

每当您对双向链表进行变异时,您(就您而言)最多有六个指针需要担心。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM