感觉写的析构函数有点不太对,恳请大佬们帮忙指出该怎么改!
#include <iostream>
using namespace std;
#define ERROR 0
#define OK 1
class Node
{
public:
int data;
Node* prior;//前驱
Node* next;//后继
};
class DoubleLink
{
public:
DoubleLink();
~DoubleLink();
int DoubleCreate(int n);//尾插法创建链表,n个元素
int DoublePrint();//遍历
int DoubleInsert_Index(int i, int elem);//在指定位置i插入元素
int DoubleInsert_end(int elem);//尾插元素
int DoubleInsert_head(int elem);//头插元素
int DoubleDelete_Index(int i);//在指定位置i删除元素
int DoubleDelete_end();//尾删
private:
Node* head;
};
DoubleLink::DoubleLink()
{
head = new Node;
head->data = 0;
head->prior = NULL;
head->next = NULL;
}
DoubleLink::~DoubleLink()
{
//第一种:
Node* p;
while (head->next != NULL)
{
p = head->next;
head->next = p->next;
p->next->prior = head;
delete p;
}
//第二种:销毁链表
//Node* p = head;
//while (p != NULL)
//{
// head = head->next;
// delete p;
// p = head;
//}
}
int DoubleLink::DoubleCreate(int n)//尾插法创建链表
{
Node* p = head;
p->prior = NULL;
for (int i = 0; i < n; i++)
{
Node* s = new Node;//要插入的结点
cout << "输入第" << i + 1 << "个元素:" << endl;
cin >> s->data;
s->prior = p;
s->next = NULL;
p->next = s;
p = s;//将新插入的结点s 设置为当前结点
}
cout << "创建成功!" << endl;
return OK;
}
int DoubleLink::DoublePrint()//遍历
{
Node* p = head;
if (!p)
{
cout << "链表为空" << endl;
return ERROR;
}
cout << "遍历链表:";
while (p->next)
{
cout << p->next->data << " ";
p = p->next;
}
cout << endl;
}
//在指定位置 i 插入元素 elem
int DoubleLink::DoubleInsert_Index(int i, int elem)
{
Node* p = head;
Node* s = new Node;
int j = 1;
while (p->next != NULL && j < i)
{
p = p->next;
++j;
}
if (p->next == NULL || j > i)
{
cout << "插入位置有误!" << endl;
return ERROR;
}
else
{
s->data = elem;
s->prior = p;
s->next = p->next;
p->next->prior = s;
p->next = s;
cout << "插入成功!" << endl;
return OK;
}
}
int DoubleLink::DoubleInsert_end(int elem)//尾插
{
Node* p = head;
Node* s = new Node;
while (p->next != NULL)//寻找最后一个结点
{
p = p->next;
}
s->data = elem;
s->prior = p;
p->next = s;
s->next = NULL;
cout << "尾插入成功!" << endl;
return OK;
}
int DoubleLink::DoubleInsert_head(int elem)//头插元素
{
Node* s = new Node;//待插入的元素结点
s->data = elem;
s->prior = head;
s->next = head->next;
if (head->next != NULL)
{
head->next->prior = s;
}
head->next = s;
cout << "头插成功!" << endl;
return OK;
}
int DoubleLink::DoubleDelete_Index(int i)//在指定位置i删除元素
{
Node* p = head->next;
//Node* q = new Node;
int j = 1;
while (p->next != NULL && j < i)
{
p = p->next;
++j;
}
if (p->next == NULL || j > i)
{
cout << "删除位置错误!" << endl;
return ERROR;
}
else
{
p->prior->next = p->next;
p->next->prior = p->prior;
delete p;
cout << "删除成功!" << endl;
return OK;
}
}
int DoubleLink::DoubleDelete_end()//删除尾结点
{
Node* p = head;
while (p->next != NULL)//寻找到最后一个结点
{
p = p->next;
}
p->prior->next = NULL;
return OK;
}
int main()
{
DoubleLink DL;
DL.DoubleCreate(3);//依次输入:1 2 3
DL.DoublePrint();
DL.DoubleInsert_Index(2, 9);//在第2个位置插入9,结果 === 1 9 2 3
DL.DoublePrint();
DL.DoubleInsert_end(8);//尾插9, === 1 9 2 3 8
DL.DoubleInsert_head(7);//头插7,=== 7 1 9 2 3 8
DL.DoublePrint();
DL.DoubleDelete_Index(3);//删除第3个位置,=== 7 1 2 3 8; 注:不能删除尾结点
DL.DoubleDelete_end();//删除尾结点 === 7 1 2 3
DL.DoublePrint();
system("pause");
return 0;
}
创建链表时依次输入: 1 2 3
结果如图: