ThreeLetterList.h头文件源代码:
#ifndef
THREE_LETTER_LIST
#define
THREE_LETTER_LIST
class
ThreeLetterList
{
private:
//嵌套类
class
ThreeLetterNode
{
public:
char
data[3];
ThreeLetterNode*
link; //指向下一个节点
};
ThreeLetterNode*
first;
public:
void
test();
void
show();
};
void
ThreeLetterList::test()
{
ThreeLetterNode*
f=new ThreeLetterNode();
f->data[0]='B';
f->data[1]='A';
f->data[2]='T';
f->link=0;
first=f;
f=new
ThreeLetterNode();
f->data[0]='C';
f->data[1]='A';
f->data[2]='T';
f->link=0;
first->link=f;
}
void
ThreeLetterList::show()
{
std::cout<<first->data[0]
<<first->data[1]
<<first->data[2]<<"
-> ";
std::cout<<first->link->data[0]
<<first->link->data[1]
<<first->link->data[2]<<std::endl;
}
#endif
主程序源代码:
#include
#include"ThreeLetterList.h"
using
namespace std;
class
List;
//节点类
class
Node
{
friend
class List;
private:
int
data; //节点数据
Node*
link; //指向下一个节点的指针
};
//链表类
class
List
{
public:
void
test();
void
show();
private:
Node*
first; //指向第一个节点的指针
};
void
List::test()
{
Node*
f=new Node();
f->data=44;
f->link=0;
first=f;
f=new
Node();
f->data=72;
f->link=0;
first->link=f;
f=new
Node();
f->data=210;
f->link=0;
first->link->link=f;
}
void
List::show()
{
cout<<first->data<<"
-> ";
cout<<first->link->data<<"
-> ";
cout<<first->link->link->data<<endl;
}
int
main()
{
List
a;
a.test();
a.show();
ThreeLetterList
b;
b.test();
b.show();
cout<<"OK"<<endl;
return
0;
}
运行结果: