- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Emep.Dotnet.Frame
- {
- class TestMyLinkedList
- {
- public static void Main()
- {
- SinglyLinkedList<Person> mylist = new SinglyLinkedList<Person>();
- Person per1 = new Person( "aladdin" , 100 ) ;
- Person per2 = new Person("zhao", 100);
- Person per3 = new Person("jacky", 100);
- Person per4 = new Person("emep", 100);
- Person per5 = new Person("fuck", 100);
- Person per6 = new Person("gcd", 100);
- Person per7 = new Person("shit", 100);
- Person per8 = new Person("mygod", 100);
- mylist.Add( per1 ) ;
- mylist.Add( per2 ) ;
- mylist.Add( per3 ) ;
- mylist.Add( per4 ) ;
- mylist.Add( per5 ) ;
- mylist.Add( per6 ) ;
- mylist.Add( per7 ) ;
- mylist.Add( per8 ) ;
- Person p = new Person( "insertjacky" , 100 ) ;
- mylist.Insert( p , 6) ;
- foreach( Person per in mylist)
- {
- Console.WriteLine( per.Name ) ;
- }
- Console.Write( "链表大小:{0}" , mylist.Count ) ;
- Console.ReadLine() ;
- }
- }
- class Person
- {
- public string Name ;
- public int age ;
- public Person( string name , int age )
- {
- this.Name = name ;
- this.age = age ;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Emep.Dotnet.Frame
- {
- /// <summary>
- /// 实现对元素的存删插入等操作
- /// </summary>
- class SinglyLinkedList<T>:ICollection<T>
- {
- private SinglyLinkedListNode<T> firstNode;
- //头节点
- public SinglyLinkedListNode<T> FirstNode
- {
- get { return firstNode; }
- set { firstNode = value; }
- }
- /// <summary>
- /// 将元素插入到某个位置
- /// </summary>
- /// <param name="item">要插入的元素</param>
- /// <param name="index">要插入元素的位置索引</param>
- public void Insert( T item , int index )
- {
- if( index < 0 || index >= this.Count)
- {
- throw new Exception( "索引值非法,无法进行插入" ) ;
- }
- if( item == null )
- {
- throw new Exception( "要插入的元素不可以为空,无法进行插入" ) ;
- }
- SinglyLinkedListNode<T> insertItem = new SinglyLinkedListNode<T>( item ) ;
- if( index == 0 )
- {
- insertItem.Next = this.firstNode ;
- this.firstNode = insertItem ;
- }
- else
- {
- SinglyLinkedListNode<T> locaItem = this.GetElementByIndex( index ) ;
- SinglyLinkedListNode<T> upItem = this.GetElementByIndex( index - 1 ) ;
- upItem.Next = insertItem ;
- insertItem.Next = locaItem ;
- }
- }
- //查询对象的索引值,查询对象第一次出先的位置
- public int IndexOf( T item)
- {
- int reIndex = -1 ;
- if (item == null)
- {
- throw new Exception("对空参数为空,无法得到其索引值!");
- }
- int i = 0 ;
- while( i < this.Count )
- {
- SinglyLinkedListNode<T> tempObj = GetElementByIndex( i ) ;
- if( tempObj.ItemValue.Equals( item ) )
- {
- reIndex = i ;
- break ;
- }
- ++ i ;
- }
- return reIndex;
- }
- //通过索引得到节点
- public SinglyLinkedListNode<T> GetElementByIndex( int index )
- {
- SinglyLinkedListNode<T> reNode = null ;
- if( index < 0 || this.Count == 0 || this.firstNode == null )
- {
- throw new Exception( "对不起,链表中暂时没有元素,元法通过索引得到元素!" ) ;
- }
- int i = 0 ;
- SinglyLinkedListNode<T> tempNode = this.firstNode ;
- while( tempNode != null )
- {
- if( i == index )
- {
- reNode = tempNode ;
- break ;
- }
- ++ i ;
- //更新节点
- tempNode = tempNode.Next ;
- }
- return reNode ;
- }
- //对集合ICollection<T>接口的实现
- #region ICollection<T> 成员
- //添加元素
- public void Add(T item)
- {
- if (item == null)
- {
- throw new Exception("元素不可以为空,无法添加!");
- }
- SinglyLinkedListNode<T> currItemNode = new SinglyLinkedListNode<T>( item );
- if (this.firstNode == null)
- {
- //如果没有添加过元素,则应把这个元素放到第一个
- this.firstNode = currItemNode;
- }
- else
- {
- //如果添加过,应查出最后一个元素的位置,然后添加到其后。
- SinglyLinkedListNode<T> lastNode = this.GetElementByIndex( this.Count - 1) ;
- lastNode.Next = currItemNode ;
- }
- }
- public void Clear()
- {
- //清空头对象引用,所有的链表对象,注定将遭受被回收的命运,有点火烧赤壁的感觉
- this.firstNode = null ;
- }
- //检查某对象是不是存在于本链表中
- public bool Contains( T item )
- {
- return ( this.IndexOf( item ) != -1 ) ;
- }
- //复制
- public void CopyTo( T[] array , int arrayIndex )
- {
- throw new NotImplementedException();
- }
- //计算链表元素合
- public int Count
- {
- get
- {
- int reCount = 0 ;
- if( this.firstNode != null )
- {
- SinglyLinkedListNode<T> tempNode = this.firstNode ;
- int i = 0 ;
- while( tempNode != null )
- {
- tempNode = tempNode.Next ;
- ++ i ;
- }
- reCount = i;
- }
- return reCount ;
- }
- }
- //我们的链表中没有加入是不是只读的判断,所以在这里,忽略它
- public bool IsReadOnly
- {
- get { return false ; }
- }
- //删除链表中的元素
- public bool Remove( T item )
- {
- bool flag = false ;
- if( item == null )
- {
- throw new Exception( "元素为空,无法进行删除!" ) ;
- }
- int index = this.IndexOf( item ) ;
- //判断是否为头元素
- if( index == 0 )
- {
- this.firstNode = this.firstNode.Next ;
- flag = true ;
- }
- else
- {
- //直接更新当前元素的上一个元素的Next引用指向当前元素的Next,这叫个别扭
- SinglyLinkedListNode<T> currNode = this.GetElementByIndex( index ) ;
- SinglyLinkedListNode<T> upNode = this.GetElementByIndex( index - 1) ;
- upNode.Next = currNode.Next ;
- flag = true ;
- }
- return flag ;
- }
- #endregion
- //IEnumerable<T> 佚代接口的实现
- #region IEnumerable<T> 成员
- public IEnumerator<T> GetEnumerator()
- {
- return new SingLyLinkedListEnumerator<T>(this);
- }
- #endregion
- #region IEnumerable 成员
- //显示实现IEnumerator接口
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- #endregion
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Emep.Dotnet.Frame
- {
- /// <summary>
- /// 单向链表的节点类
- /// </summary>
- class SinglyLinkedListNode<T>
- {
- private T itemValue;
- //当前节点的内容元素
- public T ItemValue
- {
- get { return itemValue; }
- set { itemValue = value; }
- }
- private SinglyLinkedListNode<T> next;
- //当前节点对下一个节点的引用
- internal SinglyLinkedListNode<T> Next
- {
- get { return next; }
- set { next = value; }
- }
- /// <summary>
- /// 构造方法
- /// </summary>
- public SinglyLinkedListNode( T item )
- {
- if (item == null)
- {
- throw new Exception("链表节点参数不可以为空");
- }
- this.itemValue = item ;
- this.next = null;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Emep.Dotnet.Frame
- {
- /// <summary>
- /// 为链表实现的佚代器
- /// </summary>
- /// <typeparam name="T"></typeparam>
- class SingLyLinkedListEnumerator<T>:IEnumerator<T>
- {
- //当前节点
- private SinglyLinkedListNode<T> currNode ;
- //要佚代的链表
- private readonly SinglyLinkedList<T> list ;
- //构造方法初始要佚代的对象
- public SingLyLinkedListEnumerator( SinglyLinkedList<T> list )
- {
- this.list = list ;
- }
- #region IEnumerator<T> 成员
- public T Current
- {
- get
- {
- return this.currNode.ItemValue ;
- }
- }
- #endregion
- #region IDisposable 成员
- //回收本类中例用的引用类型
- public void Dispose()
- {
- this.currNode = null ;
- }
- #endregion
- #region IEnumerator 成员
- object System.Collections.IEnumerator.Current
- {
- get
- {
- return this.Current ;
- }
- }
- public bool MoveNext()
- {
- bool flag = false ;
- if ( this.currNode == null )
- {
- this.currNode = this.list.FirstNode ;
- flag = true ;
- }
- else
- {
- if( this.currNode.Next != null )
- {
- this.currNode = this.currNode.Next ;
- flag = true ;
- }
- }
- return flag ;
- }
- public void Reset()
- {
- this.currNode = null;
- }
- #endregion
- }
- }