转贴-- 单向链表的实现 C# http://blog.csdn.net/a497785609/archive/2009/09/29/4617692.aspx
2009-12-13 16:46
513 查看
转贴-- 单向链表的实现 C# http://blog.csdn.net/a497785609/archive/2009/09/29/4617692.aspx
using System ;
public class LinkedList
{
//嵌套类表示单个节点;
private class Node
{
public Node (object values)
{
item=values ;
}
public object item; //数据域;
public LinkedList.Node next;//指针域;
public override string ToString()
{
return item.ToString ();
}
}
private int count;//记录元素个数;
public int Count
{
get {return this.count ;}
}
private Node head;//头指针;
public object this[int index]//索引器;
{
get {return GetByIndex (index).item;}
set{GetByIndex (index).item=value ;}
}
//①添加元素;
public void Add(object values)
{
Node newNode=new Node (values);
if(head ==null ) //如果头指针为空;
{
head =newNode ;
}
else
{
GetByIndex(count-1).next=newNode; //插到链表结尾;
}
count ++; //链表长度+1;
}
//②在指定索引处插入元素;
public void Insert(int index,object values)
{
Node tempNode;
if(index ==0)
{
if(head ==null )
{
tempNode =new Node (values );
tempNode.next =head ;
head =tempNode ;
}
}
else
{
Node preNode=GetByIndex(index-1); //找插入节点的前驱;
Node nextNode=preNode.next ; //找插入节点的后继结点;
tempNode =new Node (values);
preNode.next =tempNode;
tempNode.next =nextNode ;
}
count ++;
}
//③删除指定索引元素;
public void RemoveAt(int index)
{
if (index ==0) //删除节点为头指针;
{
head =head.next ;
}
else
{
Node preNode=GetByIndex(index-1);
if(preNode.next ==null)
{
throw new ArgumentOutOfRangeException("index","索引超出范围!");
}
preNode.next =preNode.next.next ;
}
count --;
}
public override string ToString()
{
string s="";
for(Node temp=head; temp!=null; temp=temp.next)
{
s+=temp.ToString ()+" ";
}
return s;
}
private Node GetByIndex(int index)
{
if((index <0)||(index >= this.count ))
{
throw new ArgumentOutOfRangeException("index","索引超出范围!");
}
Node tempNode=this.head ;
for(int i=0;i<index ;i++)
{
tempNode=tempNode.next ;
}
return tempNode ;
}
}
class App
{
static void Main()
{
LinkedList lst=new LinkedList ();
Console .WriteLine("①添加元素:");
lst .Add (0);
lst .Add (1);
lst .Add (2);
lst .Add (3);
Console .WriteLine(lst.ToString());
Console .WriteLine("②在2号位置,添加元素50:");
lst .Insert (2,50);
Console .WriteLine(lst.ToString());
Console .WriteLine("③移除1号元素:");
lst.RemoveAt(1);
Console .WriteLine(lst.ToString());
Console .WriteLine("④把2号元素赋值为9:");
lst [2]=9;
Console .WriteLine(lst.ToString());
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/a497785609/archive/2009/09/29/4617692.aspx
using System ;
public class LinkedList
{
//嵌套类表示单个节点;
private class Node
{
public Node (object values)
{
item=values ;
}
public object item; //数据域;
public LinkedList.Node next;//指针域;
public override string ToString()
{
return item.ToString ();
}
}
private int count;//记录元素个数;
public int Count
{
get {return this.count ;}
}
private Node head;//头指针;
public object this[int index]//索引器;
{
get {return GetByIndex (index).item;}
set{GetByIndex (index).item=value ;}
}
//①添加元素;
public void Add(object values)
{
Node newNode=new Node (values);
if(head ==null ) //如果头指针为空;
{
head =newNode ;
}
else
{
GetByIndex(count-1).next=newNode; //插到链表结尾;
}
count ++; //链表长度+1;
}
//②在指定索引处插入元素;
public void Insert(int index,object values)
{
Node tempNode;
if(index ==0)
{
if(head ==null )
{
tempNode =new Node (values );
tempNode.next =head ;
head =tempNode ;
}
}
else
{
Node preNode=GetByIndex(index-1); //找插入节点的前驱;
Node nextNode=preNode.next ; //找插入节点的后继结点;
tempNode =new Node (values);
preNode.next =tempNode;
tempNode.next =nextNode ;
}
count ++;
}
//③删除指定索引元素;
public void RemoveAt(int index)
{
if (index ==0) //删除节点为头指针;
{
head =head.next ;
}
else
{
Node preNode=GetByIndex(index-1);
if(preNode.next ==null)
{
throw new ArgumentOutOfRangeException("index","索引超出范围!");
}
preNode.next =preNode.next.next ;
}
count --;
}
public override string ToString()
{
string s="";
for(Node temp=head; temp!=null; temp=temp.next)
{
s+=temp.ToString ()+" ";
}
return s;
}
private Node GetByIndex(int index)
{
if((index <0)||(index >= this.count ))
{
throw new ArgumentOutOfRangeException("index","索引超出范围!");
}
Node tempNode=this.head ;
for(int i=0;i<index ;i++)
{
tempNode=tempNode.next ;
}
return tempNode ;
}
}
class App
{
static void Main()
{
LinkedList lst=new LinkedList ();
Console .WriteLine("①添加元素:");
lst .Add (0);
lst .Add (1);
lst .Add (2);
lst .Add (3);
Console .WriteLine(lst.ToString());
Console .WriteLine("②在2号位置,添加元素50:");
lst .Insert (2,50);
Console .WriteLine(lst.ToString());
Console .WriteLine("③移除1号元素:");
lst.RemoveAt(1);
Console .WriteLine(lst.ToString());
Console .WriteLine("④把2号元素赋值为9:");
lst [2]=9;
Console .WriteLine(lst.ToString());
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/a497785609/archive/2009/09/29/4617692.aspx
相关文章推荐
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分(来源:http://blog.csdn.net/yangjundeng/archive/2005/03/17/321920.aspx)
- 转贴一个JAVA数据库连接大全!!_http://blog.csdn.net/lxblg
- C#单向链表的实现
- JavaScript实现DropDownList(Select)三级联动无刷新 (http://blog.csdn.net/itfly8/archive/2007/07/05/1679102.aspx)
- j2ee程序员应该掌握的linux知识(转贴来自http://blog.csdn.net/yzhz)
- C#向Excel报表中插入图片的2种方法 http://blog.csdn.net/net_lover/archive/2007/07/23/1702797.aspx
- 线性表的链式存储结构_单向链表[带头结点]_C#实现
- [转]C#数据库连接字符串 [http://blog.csdn.net/xiaokexinger/archive/2007/03/26/1541441.aspx]
- C++中单向链表的实现(类似C#)
- C# 实现一个单向链表
- 按回车实现点击某按钮的效果 http://blog.csdn.net/jjzaihaozhe/archive/2007/08/18/1749635.aspx
- 使用观察者(Observer)实现对象监听 (转自http://blog.csdn.net/kalex)
- c#读取并修改App.config文件实例(转载:http://blog.csdn.net/abuhome/archive/2010/01/13/5184467.aspx)
- 使用Support Library实现ActionBar(http://blog.csdn.net/xyz_lmn/article/details/8132420)
- MFC OCX控件实现安全初始化和脚本安全的方法http://blog.csdn.net/xiliang_pan/article/details/8264685
- FTP搜索引擎的设计与实现http://blog.csdn.net/heiyeshuwu/archive/2007/03/25/1540532.aspx
- 关于交叉报表的动态实现 [http://blog.csdn.net/lupenda/archive/2004/10/18/141744.aspx]
- C#环境下的钩子详解(转自http://blog.csdn.net/stevenson_aspdotnet/archive/2010/03/04/5346725.aspx)
- 用webbrowser实现html界面的应用(http://writeblog.csdn.net/PostEdit.aspx)
- .NET委托:一个C#睡前故事(http://blog.csdn.net/uoyevoli)