C++ 中的双向链表(释放的指针未分配)
Doubly Linked List in C++ (Pointer being freed was not allocated)
有人可以告诉我哪里出错了吗?我认为问题出在 clear() 函数上,但我不完全确定。目标是创建一个双向链表。我仍然需要添加一些功能,但我觉得我已经添加了足够的 运行 我在 main() 中提供的内容。
#include <iostream>
using namespace std;
template <class T>
class doubLList;
template <class T>
class doubNode{
T data;
doubNode<T>* next;
doubNode<T>* prev;
public:
doubNode(T data = T(), doubNode<T>* prev = nullptr, doubNode<T>* next = nullptr) : data(data),
next(next), prev(prev) {}
friend doubLList<T>;
T& returnData() {return data;}
doubNode<T>*& returnNext() {return next;}
};
template <class T>
class doubLList{
doubNode<T>* head;
doubNode<T>* tail;
public:
doubLList() {head = new doubNode<T>; tail = new doubNode<T>; head->next = tail; tail->prev = head; cout<< "constructed" << endl;}
doubLList(const doubLList<T>& copy);
~doubLList();
doubLList<T>& operator=(const doubLList<T>& rhs);
void clear();
void insert(const T& data) {head->next = new doubNode<T>(data, head->next, head); head->next->next->prev = head->next;}
doubNode<T>*& returnHead() {return head;}
};
template <class T>
void doubLList<T>::clear(){
cout << "Clear" << endl;
while(head->next != tail){
cout << "Run" << endl;
doubNode<T>* delNode = head->next;
head->next = delNode->next;
head->next->prev = head;
delete delNode;
}
}
template <class T>
doubLList<T>::doubLList(const doubLList<T>& copy){
head = new doubNode<T>;
tail = new doubNode<T>;
head->next = tail;
tail->prev = head;
*this = copy;
}
template <class T>
doubLList<T>& doubLList<T>::operator=(const doubLList<T>& rhs) {
if(this == &rhs){
return *this;
}
clear();
doubNode<T>* rhsPtr = rhs->next;
while(rhs->next->next){
tail->prev = new doubNode<T>(rhsPtr->data, tail, tail->prev);
tail->prev->prev->next = tail->prev;
rhsPtr = rhsPtr->next;
}
}
template <class T>
doubLList<T>::~doubLList(){
clear();
delete head;
delete tail;
head = nullptr;
tail = nullptr;
}
int main() {
doubLList<int>test;
cout << test.returnHead() << endl;
test.insert(1);
return 0;
}
提前致谢!
以下提供了正确的实现:
已更改
doubNode(T data = T(), doubNode<T>* prev = nullptr, doubNode<T>* next = nullptr) : data(data),
next(next), prev(prev) {}
至
doubNode(T data = T(), doubNode<T>* next = nullptr, doubNode<T>* prev = nullptr) : data(data),
next(next), prev(prev) {}
有人可以告诉我哪里出错了吗?我认为问题出在 clear() 函数上,但我不完全确定。目标是创建一个双向链表。我仍然需要添加一些功能,但我觉得我已经添加了足够的 运行 我在 main() 中提供的内容。
#include <iostream>
using namespace std;
template <class T>
class doubLList;
template <class T>
class doubNode{
T data;
doubNode<T>* next;
doubNode<T>* prev;
public:
doubNode(T data = T(), doubNode<T>* prev = nullptr, doubNode<T>* next = nullptr) : data(data),
next(next), prev(prev) {}
friend doubLList<T>;
T& returnData() {return data;}
doubNode<T>*& returnNext() {return next;}
};
template <class T>
class doubLList{
doubNode<T>* head;
doubNode<T>* tail;
public:
doubLList() {head = new doubNode<T>; tail = new doubNode<T>; head->next = tail; tail->prev = head; cout<< "constructed" << endl;}
doubLList(const doubLList<T>& copy);
~doubLList();
doubLList<T>& operator=(const doubLList<T>& rhs);
void clear();
void insert(const T& data) {head->next = new doubNode<T>(data, head->next, head); head->next->next->prev = head->next;}
doubNode<T>*& returnHead() {return head;}
};
template <class T>
void doubLList<T>::clear(){
cout << "Clear" << endl;
while(head->next != tail){
cout << "Run" << endl;
doubNode<T>* delNode = head->next;
head->next = delNode->next;
head->next->prev = head;
delete delNode;
}
}
template <class T>
doubLList<T>::doubLList(const doubLList<T>& copy){
head = new doubNode<T>;
tail = new doubNode<T>;
head->next = tail;
tail->prev = head;
*this = copy;
}
template <class T>
doubLList<T>& doubLList<T>::operator=(const doubLList<T>& rhs) {
if(this == &rhs){
return *this;
}
clear();
doubNode<T>* rhsPtr = rhs->next;
while(rhs->next->next){
tail->prev = new doubNode<T>(rhsPtr->data, tail, tail->prev);
tail->prev->prev->next = tail->prev;
rhsPtr = rhsPtr->next;
}
}
template <class T>
doubLList<T>::~doubLList(){
clear();
delete head;
delete tail;
head = nullptr;
tail = nullptr;
}
int main() {
doubLList<int>test;
cout << test.returnHead() << endl;
test.insert(1);
return 0;
}
提前致谢!
以下提供了正确的实现:
已更改
doubNode(T data = T(), doubNode<T>* prev = nullptr, doubNode<T>* next = nullptr) : data(data),
next(next), prev(prev) {}
至
doubNode(T data = T(), doubNode<T>* next = nullptr, doubNode<T>* prev = nullptr) : data(data),
next(next), prev(prev) {}