C++ 双链表的删除前端函数
C++ RemoveFront function for Doubly Linked List
我正在为学校做一个实验室,我被困在一个双链表的函数上。当我尝试使用 removeFront(( 函数从列表中删除前端节点时,它返回 NULL 和 couts 供我创建一个列表,即使已经有一个列表。我将在下面发布我的 cpp 文件和主要文件,希望有人可以帮助我了解它出了什么问题。
//DLinkedList.h
#pragma once
#include <string>
using namespace std;
typedef string Elem;
struct DNode
{
Elem value;
DNode* next;
DNode* prev;
};
class DLinkedList
{
public:
DLinkedList() { header_ = NULL; }
~DLinkedList() { };
bool empty() const;
const Elem& front() const;
const Elem& back() const;
void addFront(const Elem& e);
void addBack(const Elem& e);
void removeFront();
void removeBack();
private:
DNode* header_;
DNode* trailer_;
protected:
void add(DNode* v, const DNode& e);
void remove(DNode* v);
};
//DLinkedList.cpp
#include <iostream>
#include "DLinkedList.h"
using namespace std;
const Elem& DLinkedList::front() const
{
if (header_ == NULL)
{
cout << "Please create a doubly linked list first.nn";
}
else
{
return header_->value;
}
}
void DLinkedList::addFront(const Elem& e) //DONE
{
// If there is no header
// create a temporary node and set
// the header to it
if (header_ == NULL)
{
DNode *temp;
temp = new(struct DNode);
temp->prev = NULL;
temp->value = e;
temp->next = NULL;
header_ = temp;
trailer_ = temp;
}
else
{
//Create current node to point to header
// and temp node to be the new front node
DNode *current;
DNode *temp;
current = header_;
temp = new(struct DNode);
temp->prev = NULL;
temp->value = e;
temp->next = current->next;
header_->prev = temp->next;
header_ = temp;
}
cout << "Element Inserted at the front." << endl;
}
void DLinkedList::removeFront()
{
// Check to see if there is anything
// in the list first
if (header_ == NULL)
{
cout << "Create a doubly linked list first.";
}
// Check to see if the list has more than one item.
// If it only has one node erase it.
DNode *current;
current = header_;
if (current->next == NULL)
{
header_->next = NULL;
header_->value = "";
header_->prev = NULL;
header_ = NULL;
}
else
{
current = current->next;
//header_->next = NULL;
//header_->value = "";
//header_->prev = NULL;
header_ = current;
header_->prev = NULL;
}
}
//Main.cpp
#include <iostream>
#include <string>
#include <cassert>
#include "DLinkedList.h"
using namespace std;
int main()
{
DLinkedList album;
album.addFront("Word");
album.addFront("Turn");
album.addFront("Bird");
album.addFront("Weird");
cout << album.front() << endl;
album.removeFront();
cout << album.front() << endl;
system("pause");
return 0;
}
这就是我会怎么做的。
void DLinkedList::removeFront()
{
// Check to see if there is anything
// in the list first
if (header_ == NULL)
{
cout << "Create a doubly linked list first.";
}
// Check to see if the list has more than one item.
// If it only has one node erase it.
if (header_->next == NULL)
{
delete header_;
}
else
{
DNode* next = header_->next;
delete header_;
header_ = next;
header_->prev = NULL;
}
}
我认为你的addFront也是错误的,我会这样做:
void DLinkedList::addFront(const Elem& content) //DONE
{
// If there is no header
// create a temporary node and set
// the header to it
if (header_ == NULL)
{
header_ = new(struct DNode);
header_->prev = NULL;
header_->value = content;
header_->next = NULL;
trailer_ = header_;
}
else
{
//Create current node to point to header
// and temp node to be the new front node
DNode *tmp;
tmp = new(struct DNode);
tmp->prev = NULL;
tmp->value = content;
tmp->next = header_;
header_->prev = tmp;
header_ = tmp;
}
cout << "Element Inserted at the front." << endl;
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- C++ 双链表的删除前端函数