用VC++使用类的思想学习数据结构会比较好理解。
给个例子如下。
头文件:
typedef struct pronode
{
int wx;
int up;
int down;
boolean haverule;
CString rule;
CString name;
pronode *next;
}mypro;
class profile
{
private:
int len;
mypro node;
mypro *headnode;
public:
profile()
{
len=0;
headnode=new mypro;
headnode->next=NULL;
}
void addnode(mypro node);
bool instert(int n,mypro node);
bool delnode(int n);
mypro* getnode(int n);
int getlen();
};
CPP文件
//-----------------------------------
//profile
//-----------------------------------
void profile::addnode(mypro node)
{
mypro *addnode,*getnode,*nextnode;
if(headnode->next==NULL)
{
addnode=new mypro;
(*addnode).down=node.down;
(*addnode).up=node.up;
(*addnode).wx=node.wx;
(*addnode).haverule=node.haverule;
(*addnode).name.GetBuffer(100);
(*addnode).name.Insert((*addnode).name.GetLength(),(LPCTSTR)node.name);
(*addnode).name.ReleaseBuffer();
(*addnode).rule.GetBuffer(100);
(*addnode).rule.Insert((*addnode).rule.GetLength(),(LPCTSTR)node.rule);
(*addnode).rule.ReleaseBuffer();
(*addnode).next=NULL;
headnode->next=addnode;
len++;
}
else
{
getnode=headnode;
nextnode=headnode;
do
{
getnode=nextnode;
nextnode=getnode->next;
}while(nextnode!=NULL);
addnode=new mypro;
getnode->next=addnode;
(*addnode).down=node.down;
(*addnode).up=node.up;
(*addnode).wx=node.wx;
(*addnode).haverule=node.haverule;
(*addnode).name.GetBuffer(100);
(*addnode).name.Insert((*addnode).name.GetLength(),(LPCTSTR)node.name);
(*addnode).name.ReleaseBuffer();
(*addnode).rule.GetBuffer(100);
(*addnode).rule.Insert((*addnode).rule.GetLength(),(LPCTSTR)node.rule);
(*addnode).rule.ReleaseBuffer();
(*addnode).next=NULL;
(*getnode).next=addnode;
len++;
}
}
mypro* profile::getnode(int n)
{
int intpos=0;
mypro *renode,*getnode,*nextnode;
renode=new mypro;
(*renode).down=-1;
(*renode).up=-1;
(*renode).wx=-1;
(*renode).haverule=false;
(*renode).name="";
(*renode).rule="";
(*renode).next=NULL;
if(n>len-1)
{
return renode;
delete renode;
}
else
{
nextnode=headnode;
getnode=headnode;
do
{
getnode=nextnode;
nextnode=getnode->next;
intpos++;
}while(intpos<=n);
renode->down=nextnode->down;
renode->up=nextnode->up;
renode->name=nextnode->name;
renode->haverule=nextnode->haverule;
renode->rule=nextnode->rule;
renode->next=nextnode->next;
renode->wx=nextnode->wx;
return renode;
delete renode;
}
}
int profile::getlen()
{
return len-1;
}
bool profile::delnode(int n)
{
int intpos=0;
mypro *getnode,*nextnode,*delnode;
if(n>len-1)
return false;
else
{
nextnode=new mypro;
getnode=headnode;
do
{
nextnode=getnode->next;
getnode=nextnode;
intpos++;
}while(intpos<n);
delnode=nextnode->next;
nextnode->next=nextnode->next->next;
delete delnode;
len--;
return true;
}//if(n>len-1)
}
bool profile::instert(int n,mypro node)
{
int intpos=0;
mypro *getnode,*nextnode,*addnode1;
if(n>len-1)
return false;
else
{
addnode1=new mypro;
nextnode=headnode;
do
{
getnode=nextnode->next;
nextnode=getnode;
(*addnode1).next=nextnode->next;
}while(++intpos<=n);
if(nextnode->next=NULL)
this->addnode(node);
else
{
(*addnode1).down=node.down;
(*addnode1).up=node.up;
(*addnode1).wx=node.wx;
(*addnode1).haverule=node.haverule;
(*addnode1).name=node.name;
(*addnode1).rule=node.rule;
nextnode->next=addnode1;
}
len++;
return true;
}//if(n>len-1)
}