志在指尖
用双手敲打未来

单链表类

publicclassNode
{
publicTData{get;set;}
publicNodeNext{get;set;}
publicNode(Titem)
{
this.Data=item;
this.Next=null;
}
publicNode()
{
this.Data=default(T);
this.Next=null;
}
}[/mw_shl_code
[mw_shl_code=csharp,true]publicclassLinkList
{
publicNodeHead{get;set;}C#
publicLinkList()
{
Head=null;
}
///
///添加新元素到链尾
///
///
publicvoidAppend(Titem)
{
Nodefoot=newNode();
NodeA=newNode();
if(Head==null)
{
Head=foot;
return;
}
A=Head;
while(A.Next!=null)
{
A=A.Next;
}
A.Next=foot;
}
///
///删去链表元素
///
publicvoidDelete(inti)
{
if(i==1)//表头
{
Head=Head.Next;
return;
}
NodeA=newNode();
NodeB=newNode();
B=Head;
intj=1;
while(B.Next!=null&&j
{
A=B;
B=B.Next;
j++;
}
if(j==i)
{
A.Next=B.Next;
}
}
///
///核算链表元素个数
///
///
publicintGetLength()
{
Nodep=Head;
intlength=0;
while(p!=null)
{
p=p.Next;
length++;
}
returnlength;
}
///
///判断链表是否为空
///
///
publicboolIsEmpty()
{
if(Head==null)
returntrue;
else
returnfalse;
}
///
///清空链表
///
publicvoidClear()
{
Head=null;
}
///
///获取当前方位的结点值
///
///
///
publicTGetNodeValue(inti)
{
if(IsEmpty()||i<1||i>GetLength())
{
Console.WriteLine(“单链表为空或结点方位有误!”);
returndefault(T);
}
NodeA=newNode();
A=Head;
intj=1;
while(A.Next!=null&&j
{
A=A.Next;
j++;
}
returnA.Data;
}
///
///添加单链表刺进的方位
///
///
///
publicvoidInsert(Titem,intn)
{
if(IsEmpty()||n<1||n>GetLength())
{
Console.WriteLine(“单链表为空或结点方位有误!”);
return;
}
if(n==1)
{
NodeH=newNode();
H.Next=Head;
Head=H;
return;
}
//==========================取n结点的值===============
NodeA=newNode();
NodeB=newNode();
B=Head;
intj=1;
while(B.Next!=null&&j<n)
{
A=B;
B=B.Next;
j++;
}
//end=======================取n结点的值===============
if(j==n)
{
NodeC=newNode(item);
A.Next=C;
C.Next=B;
}
}
}

未经允许不得转载:IT技术网站 » 单链表类
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载