链表类具有哈希表的功能
2024-07-21 02:25:32
供稿:网友
using system;
namespace study
{
/// <summary>
/// cchain 的摘要说明。
/// </summary>
public class cchain
{
public class cchainnode
{
public object data;
public cchainnode nextchainnode;
public cchainnode previouschainnode;
public object tag;
public string key;
public cchainnode()
{
this.data=null;
this.tag=null;
this.key=null;
this.nextchainnode=this.previouschainnode=null;
}
public cchainnode(object vdata,object vtag,string vkey)
{
this.data=vdata;
this.tag=vtag;
this.key=vkey;
this.nextchainnode=this.previouschainnode=null;
}
}
public class cchainiterator
{
private cchainnode pcurrentchainnode,pfirstchainnode,plastchainnode;
private bool pbfirst,pblast;
public cchainiterator(cchainnode vfirstchainnode,cchainnode vlastchainnode)
{
this.pcurrentchainnode=this.pfirstchainnode=this.plastchainnode=null;
if(vfirstchainnode!=null)
{
pcurrentchainnode=pfirstchainnode=vfirstchainnode;
pbfirst=false;
}
if(vlastchainnode!=null)
{
pcurrentchainnode=plastchainnode=vlastchainnode;
pblast=false;
}
}
public cchainnode currentchainnode
{
get
{
return this.pcurrentchainnode;
}
}
public bool nextchainnode()
{
if(this.pfirstchainnode==null)
{
return false;
}
if(pcurrentchainnode==pfirstchainnode && pbfirst==false)
{
pbfirst=true;
return true;
}
else
{
pcurrentchainnode=pcurrentchainnode.nextchainnode;
if(pcurrentchainnode ==null)
{
return false;
}
else
{
return true;
}
}
}
public bool previouschainnode()
{
if(this.plastchainnode==null)
{
return false;
}
if(pcurrentchainnode==plastchainnode && pblast==false)
{
pblast=true;
return true;
}
else
{
pcurrentchainnode=pcurrentchainnode.previouschainnode;
if(pcurrentchainnode ==null)
{
return false;
}
else
{
return true;
}
}
}
}
public cchainnode firstchainnode;
public cchainnode lastchainnode;
public cchainnode currentchainnode;
public cchain()
{
this.firstchainnode=this.lastchainnode=this.currentchainnode =null;
}
~cchain()
{
}
public bool isempty()
{
if(this.firstchainnode==null)
{
return true;
}
else
{
return false;
}
}
public bool appendchainnode(cchainnode vchainnode)
{
if(vchainnode==null)
{
return false;
}
else
{
if(this.firstchainnode==null)
{
this.firstchainnode=vchainnode;
this.firstchainnode.previouschainnode=null;
this.firstchainnode.nextchainnode=null;
this.lastchainnode=this.currentchainnode=this.firstchainnode;
}
else
{
this.currentchainnode.nextchainnode=vchainnode;
vchainnode.previouschainnode=this.currentchainnode;
vchainnode.nextchainnode=null;
this.lastchainnode=this.currentchainnode=vchainnode;
}
return true;
}
}
public bool insertchainnode(int index,cchainnode vchainnode)
{
cchainnode pcn;
int pcount=0;
cchain.cchainiterator pci=new cchainiterator(this.firstchainnode,null);
if(vchainnode==null || index<0)
{
return false;
}
if(index==0)
{
pcn=this.firstchainnode;
vchainnode.nextchainnode=pcn;
pcn.previouschainnode=vchainnode;
this.firstchainnode=vchainnode;
vchainnode.previouschainnode=null;
return true;
}
while(pci.nextchainnode()==true)
{
pcount++;
if(pcount==index)
{
pcn=pci.currentchainnode.nextchainnode;
pci.currentchainnode.nextchainnode=vchainnode;
vchainnode.previouschainnode=pci.currentchainnode;
vchainnode.nextchainnode=pcn;
if(pcn!=null)
{
pcn.previouschainnode=vchainnode;
}
return true;
}
}
return false;
}
public bool containskey(string vkey)
{
cchain.cchainiterator pci=new cchainiterator(this.firstchainnode,null);
while(pci.nextchainnode()==true)
{
if(pci.currentchainnode.key ==vkey)
{
return true;
}
}
return false;
}
public cchain.cchainnode item(string vkey)
{
cchain.cchainiterator pci=new cchainiterator(this.firstchainnode,null);
while(pci.nextchainnode()==true)
{
if(pci.currentchainnode.key ==vkey)
{
return pci.currentchainnode;
}
}
return null;
}
public object itemdata(string vkey)
{
cchain.cchainiterator pci=new cchainiterator(this.firstchainnode,null);
while(pci.nextchainnode()==true)
{
if(pci.currentchainnode.key ==vkey)
{
return pci.currentchainnode.data;
}
}
return null;
}
public int chainnodecount()
{
int pcount=0;
cchain.cchainiterator pci=new cchainiterator(this.firstchainnode,null);
while(pci.nextchainnode()==true)
{
pcount++;
}
return pcount;
}
}
}