主程序中的错误如何改正,及让程序正常运行,不使用空结点
cpp:
#include<iostream>
#include"list.h"
using namespace std;
class Complex
{
double re, im; //实部和虚部
double len; //复数的模
public:
Complex(double re = 0, double im = 0) //构造
{
this->re = re; this->im = im;
}
friend ostream& operator << (ostream& os, const Complex& c) //输出复数
{
os << c.re << " + " << c.im<<"i";
return os;
}
friend istream& operator >> (istream& is, Complex& c) //读入复数
{
is >> c.re >> c.im;
return is;
}
//添加其他代码
void operator=(Complex& p)
{
re = p.re;
im = p.im;
}
bool operator==(Complex& p)
{
if (re == p.re && im == p.im)
return 1;
return 0;
}
};
int main()
{
List<Complex> clist; //创建空的复数链表(不带链头空节点)
cout << clist << endl;
cout << endl;
Complex c;
cout << "...建立复数链表..." << endl << endl;
cout << "输入6组复数并插入链表:" << endl;
for (int i = 0; i < 6; i++)
{
cin >> c; //读入复数
clist.insertRear(c); //将复数插在链尾
}
cout << clist << endl;
cout << endl;
cout << "...测试查找一个关键字节点的前节点..." << endl << endl;
cout << "输入待查找复数:" << endl;
cin >> c;
Node<Complex>* pFind = 0, *pFront = 0; //关键字节点及其前节点的指针
pFind = clist.search(c);
if (pFind)
{
pFront = clist.findFront(pFind);
}
cout << pFront->info << endl; //输出前节点值
Node<Complex>* pDel = clist.delP(pFront); //
cout << endl;
cout << "...测试删除关键字节点..." << endl << endl;
cout << "输入待查找复数:" << endl;
cin >> c;
pDel = clist.delData(c);
if (!pDel)
cout << c << "不在链表中" << endl;
cout << clist << endl;
return 0;
}
h:
#include <iostream>
using namespace std;
template <typename T> class List;
template <typename T> class Node
{
public:
T info; //节点数据
Node<T> *link; //节点指针
Node() { link = 0; } //创建空节点
Node(const T&data)
{
info = data; link = 0;
} //创建节点并初始化
void InsertAfter(Node<T>*); //将参数节点接在本节点后
Node<T>* RemoveAfter(); //删除本节点的后节点,返回删除节点地址,若为空,则表示无法删除
friend class List<T>;
};
template <typename T>
void Node<T>::InsertAfter(Node<T>* p)
{
p->link = link;
link = p;
}
template <typename T>
Node<T>* Node<T>::RemoveAfter()
{
Node<T>* after = link; //指向本节点的后节点
if (after)
link = after->link;
return after;
}
template <typename T>
class List
{
public:
Node < T > *head, *tail;
List() //构造空链表,无链头空节点
{
head = 0; //添加代码
tail = 0;
}
~List() //析构链表
{
while (head->link != NULL) //添加代码
{
Node<T>* temppoint;
temppoint = head->link;
head->link = temppoint->link;
delete temppoint;
}
tail = head;
delete head;
}
bool isEmpty() { return head == 0 && tail == 0; } //判断是否空链表
friend ostream& operator << (ostream& os, List<T>& list) //重载运算符用于输出链表
{
if (list.isEmpty()) os << "空链表!" << endl;
else
{
Node<T> *p =list.head;
while (p)
{
os << p->info << " ";
p = p->link;
}
}
return os;
}
//添加声明:将数据节点插在链尾
void insertRear(T k);
//添加声明: 在链表中查找数据节点,返回节点指针
List<T>* search(Node<T> k);
//添加声明:删除链表中给定节点(给出待删除节点地址)
void delP(Node<T>* point);
//添加声明:删除链表中给定数据节点(给出待删除数据)
void delData(Node<T> k);
//添加声明:获取给定节点的前节点
List<T>* findFront(Node<T>* p);
};
//添加代码:补充以上函数声明的定义
template <typename T>
void List<T>::insertRear(T k) //将数据节点插在链尾
{
/*if (head == 0 && tail == 0)
{
head->info = k;
tail = head->link;
}*/
Node<T>* temppoint=NULL;
temppoint->info = k;
temppoint->link =tail->link;
tail->link = temppoint;
tail = temppoint;
/*if (head == 0 && tail == 0)
{
head = new Node<T>;
tail = head;
tail->info = k;
tail->link = 0;
return;
}
tail->link = new Node<T>;
tail = tail->link;
tail->info = k;
tail->link = 0;
return;*/
}
template <typename T>
List<T>* List<T> ::search(Node<T> k) //在链表中查找数据节点,返回节点指针
{
Node<T>* temppoint = head;
while (temppoint->info != k&&temppoint->link!=NULL)
{
temppoint = temppoint->link;
}
if (temppoint->info == k)
{
cout << "找到该数据节点" << endl;
return temppoint;
}
else if (temppoint->info != k)
{
cout << "该链表中不含该元素" << endl;
return 0;
}
}
template <typename T>
void List<T>::delP(Node<T>* point) //删除链表中给定节点(给出待删除节点地址)
{
Node<T>* temppoint;
temppoint = point.findFront();
if (point->link != NULL)
{
temppoint->link = point->link;
}
else if (point->link == NULL)
{
temppoint->link = NULL;
}
delete point;
}
template <typename T>
void List<T>::delData(Node<T> k) //删除链表中给定数据节点(给出待删除数据)
{
Node<T>* temppoint = search(k);
delP(temppoint);
}
template <typename T>
List<T>*List<T>:: findFront(Node<T>* p) //获取给定节点的前节点
{
Node<T>* temppoint = head;
while (temppoint->link != p)
{
temppoint = temppoint->link;
}
return temppoint;
}