#include //指示编译器将文件iostream中的代码嵌入到该程序中该指令所在的地方
using namespace std;
#include //包含文件流头文件
#define BUFSIZE 50 //定义BUFSIZE 的值为50
class mGuestroom;
template
class cNode //节点
{
public:
T data;
cNode *next; //到下一个节点
};
template //创建模板
class tList
{
public: //定义成员函数
tList();
void Create(); //创建链表
bool Empty() const; //判断链表是否为空
void Insert(const T e); //从尾部插入一个元素
cNode* GetcNode(int i); //返回第i个节点
bool Find(const T e); //在链表中查找某个值
~tList(); //销毁链表,析构
cNode *head; //头节点
};
template
tList::tList() //类的成员函数的实现
{
head = new cNode; //头节点
head->next = NULL; //以null为尾节点
}
template
void tList::Create()
{
cNode *p;
p = head;
p->next = NULL;
}
template
bool tList::Empty() const
{
return (head->next == NULL);
}
template
void tList::Insert(const T e) //插入
{
cNode *p, *q;
p = head;
q = new cNode;
q->data = e;
while (p->next)
{
p = p->next;
}
p->next = q;
q->next = NULL;
}
template
cNode* tList::GetcNode(int i) //返回第i个节点
{
int k = 0;
cNode *p;
p = head;
while (p && k < i)
{
p = p->next;
++k;
}
return p;
}
template
bool tList::Find(const T e) //在链表中查找某个值
{
bool flag = false;
cNode *p;
p = head->next;
while (p)
{
if (p->data == e)
{
flag = true;
break;
}
p = p->next;
}
return flag;
}
template
tList::~tList()
{
cNode *p;
while (head)
{
p = head->next;
delete head;
head = p;
}
}