C++模拟链表迭代器析构异常
我在用C++写链表和迭代器的时候,会在return语句析构的时候出错,如果只是定义一个链表构造析构都没问题,但是如果构造一个迭代器的时候,析构迭代器的时候会把它指向的链表也析构掉,然后还会去调用链表的析构函数,但是这时候已经没有链表了,找不到头节点的什么,就报错了,这是我显示调用析构函数看出来的,我用的编程软件是vs2015,很急,求各位大神赐教
#include <iostream>
using namespace std;
template<class Type> class ListNode;
template<class Type> class List;
template<class Type> class ListIterator;
//ListNode类
template<class Type>
class ListNode {
friend class List<Type>;
friend class ListIterator<Type>;
private:
Type data; //节点数据
ListNode<Type> *link; //节点链表指针
public:
ListNode();
ListNode(const Type &item);
ListNode<Type> *NextNode() { //给出当前结点的下一节点地址
return link;
}
void InsertAfter(ListNode<Type> *p);//在当前节点后插入节点p
ListNode<Type> *RemoveAfter(); //摘下当前节点的下一节点
};
template<class Type>
ListNode<Type>::ListNode() :link(NULL) {}
template<class Type>
ListNode<Type>::ListNode(const Type &item)
: data(item), link(NULL) {}
template<class Type>
void ListNode<Type>::InsertAfter(ListNode<Type> *p) {
p->link = link;
link = p;
}
template<class Type>
ListNode<Type> *ListNode<Type>::RemoveAfter() {
ListNode<Type> *tempptr = link;
if (link == NULL) {
return NUll;
}
link = tempptr->link;
return tempptr;
}
//List类
template<class Type>
class List {
friend class ListIterator<Type>;
private:
ListNode<Type> *first, *last;
public:
ListNode<Type> *GetNode(const Type &item, ListNode<Type> *next); // 创建数据为item,指针为next的新节点
List(const Type &value) { //构造函数
last = first = new ListNode<Type>(value);
}
~List(); //析构函数
void MakeEmpty(); //链表置空
int Length()const; //求链表的长度
ListNode<Type> *Find(Type value);
ListNode<Type> *FindByIndex(int i);
int Insert(Type value, int i);
Type *Remove(int i);
Type *Get(int i);
Type Get_Min();//使用非递归方式返回链表中最小的元素
ListNode<Type> *reverse1();//递归
ListNode<Type> *reverse2();//非递归
};
template<class Type>
ListNode<Type> *List<Type>::GetNode(const Type &item, ListNode<Type> *next = NULL) {
ListNode<Type> *newnode = new ListNode<Type>(item);
newnode->link = next;
return newnode;
}
template<class Type> //zhangl@swc.neu.edu.cn
List<Type>::~List() {
//析构函数
MakeEmpty();
delete first;
}
template<class Type>
void List<Type>::MakeEmpty() {
//删除链表中除表头节点外的所有其他节点
ListNode<Type> *q;
while (first->link != NULL) {
q = first->link;
first->link = q->link;
delete q;
}
last = first;
}
template<class Type>
int List<Type>::Length()const {
ListNode<Type> *p = first->link;
int count = 0;
where(p != NULL) {
p = p->link;
count++;
}
return count;
}
template<class Type>
ListNode<Type> *List<Type>::Find(Type value) {
ListNode<Type> *p = first->link;
while (p != NULL&&p->data != value) {
p = p->link;
}
return p;
}
template<class Type>
ListNode<Type> *List<Type>::FindByIndex(int i) {
if (i < -1)return NULL;
if (i == -1)return first;
ListNode<Type> *p = first->link;
int j = 0;
while (p != NULL&&j < i) {
p = p->link;
j++;
}
return p;
}
template<class Type>
int List<Type>::Insert(Type value, int i) {
ListNode<Type> *p = FindByIndex(i - 1);
if (p == NULL) return 0;
ListNode<Type> *newnode = GetNode(value, p->link);
if (p->link == NULL) last = newnode;
p->link = newnode;
return 0;
}
template<class Type>
Type *List<Type>::Remove(int i) {
ListNode<Type> *p = FindByIndex(i - 1), *q;
if (p == NUll || p->link == NULL) return NULL;
q = p->link;
p->link = q->link;
Type value = new Type(q->data);
if (q == last) last = p;
delete q;
return &value;
}
template<class Type>
Type *List<Type>::Get(int i) {
ListNode<Type> *p = FindByIndex(i);
if (p == NULL || p == first) {
return NULL;
}
else {
return &p->data;
}
}
template<class Type>
Type List<Type>::Get_Min() {
ListNode<Type> *s = first;
if (s == NULL || s->link == NULL) {
return NULL;
}
s = s->link;
Type min = s->data;
while (s->link != NULL) {
s = s->link;
if (s->data < min) {
min = s->data;
}
}
}
enum Boolean { False, True };
//迭代器
template<class Type> class ListIterator {
private:
List<Type> list;
ListNode<Type> *current;
public:
ListIterator(const List<Type>& l) :list(l), current((l.first)->link) {}
};
int main() {
List<int> l1 = List<int>(1);
for (int i = 0; i < 6; i++) {
l1.Insert(i , i+1);
}
ListIterator<int> li = ListIterator<int>(l1);
/*
int *ptr = li.First();
while (ptr != NULL) {
cout << *ptr << endl;
ptr = li.Next();
}
*/
li.~ListIterator();
l1.~List();
return 1;
}