我正在尝试为项目创建双向链表容器。我不能使用任何标准容器。必须对双重链表进行排序。到目前为止,这是我的代码:
#include <iostream>
using namespace std;
template <typename T>
class dll {
private:
struct Node {
Node* prev;
Node* next;
T data;
};
Node* head;
Node* tail;
public:
dll();
~dll();
void insert(T value);
bool empty() const { return head == tail; };
};
template <typename T> dll<T>::dll() {
head = nullptr;
tail = head;
}
template <typename T> dll<T>::~dll() {
delete[] head;
}
template <typename T> void dll<T>::insert(T value) {
Node *node = new Node;
node->data = value;
// Case 1: There are no nodes yet
if (head == nullptr) {
node->prev = nullptr;
node->next = nullptr;
head = node;
tail = head;
}
else {
// Case 2: There is more than one node
Node *curr = head;
if (curr->next != nullptr)
{
while (curr->next) {
// If the value is less than the current value
if (value < curr->data) {
Node *temp = new Node;
temp->data = curr->data;
temp->next = curr->next;
temp->prev = curr->prev;
node->next = temp;
node->prev = temp->prev;
curr->prev = node;
}
curr = curr->next;
}
}
// Case 3: There is only one node
else {
node->prev = head;
node->next = nullptr;
tail = node;
}
}
}
int main() {
dll<int> list;
list.insert(10);
list.insert(20);
list.insert(15);
}
我遇到的问题是我的插入功能。我正在使用调试器并单步执行代码:list.insert(10);。
它正确地进入了第一种情况,其中head == nullptr并创建了Node。 当我进入下一行代码(list.insert(20))时,它会用这一行创建一个节点:Node * node = new Node; 但它正在创建具有head指向的内存地址的节点。
我对head变量进行了监视,节点变量和内存地址是相同的。基本上它创建的节点与上次插入时相同。
我不知道如何获取该行:Node * code = new Node;创造新的东西。我在这里使用新关键字错了吗?
答案 0 :(得分:1)
为了简化Node的初始化,让我们添加一个合理的构造函数,将prev和next成员初始化为null。这使得以后的代码更容易。
struct Node {
Node* prev;
Node* next;
T data;
Node() : prev(nullptr), next(nullptr)
{
}
};
在链表问题中总有四种情况需要注意。其中一些你得到了。插入空列表。插入列表的前面,插入列表的末尾和中间。
template <typename T> void dll<T>::insert(T value) {
Node *node = new Node;
node->data = value;
// Case 1: There are no nodes yet
if (head == nullptr) {
head = node;
tail = head;
return;
}
// case 2 - inserting at the head of the list
if (node->data < head->data)
{
node->next = head;
head = node;
return;
}
// case 3 - inserting at the end of the list
if (node->data >= tail->data)
{
node->prev = tail;
tail->next = node;
tail = node;
return;
}
// general case - inserting into the middle
Node* probe = head;
while (probe && (node->data >= probe->data))
{
probe = probe->next;
}
if (probe)
{
node->next = probe;
node->prev = probe->prev;
probe->prev->next = node;
probe->prev = node;
return;
}
// error - we shouldnt' reach this point. If we did, it meant the list was out of order to begin with.
return;
}
答案 1 :(得分:0)
首先,析构函数无效。这句话
delete[] head;
表示head是一个数组。但是头不是数组。它是指向Node类型的单个对象的指针。您必须删除析构函数中列表的所有节点。析构函数可以按以下方式查找
template <typename T>
dll<T>::~dll()
{
while ( head )
{
Node *tmp = head;
head = head->next;
delete tmp;
}
}
至于方法插入,那么它看起来非常简单。例如
template <typename T>
void dll<T>::insert( const T &value )
{
Node *current = head;
Node *previous = nullptr;
while ( current && !( value < current->data ) )
{
previous = current;
current = current->next;
}
Node *node = new Node { previous, current, value };
if ( previous == nullptr ) head = node;
else previous->next = node;
if ( current == nullptr ) tail = node;
else current->prev = node;
}
没有任何需要和理由将构造函数添加到结构Node中。当它是一个聚合时它会更好。
这是一个测试程序
#include <iostream>
template <typename T>
class dll
{
private:
struct Node
{
Node *prev;
Node *next;
T data;
};
Node *head;
Node *tail;
public:
dll();
~dll();
void insert( const T &value);
bool empty() const { return head == tail; };
void print() const;
};
template <typename T>
dll<T>::dll()
{
head = tail = nullptr;
}
template <typename T>
dll<T>::~dll()
{
while ( head )
{
Node *tmp = head;
head = head->next;
delete tmp;
}
}
template <typename T>
void dll<T>::insert( const T &value )
{
Node *current = head;
Node *previous = nullptr;
while ( current && !( value < current->data ) )
{
previous = current;
current = current->next;
}
Node *node = new Node { previous, current, value };
if ( previous == nullptr ) head = node;
else previous->next = node;
if ( current == nullptr ) tail = node;
else current->prev = node;
}
template <typename T>
void dll<T>::print() const
{
for ( Node *current = head; current; current = current->next )
{
std::cout << current->data << ' ';
}
}
int main()
{
dll<int> list;
list.insert( 10 );
list.insert( 20 );
list.insert( 15 );
list.print();
std::cout << std::endl;
return 0;
}
输出
10 15 20