#include<iostream.h>
class CWnodeChain;
class CWnode
{
friend CWnodeChain;
private:
int data;
CWnode *link;
};
class CWnodeChain
{
public:
CWnodeChain(){first=0;}
~CWnodeChain();
int Length();
CWnodeChain Insert(int nd);
void Output();
private:
CWnode *first;
};
CWnodeChain::~CWnodeChain()
{
CWnode *p=first;
while(first)
{
p=first->link;
delete first;
first=p;
}
}
int CWnodeChain::Length()
{
CWnode *p;
int len=0;
for(p=first;p;p->link)
{
len++;
}
return len;
}
CWnodeChain CWnodeChain::Insert(int nd)
{
CWnode *q=new CWnode;
q->data=nd;
//从头接点插入
if(!first)
{
first=q;
}
else
{
q->link=first;
first=q;
}
return *this;
}
void CWnodeChain::Output() //输出
{
CWnode *p;
for(p=first;p;p=p->link)
cout<<p->data<<endl;
}
void main()
{
CWnodeChain obj;
cout<<obj.Length()<<endl;
obj.Insert(3);
cout<<obj.Length()<<endl;
obj.Output();
}
MM们
相关分类