C#实现单向链表

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Emep.Dotnet.Frame
  6. {
  7.     class TestMyLinkedList
  8.     {
  9.         public static void Main()
  10.         {
  11.             SinglyLinkedList<Person> mylist = new SinglyLinkedList<Person>();
  12.             Person per1 = new Person( "aladdin" , 100 ) ;
  13.             Person per2 = new Person("zhao", 100);
  14.             Person per3 = new Person("jacky", 100);
  15.             Person per4 = new Person("emep", 100);
  16.             Person per5 = new Person("fuck", 100);
  17.             Person per6 = new Person("gcd", 100);
  18.             Person per7 = new Person("shit", 100);
  19.             Person per8 = new Person("mygod", 100);
  20.             mylist.Add( per1 ) ;
  21.             mylist.Add( per2 ) ;
  22.             mylist.Add( per3 ) ;
  23.             mylist.Add( per4 ) ;
  24.             mylist.Add( per5 ) ;
  25.             mylist.Add( per6 ) ;
  26.             mylist.Add( per7 ) ;
  27.             mylist.Add( per8 ) ;
  28.             Person p = new Person( "insertjacky" , 100 ) ;
  29.             mylist.Insert( p , 6) ;
  30.             foreach( Person per in mylist)
  31.             {
  32.                 Console.WriteLine( per.Name ) ;
  33.             }
  34.     
  35.             Console.Write( "链表大小:{0}" ,  mylist.Count ) ;
  36.             Console.ReadLine() ;
  37.         }
  38.     }
  39.     class Person
  40.     {
  41.         public string Name ;
  42.         public int age ;
  43.         public Person( string name , int age ) 
  44.         {
  45.             this.Name = name ;
  46.             this.age = age ;
  47.         }
  48.     }
  49. }

 

 

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Emep.Dotnet.Frame
  6. {
  7.     /// <summary>
  8.     /// 实现对元素的存删插入等操作
  9.     /// </summary>
  10.     class SinglyLinkedList<T>:ICollection<T>
  11.     {
  12.         private SinglyLinkedListNode<T> firstNode;
  13.         //头节点
  14.         public SinglyLinkedListNode<T> FirstNode
  15.         {
  16.             get { return firstNode; }
  17.             set { firstNode = value; }
  18.         }
  19.         /// <summary>
  20.         /// 将元素插入到某个位置
  21.         /// </summary>
  22.         /// <param name="item">要插入的元素</param>
  23.         /// <param name="index">要插入元素的位置索引</param>
  24.         public void Insert( T item , int index )
  25.         {
  26.             if( index < 0 || index >= this.Count)
  27.             {
  28.                 throw  new Exception( "索引值非法,无法进行插入" ) ;
  29.             }
  30.             if( item == null )
  31.             {
  32.                 throw new Exception( "要插入的元素不可以为空,无法进行插入" ) ;
  33.             }
  34.             SinglyLinkedListNode<T> insertItem = new SinglyLinkedListNode<T>( item ) ;
  35.             if( index == 0 )
  36.             {
  37.                 insertItem.Next = this.firstNode ;
  38.                 this.firstNode = insertItem ;
  39.             }
  40.             else
  41.             {
  42.                 SinglyLinkedListNode<T> locaItem = this.GetElementByIndex( index ) ;
  43.                 SinglyLinkedListNode<T> upItem = this.GetElementByIndex( index - 1 ) ;
  44.                 upItem.Next = insertItem ;
  45.                 insertItem.Next = locaItem ;
  46.             }
  47.         }
  48.         //查询对象的索引值,查询对象第一次出先的位置
  49.         public int IndexOf( T  item)
  50.         {
  51.             int reIndex = -1 ;
  52.             if (item == null)
  53.             {
  54.                 throw new Exception("对空参数为空,无法得到其索引值!");
  55.             }
  56.             
  57.             int i = 0 ;
  58.             while( i < this.Count )
  59.             {
  60.                 SinglyLinkedListNode<T> tempObj = GetElementByIndex( i ) ;
  61.                 if( tempObj.ItemValue.Equals( item ) )
  62.                 {
  63.                     reIndex = i ;
  64.                     break ;
  65.                 }
  66.                 ++ i ;
  67.             }
  68.             return reIndex;
  69.         }
  70.         //通过索引得到节点
  71.         public SinglyLinkedListNode<T> GetElementByIndex( int index )
  72.         {
  73.             SinglyLinkedListNode<T> reNode = null ;
  74.             if( index < 0 || this.Count == 0 || this.firstNode == null )
  75.             {
  76.                 throw new Exception( "对不起,链表中暂时没有元素,元法通过索引得到元素!" ) ;
  77.             }
  78.             int i = 0 ;
  79.             SinglyLinkedListNode<T> tempNode = this.firstNode ;
  80.             while( tempNode != null )
  81.             {
  82.                 if( i == index )
  83.                 {
  84.                     reNode = tempNode ;
  85.                     break ;
  86.                 }
  87.                 
  88.                 ++ i ;
  89.                 //更新节点
  90.                 tempNode = tempNode.Next ;
  91.             }
  92.             return reNode ;
  93.         }
  94.         //对集合ICollection<T>接口的实现
  95.         #region ICollection<T> 成员
  96.         //添加元素
  97.         public void Add(T item)
  98.         {
  99.             if (item == null)
  100.             {
  101.                 throw new Exception("元素不可以为空,无法添加!");
  102.             }
  103.             SinglyLinkedListNode<T> currItemNode = new SinglyLinkedListNode<T>( item );
  104.             if (this.firstNode == null)
  105.             {
  106.                 //如果没有添加过元素,则应把这个元素放到第一个
  107.                 this.firstNode = currItemNode;
  108.             }
  109.             else
  110.             { 
  111.                 //如果添加过,应查出最后一个元素的位置,然后添加到其后。
  112.                 SinglyLinkedListNode<T> lastNode = this.GetElementByIndex( this.Count - 1) ;
  113.                 lastNode.Next = currItemNode ;
  114.             }
  115.         }
  116.         public void Clear()
  117.         {
  118.             //清空头对象引用,所有的链表对象,注定将遭受被回收的命运,有点火烧赤壁的感觉
  119.             this.firstNode = null ;
  120.         }
  121.         //检查某对象是不是存在于本链表中
  122.         public bool Contains( T item )
  123.         {
  124.             return ( this.IndexOf( item )  != -1 ) ;
  125.         }
  126.         //复制
  127.         public void CopyTo( T[] array , int arrayIndex )
  128.         {
  129.             throw new NotImplementedException();
  130.         }
  131.         //计算链表元素合
  132.         public int Count
  133.         {
  134.             get
  135.             { 
  136.                 int reCount = 0 ;
  137.                 ifthis.firstNode != null )
  138.                 {
  139.                     SinglyLinkedListNode<T> tempNode = this.firstNode ;
  140.                     int i = 0 ;
  141.                     while( tempNode != null )
  142.                     {
  143.                         tempNode = tempNode.Next ;
  144.                         ++ i ;    
  145.                     }
  146.                     reCount = i;
  147.                 }
  148.                 return reCount ;
  149.             }
  150.         }
  151.         //我们的链表中没有加入是不是只读的判断,所以在这里,忽略它
  152.         public bool IsReadOnly
  153.         {
  154.             get {  return false ; }
  155.         }
  156.         //删除链表中的元素
  157.         public bool Remove( T item )
  158.         {
  159.             bool flag = false ;
  160.             if( item == null )
  161.             {
  162.                 throw new Exception( "元素为空,无法进行删除!" ) ;
  163.             }
  164.             int index = this.IndexOf( item ) ;
  165.             //判断是否为头元素
  166.             if( index == 0 )
  167.             {
  168.                 this.firstNode = this.firstNode.Next ;
  169.                 flag = true ;
  170.             }
  171.             else
  172.             {
  173.                 //直接更新当前元素的上一个元素的Next引用指向当前元素的Next,这叫个别扭
  174.                 SinglyLinkedListNode<T> currNode = this.GetElementByIndex( index ) ;
  175.                 SinglyLinkedListNode<T> upNode = this.GetElementByIndex( index - 1) ;
  176.                 upNode.Next = currNode.Next ;
  177.                 flag = true ;
  178.             }
  179.             return flag ;
  180.         }
  181.         #endregion
  182.         //IEnumerable<T> 佚代接口的实现
  183.         #region IEnumerable<T> 成员
  184.         public IEnumerator<T> GetEnumerator()
  185.         {
  186.             return new SingLyLinkedListEnumerator<T>(this);
  187.         }
  188.         #endregion
  189.         #region IEnumerable 成员
  190.         //显示实现IEnumerator接口
  191.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  192.         {
  193.             return GetEnumerator();
  194.         }
  195.         #endregion
  196.     }
  197. }

 

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Emep.Dotnet.Frame
  6. {
  7.     /// <summary>
  8.     /// 单向链表的节点类
  9.     /// </summary>
  10.     class SinglyLinkedListNode<T>
  11.     {
  12.         private T itemValue;
  13.         //当前节点的内容元素
  14.         public T ItemValue
  15.         {
  16.             get { return itemValue; }
  17.             set { itemValue = value; }
  18.         }
  19.        
  20.         private SinglyLinkedListNode<T> next;
  21.         //当前节点对下一个节点的引用
  22.         internal SinglyLinkedListNode<T> Next
  23.         {
  24.             get { return next; }
  25.             set { next = value; }
  26.         }
  27.         /// <summary>
  28.         /// 构造方法
  29.         /// </summary>
  30.         public SinglyLinkedListNode( T item )
  31.         {
  32.             if (item == null)
  33.             {
  34.                 throw new Exception("链表节点参数不可以为空");
  35.             }
  36.             this.itemValue = item ;
  37.             this.next = null;
  38.         }
  39.         
  40.     }
  41. }

 

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Emep.Dotnet.Frame
  6. {
  7.     /// <summary>
  8.     /// 为链表实现的佚代器
  9.     /// </summary>
  10.     /// <typeparam name="T"></typeparam>
  11.     class SingLyLinkedListEnumerator<T>:IEnumerator<T>
  12.     {
  13.         //当前节点
  14.         private SinglyLinkedListNode<T> currNode ;
  15.         //要佚代的链表
  16.         private readonly SinglyLinkedList<T>  list ;
  17.         //构造方法初始要佚代的对象
  18.         public SingLyLinkedListEnumerator( SinglyLinkedList<T> list )
  19.         {
  20.             this.list = list ;
  21.         }
  22.         #region IEnumerator<T> 成员
  23.         public T Current
  24.         {
  25.             get
  26.             {
  27.                return this.currNode.ItemValue ;
  28.             }
  29.         }
  30.         #endregion
  31.         #region IDisposable 成员
  32.         //回收本类中例用的引用类型
  33.         public void Dispose()
  34.         {
  35.             this.currNode = null ;
  36.         }
  37.         #endregion
  38.         #region IEnumerator 成员
  39.         object System.Collections.IEnumerator.Current
  40.         {
  41.             get
  42.             { 
  43.                 return this.Current ;
  44.             }
  45.         }
  46.         public bool MoveNext()
  47.         {
  48.             bool flag = false ;
  49.             if ( this.currNode == null )
  50.             {
  51.                 this.currNode = this.list.FirstNode ;
  52.                 flag = true ;
  53.             }
  54.             else
  55.             {
  56.                 ifthis.currNode.Next != null )
  57.                 {
  58.                     this.currNode = this.currNode.Next ;
  59.                     flag = true ;
  60.                 }
  61.             }
  62.             return flag ;
  63.         }
  64.         public void Reset()
  65.         {
  66.             this.currNode = null;
  67.         }
  68.         #endregion
  69.     }
  70. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值