C#实现双向链表

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace 双向链表
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             DoubleLinkedList<Person> list = new DoubleLinkedList<Person>() ;
  12.             Person per1 = new Person( "aladdin" , 1 ) ;
  13.             Person per2 = new Person( "jacky" , 1 ) ;
  14.             Person per3 = new Person( "fuck" , 1 ) ;
  15.             Person per4 = new Person( "zhao" , 1 ) ;
  16.             Person per5 = new Person( "aladdin2" , 1 ) ;
  17.             Person per6 = new Person( "jacky2" , 1 ) ;
  18.             Person per7 = new Person( "fuck2" , 1 ) ;
  19.             Person per8 = new Person( "zhao2" , 1 ) ;
  20.             list.Add( per1 ) ;
  21.             list.Add( per2 ) ;
  22.             list.Add( per3 ) ;
  23.             list.Add( per4 ) ;
  24.             list.Add( per5 ) ;
  25.             list.Add( per6 ) ;
  26.             list.Add( per7 ) ;
  27.             list.Add( per8 ) ;
  28.             Console.WriteLine( "链表大小:{0}" , list.Count ) ;
  29.             Console.WriteLine( "检查Pers8是否存在与链表中:{0}" , list.Contains( per8 ) ) ;
  30.             Person per9 = new Person( "per9" , 100 ) ;
  31.             Console.WriteLine( "检查Pers9是否存在与链表中:{0}" , list.Contains( per9 ) ) ;
  32.             Console.WriteLine( "Per8的索引值{0}", list.IndexOf( per8 ) ) ;
  33.             Console.WriteLine( "Per9的索引值{0}", list.IndexOf( per9 ) ) ;
  34.             Console.WriteLine( "索引7点的元素值:{0}" , list.GetElementByIndex( 7 ).name) ;
  35.             Person[] pers = new Person[ 20 ] ;
  36.             list.CopyTo( pers , 3 ) ;
  37.             foreach( Person p in pers ) 
  38.             {
  39.                 if( p == null )
  40.                 {
  41.                     Console.WriteLine( "空..." ) ;
  42.                 }
  43.                 else
  44.                 {
  45.                     Console.WriteLine( p.name ) ;
  46.                 }
  47.                 
  48.             }
  49.                         foreach( Person per in list )
  50.             {
  51.                 Console.WriteLine( "名字:{0} 年纪:{1}" , per.name , per.age ) ;
  52.             }
  53.             list.Remove( list.First ) ;
  54.             list.Remove( list.Last ) ;
  55.             list.AddBefore( list.First , new Person( "1111" ,111 ) ) ;
  56.             list.AddBefore( list.Last , new Person( "222" ,222 ) ) ;
  57.             list.AddAfter( list.First , new Person( "333" ,333 ) ) ;
  58.             list.AddAfter( list.Last , new Person( "444" ,444 ) ) ;
  59.             Console.WriteLine( "--------------------------------" ) ;
  60.             
  61.             DoubleLinkedListNode<Person> node5 = list.GetNodeByIndex( 5 ) ;
  62.             list.AddAfter( node5 , new Person( "aaa" , 1 ) );
  63.             list.AddAfter( node5 , new Person( "aaa" , 2 ) );
  64.             list.AddFirst( new Person( "第一" , 1 ) ) ;
  65.             list.AddLast( new Person( "最后" , 1 ) ) ;
  66.             list.AddFirst( new Person( "第一" , 2 ) ) ;
  67.             list.AddLast( new Person( "最后" , 2 ) ) ;
  68.             
  69.             foreach( Person per in list )
  70.             {
  71.                 Console.WriteLine( "名字:{0} 年纪:{1}" , per.name , per.age ) ;
  72.             }
  73.             Console.ReadLine() ;
  74.         }
  75.     }
  76.     class Person
  77.     {
  78.         public string name ;
  79.         public int age ;
  80.         public Person( string name , int age )
  81.         {
  82.             this.name = name ;
  83.             this.age = age ;
  84.         }
  85.     }
  86. }

 

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace 双向链表
  6. {
  7.     class DoubleLinkedListNode<T>
  8.     {
  9.         /// <summary>
  10.         /// 下一个元素的指针
  11.         /// </summary>
  12.         private DoubleLinkedListNode<T> next;
  13.         internal DoubleLinkedListNode<T> Next
  14.         {
  15.             get { return next; }
  16.             set { next = value; }
  17.         }
  18.       
  19.         /// <summary>
  20.         /// 上一个元素的指针
  21.         /// </summary>
  22.         private DoubleLinkedListNode<T> prev;
  23.         internal DoubleLinkedListNode<T> Prev
  24.         {
  25.             get { return prev; }
  26.             set { prev = value; }
  27.         }
  28.         /// <summary>
  29.         /// 当前元素所包含的值
  30.         /// </summary>
  31.         private T itemValue;
  32.         public T ItemValue
  33.         {
  34.             get { return itemValue; }
  35.             set { itemValue = value; }
  36.         }
  37.         /// <summary>
  38.         /// 构造函数
  39.         /// </summary>
  40.         /// <param name="itemVale"></param>
  41.         public DoubleLinkedListNode( T itemValue )
  42.         {
  43.             this.itemValue = itemValue ;
  44.         }
  45.     }
  46. }

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace 双向链表
  6. {
  7.     interface ILinkedList<T>
  8.     {
  9.         /// <summary>
  10.         /// 头元素
  11.         /// </summary>
  12.         DoubleLinkedListNode<T> First { get; }
  13.         /// <summary>
  14.         /// 尾元素
  15.         /// </summary>
  16.         DoubleLinkedListNode<T> Last { get; } 
  17.         /// <summary>
  18.         /// 在 LinkedList<(Of <(T>)>) 中指定的现有节点后添加指定的新节点。
  19.         /// </summary>
  20.         /// <param name="node"></param>
  21.         /// <param name="newNode"></param>
  22.         void AddAfter( DoubleLinkedListNode<T> node , DoubleLinkedListNode<T> newNode ) ;
  23.         /// <summary>
  24.         /// 在 LinkedList<(Of <(T>)>) 中的现有节点后添加新的节点或值。
  25.         /// </summary>
  26.         /// <param name="node"></param>
  27.         /// <param name="t"></param>
  28.         void AddAfter( DoubleLinkedListNode<T> node , T t )  ;
  29.         /// <summary>
  30.         /// 在 LinkedList<(Of <(T>)>) 中指定的现有节点前添加指定的新节点。
  31.         /// </summary>
  32.         /// <param name="node"></param>
  33.         /// <param name="newNode"></param>
  34.         void AddBefore( DoubleLinkedListNode<T> node , DoubleLinkedListNode<T> newNode ) ;
  35.         /// <summary>
  36.         /// 在 LinkedList<(Of <(T>)>) 中指定的现有节点前添加包含指定值的新节点。
  37.         /// </summary>
  38.         /// <param name="node"></param>
  39.         /// <param name="t"></param>
  40.         void AddBefore(DoubleLinkedListNode<T> node, T t);
  41.         /// <summary>
  42.         /// 在 LinkedList<(Of <(T>)>) 的开头处添加包含指定值的新节点。
  43.         /// </summary>
  44.         /// <param name="value"></param>
  45.         /// <returns></returns>
  46.         DoubleLinkedListNode<T> AddFirst( T value ) ;
  47.         /// <summary>
  48.         /// 在 LinkedList<(Of <(T>)>) 的开头处添加指定的新节点
  49.         /// </summary>
  50.         /// <param name="node"></param>
  51.         void AddFirst( DoubleLinkedListNode<T> node ) ;
  52.         /// <summary>
  53.         /// 在 LinkedList<(Of <(T>)>) 的结尾处添加包含指定值的新节点。
  54.         /// </summary>
  55.         /// <param name="value"></param>
  56.         /// <returns></returns>
  57.         DoubleLinkedListNode<T> AddLast( T value ) ;
  58.         /// <summary>
  59.         /// 在 LinkedList<(Of <(T>)>) 的结尾处添加指定的新节点
  60.         /// </summary>
  61.         /// <param name="node"></param>
  62.         void AddLast( DoubleLinkedListNode<T> node ) ;
  63.         /// <summary>
  64.         /// 从 LinkedList<(Of <(T>)>) 中移除指定的节点。
  65.         /// </summary>
  66.         /// <param name="node"></param>
  67.         bool Remove ( DoubleLinkedListNode<T> node );
  68.          /// <summary>
  69.         /// 从 LinkedList<(Of <(T>)>) 中按索引删除节点。
  70.         /// </summary>
  71.         /// <param name="node"></param>
  72.         bool Remove ( int index ) ;
  73.         /// <summary>
  74.         /// 从 LinkedList<(Of <(T>)>) 中查找第一个匹配项
  75.         /// </summary>
  76.         /// <param name="value"></param>
  77.         /// <returns></returns>
  78.         DoubleLinkedListNode<T> Find(T value); 
  79.         /// <summary>
  80.         /// 从 LinkedList<(Of <(T>)>) 中查找最后一个匹配项
  81.         /// </summary>
  82.         /// <param name="value"></param>
  83.         /// <returns></returns>
  84.         DoubleLinkedListNode<T> FindLast( T value ) ;
  85.         /// <summary>
  86.         /// 查询元素的索引
  87.         /// </summary>
  88.         /// <param name="item"></param>
  89.         /// <returns></returns>
  90.         int IndexOf( T item ) ;
  91.         /// <summary>
  92.         /// 能过索引得到元素的元素
  93.         /// </summary>
  94.         /// <param name="index"></param>
  95.         /// <returns></returns>
  96.         T GetElementByIndex( int index ) ;
  97.         /// <summary>
  98.         /// 通过索引得到节点对象
  99.         /// </summary>
  100.         /// <param name="index"></param>
  101.         /// <returns></returns>
  102.         DoubleLinkedListNode<T> GetNodeByIndex( int index ) ;
  103.     }
  104. }

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace 双向链表
  6. {
  7.     class DoubleLinkedList<T> : ICollection<T> , ILinkedList<T>
  8.     {
  9.         private DoubleLinkedListNode<T> first ;
  10.         private DoubleLinkedListNode<T> last ;
  11.         #region ICollection<T> 成员
  12.         public void Add( T item )
  13.         {
  14.             if( item == null )
  15.             {
  16.                 throw new Exception( "元素为空,元法进行添加!" ) ;
  17.             }
  18.             //要添加的节点
  19.             DoubleLinkedListNode<T> addItem = new DoubleLinkedListNode<T>( item ) ;
  20.           
  21.             //把新节点添加到链表的表尾
  22.             ifthis.first == null )
  23.             {
  24.                 //如果没有过行过添加
  25.                 this.first = addItem ;
  26.                 //第一次添加时,头尾指针都应是一个对象
  27.                 this.last = this.first ;
  28.             }
  29.             else
  30.             {
  31.                 //如果链表中不为空
  32.                 this.last.Next = addItem ;
  33.                 addItem.Prev = this.last ;
  34.                 this.last = addItem ;
  35.             }
  36.         }
  37.         public void Clear()
  38.         {
  39.             this.first = null ;
  40.         }
  41.         public bool Contains( T item )
  42.         {
  43.             if( item == null )
  44.             {
  45.                 throw new Exception( "元素为空,无法检测对象是否存在与链表中." ) ;
  46.             }
  47.             bool flag = false ;
  48.             ifthis.Count != 0 )
  49.             {
  50.                 DoubleLinkedListNode<T> node = this.first ;
  51.                 while( node != null )
  52.                 {
  53.                     if( node.ItemValue.Equals( item ) )
  54.                     {
  55.                         flag = true ;
  56.                         break ;
  57.                     }
  58.                     node = node.Next ;
  59.                 }
  60.             }
  61.             return flag ;
  62.         }
  63.         public void CopyTo( T[] array , int arrayIndex )
  64.         {
  65.             if( array == null  )
  66.             {
  67.                 throw new Exception( "目标数组不可以为空,无法复制" ) ;
  68.             }
  69.             if( arrayIndex < 0 || arrayIndex >= array.Length )
  70.             {
  71.                 throw new Exception( "索引值非法,无法进行复制." ) ;
  72.             }
  73.             if( (array.Length-arrayIndex) < this.Count || array.Length < this.Count )
  74.             {
  75.                 throw new Exception( "目标数组容量不够,无法进行复制." ) ;
  76.             }
  77.             forint i = 0 ; i < this.Count ; i ++ )
  78.             {
  79.                 T item = this.GetElementByIndex( i ) ;
  80.                 array[ arrayIndex ] = item ;
  81.                 ++ arrayIndex ;
  82.             }
  83.         }
  84.         public int Count
  85.         {
  86.             get
  87.             {
  88.                 int i  = 0 ;
  89.                 DoubleLinkedListNode<T> node = this.first ;
  90.                 while( node != null )
  91.                 {
  92.                     ++ i ;
  93.                     node = node.Next ;
  94.                 }
  95.                 
  96.                 return i ;
  97.             }
  98.         }
  99.         public bool IsReadOnly
  100.         {
  101.             get { return false; }
  102.         }
  103.         public bool Remove( T item )
  104.         { 
  105.             bool flag = false ;
  106.             if( item == null )
  107.             {
  108.                 throw new Exception( "元素为可以空,无法删除" )  ;
  109.             }
  110.             int index = this.IndexOf( item ) ;
  111.             if( index != -1 )
  112.             {
  113.                 flag = this.Remove( index ) ;
  114.             }
  115.             else
  116.             {
  117.                 throw new Exception( "元素在链表中不存在,无法删除。" ) ;
  118.             }
  119.             return flag ;
  120.         }
  121.         #endregion
  122.         #region IEnumerable<T> 成员
  123.         public IEnumerator<T> GetEnumerator()
  124.         {
  125.             DoubleLinkedListNode<T> currNode = this.first ;
  126.             while( currNode != null )
  127.             {
  128.                 yield return currNode.ItemValue ;
  129.                 currNode = currNode.Next ;
  130.             }
  131.         }
  132.         #endregion
  133.         #region IEnumerable 成员
  134.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  135.         {
  136.             return GetEnumerator() ;
  137.         }
  138.         #endregion
  139.         #region ILinkedList<T> 成员
  140.         public DoubleLinkedListNode<T> First
  141.         {
  142.             get {  return this.first ;}
  143.         }
  144.         public DoubleLinkedListNode<T> Last
  145.         {
  146.             get { return this.last ; }
  147.         }
  148.         public void AddAfter( DoubleLinkedListNode<T> node , DoubleLinkedListNode<T> newNode )
  149.         {
  150.             this.AddAfter( node , newNode.ItemValue ) ;
  151.         }
  152.         public void AddAfter( DoubleLinkedListNode<T> node , T t )
  153.         {
  154.             if( node == null || t == null )
  155.             {
  156.                 throw new Exception( "元素为空,不可以进行插入") ;
  157.             }
  158.             int index = this.IndexOf( node.ItemValue ) ;
  159.             
  160.             if( index != -1 )
  161.             {
  162.                 DoubleLinkedListNode<T> currNode = this.GetNodeByIndex( index ) ;
  163.                 DoubleLinkedListNode<T> upNode = currNode.Prev ;
  164.                 DoubleLinkedListNode<T> nextNode = currNode.Next ;
  165.                 DoubleLinkedListNode<T> newNode = new DoubleLinkedListNode<T>( t ) ;
  166.                 if( index == 0 )
  167.                 {
  168.                     this.first.Next = newNode ;
  169.                     newNode.Prev = this.first ;
  170.                     newNode.Next = nextNode ;
  171.                 }
  172.                 else if( index == this.Count - 1 )
  173.                 {
  174.                     newNode.Prev = this.last ;
  175.                     this.last.Next = newNode ;
  176.                     this.last = newNode ;
  177.                 }
  178.                 else
  179.                 {
  180.                     nextNode.Prev = newNode ;
  181.                     currNode.Next  = newNode ;
  182.                     newNode.Prev = currNode ;
  183.                     newNode.Next = nextNode ;
  184.                 }
  185.                 
  186.             }
  187.         }
  188.         public void AddBefore(DoubleLinkedListNode<T> node, DoubleLinkedListNode<T> newNode)
  189.         {
  190.              this.AddBefore( node , newNode.ItemValue ) ;
  191.         }
  192.         public void AddBefore(DoubleLinkedListNode<T> node, T t)
  193.         {
  194.             if( node == null || t == null )
  195.             {
  196.                 throw new Exception( "元素为空,不可以进行插入") ;
  197.             }
  198.             int index = this.IndexOf( node.ItemValue ) ;
  199.             
  200.             if( index != -1 )
  201.             {
  202.                 DoubleLinkedListNode<T> currNode = this.GetNodeByIndex( index ) ;
  203.                 DoubleLinkedListNode<T> upNode = currNode.Prev ;
  204.                 DoubleLinkedListNode<T> nextNode = currNode.Next ;
  205.                 DoubleLinkedListNode<T> newNode = new DoubleLinkedListNode<T>( t ) ;
  206.                 if( index == 0 )
  207.                 {
  208.                     this.first.Prev = newNode ;
  209.                     newNode.Next = this.first ;
  210.                     this.first = newNode ;
  211.                 }
  212.                 else if( index == this.Count - 1 )
  213.                 {
  214.                     upNode.Next = newNode ;
  215.                     newNode.Next = this.last ;
  216.                     this.last.Prev = newNode ;
  217.                 }
  218.                 else
  219.                 {
  220.                     upNode.Next = newNode ;
  221.                     nextNode.Prev = newNode ;
  222.                     currNode.Prev = upNode ;
  223.                     currNode.Next = nextNode ;
  224.                 }
  225.                 
  226.             }
  227.         }
  228.         public DoubleLinkedListNode<T> AddFirst( T value )
  229.         {
  230.             if( value == null )
  231.             {
  232.                 throw new Exception( "对不起,元素为空,不可以添加到链表开头" ) ;
  233.             }
  234.             DoubleLinkedListNode<T> node = new DoubleLinkedListNode<T>( value ) ;
  235.             this.first.Prev = node ;
  236.             node.Next = this.first ;
  237.             this.first = node ;
  238.             return this.first ;
  239.         }
  240.         public void AddFirst( DoubleLinkedListNode<T> node )
  241.         {
  242.             this.AddFirst( node.ItemValue ) ;
  243.         }
  244.         public DoubleLinkedListNode<T> AddLast( T value )
  245.         {
  246.             if( value == null )
  247.             {
  248.                 throw new Exception( "对不起,元素为空,不可以添加到链表开头" ) ;
  249.             }
  250.             DoubleLinkedListNode<T> node = new DoubleLinkedListNode<T>( value ) ;
  251.             this.last.Next = node ;
  252.             node.Prev = this.last ;
  253.             this.last = node ;
  254.             return this.last ;
  255.         }
  256.         public void AddLast( DoubleLinkedListNode<T> node )
  257.         {
  258.             this.AddLast( node.ItemValue ) ;
  259.         }
  260.         public bool Remove( DoubleLinkedListNode<T> node )
  261.         {
  262.             if( node == null )
  263.             {
  264.                 throw new Exception( "节点不可以为空,无法进行删除" ) ;
  265.             }
  266.             int index  = this.IndexOf( node.ItemValue ) ;
  267.             return this.Remove( index ) ;
  268.         }
  269.         public bool Remove( int index )
  270.         {
  271.             if( index < 0 || index >= this.Count )
  272.             {
  273.                 throw new Exception( "索引值非法,无法进行删除。" ) ;
  274.             }
  275.             bool flag = false ;
  276.             DoubleLinkedListNode<T> node = this.first ;
  277.             int i = 0 ;
  278.             while( node != null )
  279.             {
  280.                 if( i == index )
  281.                 {
  282.                     DoubleLinkedListNode<T> upNode = node.Prev ;
  283.                     DoubleLinkedListNode<T> nextNode = node.Next ;
  284.                     
  285.                     if( index == 0 )
  286.                     {
  287.                         this.first = node.Next ;
  288.                         node = null ;
  289.                     }
  290.                     else if ( index == this.Count - 1 )
  291.                     {
  292.                         this.last = upNode ;
  293.                         this.last.Next = null ;
  294.                         node = null ;
  295.                     }
  296.                     else
  297.                     {
  298.                         upNode.Next = nextNode ;
  299.                         nextNode.Prev = upNode ;
  300.                         node = null ;
  301.                     }
  302.                     flag = true ;
  303.                     break ;
  304.                 }
  305.                 node = node.Next ;
  306.                 ++ i ;
  307.             }
  308.             return flag ;
  309.         }
  310.         public int IndexOf( T item )
  311.         {
  312.             if( item == null || this.Count == 0 || this.first == null )
  313.             {
  314.                 throw new Exception( "无法得到元素索引" ) ;
  315.             }
  316.             int reInt = -1 ;
  317.             int i = 0 ;
  318.             DoubleLinkedListNode<T> node = this.first ;
  319.             while( node != null )
  320.             {
  321.                 if( node.ItemValue.Equals( item ) )
  322.                 {
  323.                     reInt = i ;
  324.                     break ;
  325.                 }
  326.                 
  327.                 ++i ;
  328.                 node = node.Next ;
  329.             }
  330.             
  331.             return reInt ;
  332.         }
  333.         public T GetElementByIndex( int index )
  334.         {
  335.             if( index < 0 || index>= this.Count )
  336.             {
  337.                 throw new Exception( "索引值非法,无法得对象." ) ;
  338.             }
  339.             DoubleLinkedListNode<T> reNode = this.first ;
  340.             int i = 0 ;
  341.             while( reNode != null )
  342.             {
  343.                 if( i == index )
  344.                 {
  345.                     break ;
  346.                 }
  347.                 reNode = reNode.Next ;
  348.                 ++ i ;
  349.             }
  350.             return reNode.ItemValue ;
  351.         }
  352.         public DoubleLinkedListNode<T> GetNodeByIndex(int index)
  353.         {
  354.             if( index < 0 || index>= this.Count )
  355.             {
  356.                 throw new Exception( "索引值非法,无法得对象节点." ) ;
  357.             }
  358.             DoubleLinkedListNode<T> reNode = this.first ;
  359.             int i = 0 ;
  360.             while( reNode != null )
  361.             {
  362.                 if( i == index )
  363.                 {
  364.                     break ;
  365.                 }
  366.                 reNode = reNode.Next ;
  367.                 ++ i ;
  368.             }
  369.             return reNode ;
  370.         }
  371.         public DoubleLinkedListNode<T> Find(T value)
  372.         {
  373.             throw new NotImplementedException();
  374.         }
  375.         public DoubleLinkedListNode<T> FindLast(T value)
  376.         {
  377.             throw new NotImplementedException();
  378.         }
  379.         #endregion
  380.     }
  381. }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值