/**设计一个算法,使用AvailSpace类,实现类似程序2-11的功能,建立一个采用模拟指针的单链表**/
#include
using namespace std;
template
struct Node
{
Node(const T& e=T(),int next=0):element(e),link(next){}
T element;
int link;
};
template
class AvailSpace
{
public:
AvailSpace(int maxSize=100);
~AvailSpace(){delete[] nodes;}
int NewNode();
void deleteNode(int i);
friend void BuildList();
private:
int numberofNodes;
Node* nodes;
int avail;
};
template
AvailSpace::AvailSpace(int maxSize)
{
numberofNodes=maxSize;
avail=0;
nodes=new Node[numberofNodes];
for(int i=0;inodes[numberofNodes-1].link=-1;
}
template
int AvailSpace::NewNode()
{
if(avail>-1)
{
int i=avail;
avail=nodes[avail].link;
return i;
}
else return -1;
}
template
void AvailSpace::deleteNode(int i)
{
nodes[i].link=avail;
avail=i;
i=-1;
}
void BuildList()
{
int number;
char c=0;
int i;
cout<<"please input the number of nodes"< cin>>number;
static AvailSpace head=AvailSpace(number);
cout<<"please input the nodes,* for end"< while(number>=0&&(c!='*'))
{
cin>>c;
i=head.NewNode();
if(i<0)
cout<<"i="< else
{
head.nodes[i].element=c;
cout<<"nodes[i].element="< }
number--;
}
}
void main()
{
BuildList();
}
阅读(1246) | 评论(0) | 转发(0) |