#include <iostream>
#include <string.h>
using namespace std;
//一个类是cLink,它主要用来管理链表中每个结点中的数据以及指向下一个结点的指针;
//另一个类是cList,它主要用来对链表进行管理并提供对链表的操作。
class clink
{
friend class clist;
clink* next;
char name[20];
int age;
public:
clink()
{
strcpy(name,"");
age=0;
}
clink(const char* x,const int y)
{
strcpy(name,x);
age=y;
}
clink(const clink& cl)
{
strcpy(name,cl.name);
age=cl.age;
}
void disp()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
}
};
class clist
{
clink* first;
clink* last;
public:
clist(){ first=last=new clink;}
clist& insert_head(const clink& node)
{
clink* ptr=first;
first=new clink;
*first=node;
first->next=ptr;
return(*this);
}
clist& insert_tail(const clink& node)
{
clink* ptr=last;
last=new clink;
*ptr=node;
ptr->next=last;
return(*this);
}
void disp()
{
cout<<"=====list====="<<endl;
clink* now=first;
while(now!=last)
{
now->disp();
now=now->next;
}
delete now;
}
void deletall()
{
while(first!=last)
{
clink* pre=first;
first=first->next;
delete pre;
}
}
};
int main(int argc, char** argv)
{
clist list1,list2;
clink stu1("yellow",21);
clink stu2("blue",20);
list1.insert_head(stu1);
list1.insert_head(stu2);
list1.disp();
list2.insert_tail(stu1);
list2.insert_tail(stu2);
list2.disp();
list1.deletall();
list1.disp();
}
运行结果