2015-12-03 70 views
1

我的目录结构是这样的:如何删除双向链表中的第一个节点?

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

    } 
+0

你怎么知道它给你一个内存泄漏? –

+0

这就是它的意思:“错误的对象0x100102c80:被释放的指针没有被分配 ***在malloc_error_break中设置一个断点来调试 ” – pigthatatepens

+1

Off topic:推荐用'nullptr'和'victim'代替那些'0' = 0;'没多大用处。它即将超出范围。 – user4581301

回答

-2

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

不过,我会给你一些指点:)你在做你有一个双链表的突变(你的情况)

随时在最多六个三分球担心。

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

如果您确切地确保所有这些指针正确管理,您可能会解决您当前的问题。

+0

不是downvoter,因为你大多是正确的。我建议一个编辑来改善:最有可能是未初始化的指针,而不是空指针。你可以''删除'空指针就好了。 'delete'检查并且对空指针不做任何事情。它也可能是未示出的代码中的一个bug,它会践踏“第一个”。没办法知道肯定。 – user4581301

+0

这就是我所想的,这就是为什么我建议所有要管理的指针。我的猜测是海报倒票,因为我没有给他们代码。 – 0111

0

尝试使用此代码为节点

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

我想你正在尝试使用双向链接列表进行堆栈。但在你的问题中并不清楚。所以我已经在所有条件下编写了删除方法。我没有测试过这个代码,你必须自己做。如果有任何错误,你可以联系我。