如何删除双链接列表中的第一个节点
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 = ?
如果您确信所有这些指针都得到了正确的管理,那么您可能会解决当前的问题。
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何仅读取文本文件中的第一个值
- 动态分配列表 - 创建一个函数,用于删除所有包含偶数值的元素
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 如何将列表从一个类复制到另一个类
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++:忽略第一个 cin.ignore 之后的输入
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- OpenGL:第二个VBO破坏了第一个VBO
- 为什么第一个Dynamic_cast没有投射到基类?
- OpenGL 2D游戏只绘制第二个精灵纹理而不是第一个
- Qt JsonObjects和初始值设定项列表 - 比一个级别更深?
- C++ 为什么程序只读取第一个值
- 如何按第一个 STL 列表的顺序对第二个 STL 列表进行排序
- C++列表处理:将最小值初始化为列表的第一个值
- STL列表超出了第一个push_back的范围