您的位置:首页 > 编程语言 > C#

C#实现双向链表

2008-12-19 03:35 417 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace 双向链表

{

class Program

{

static void Main(string[] args)

{

DoubleLinkedList<Person> list = new DoubleLinkedList<Person>() ;

Person per1 = new Person( "aladdin" , 1 ) ;

Person per2 = new Person( "jacky" , 1 ) ;

Person per3 = new Person( "fuck" , 1 ) ;

Person per4 = new Person( "zhao" , 1 ) ;

Person per5 = new Person( "aladdin2" , 1 ) ;

Person per6 = new Person( "jacky2" , 1 ) ;

Person per7 = new Person( "fuck2" , 1 ) ;

Person per8 = new Person( "zhao2" , 1 ) ;

list.Add( per1 ) ;

list.Add( per2 ) ;

list.Add( per3 ) ;

list.Add( per4 ) ;

list.Add( per5 ) ;

list.Add( per6 ) ;

list.Add( per7 ) ;

list.Add( per8 ) ;

Console.WriteLine( "链表大小:{0}" , list.Count ) ;

Console.WriteLine( "检查Pers8是否存在与链表中:{0}" , list.Contains( per8 ) ) ;

Person per9 = new Person( "per9" , 100 ) ;

Console.WriteLine( "检查Pers9是否存在与链表中:{0}" , list.Contains( per9 ) ) ;

Console.WriteLine( "Per8的索引值{0}", list.IndexOf( per8 ) ) ;

Console.WriteLine( "Per9的索引值{0}", list.IndexOf( per9 ) ) ;

Console.WriteLine( "索引7点的元素值:{0}" , list.GetElementByIndex( 7 ).name) ;

Person[] pers = new Person[ 20 ] ;

list.CopyTo( pers , 3 ) ;

foreach( Person p in pers )

{

if( p == null )

{

Console.WriteLine( "空..." ) ;

}

else

{

Console.WriteLine( p.name ) ;

}

}

foreach( Person per in list )

{

Console.WriteLine( "名字:{0} 年纪:{1}" , per.name , per.age ) ;

}

list.Remove( list.First ) ;

list.Remove( list.Last ) ;

list.AddBefore( list.First , new Person( "1111" ,111 ) ) ;

list.AddBefore( list.Last , new Person( "222" ,222 ) ) ;

list.AddAfter( list.First , new Person( "333" ,333 ) ) ;

list.AddAfter( list.Last , new Person( "444" ,444 ) ) ;

Console.WriteLine( "--------------------------------" ) ;

DoubleLinkedListNode<Person> node5 = list.GetNodeByIndex( 5 ) ;

list.AddAfter( node5 , new Person( "aaa" , 1 ) );

list.AddAfter( node5 , new Person( "aaa" , 2 ) );

list.AddFirst( new Person( "第一" , 1 ) ) ;

list.AddLast( new Person( "最后" , 1 ) ) ;

list.AddFirst( new Person( "第一" , 2 ) ) ;

list.AddLast( new Person( "最后" , 2 ) ) ;

foreach( Person per in list )

{

Console.WriteLine( "名字:{0} 年纪:{1}" , per.name , per.age ) ;

}

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 双向链表

{

class DoubleLinkedListNode<T>

{

/// <summary>

/// 下一个元素的指针

/// </summary>

private DoubleLinkedListNode<T> next;

internal DoubleLinkedListNode<T> Next

{

get { return next; }

set { next = value; }

}

/// <summary>

/// 上一个元素的指针

/// </summary>

private DoubleLinkedListNode<T> prev;

internal DoubleLinkedListNode<T> Prev

{

get { return prev; }

set { prev = value; }

}

/// <summary>

/// 当前元素所包含的值

/// </summary>

private T itemValue;

public T ItemValue

{

get { return itemValue; }

set { itemValue = value; }

}

/// <summary>

/// 构造函数

/// </summary>

/// <param name="itemVale"></param>

public DoubleLinkedListNode( T itemValue )

{

this.itemValue = itemValue ;

}

}

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace 双向链表

{

interface ILinkedList<T>

{

/// <summary>

/// 头元素

/// </summary>

DoubleLinkedListNode<T> First { get; }

/// <summary>

/// 尾元素

/// </summary>

DoubleLinkedListNode<T> Last { get; }

/// <summary>

/// 在 LinkedList<(Of <(T>)>) 中指定的现有节点后添加指定的新节点。

/// </summary>

/// <param name="node"></param>

/// <param name="newNode"></param>

void AddAfter( DoubleLinkedListNode<T> node , DoubleLinkedListNode<T> newNode ) ;

/// <summary>

/// 在 LinkedList<(Of <(T>)>) 中的现有节点后添加新的节点或值。

/// </summary>

/// <param name="node"></param>

/// <param name="t"></param>

void AddAfter( DoubleLinkedListNode<T> node , T t ) ;

/// <summary>

/// 在 LinkedList<(Of <(T>)>) 中指定的现有节点前添加指定的新节点。

/// </summary>

/// <param name="node"></param>

/// <param name="newNode"></param>

void AddBefore( DoubleLinkedListNode<T> node , DoubleLinkedListNode<T> newNode ) ;

/// <summary>

/// 在 LinkedList<(Of <(T>)>) 中指定的现有节点前添加包含指定值的新节点。

/// </summary>

/// <param name="node"></param>

/// <param name="t"></param>

void AddBefore(DoubleLinkedListNode<T> node, T t);

/// <summary>

/// 在 LinkedList<(Of <(T>)>) 的开头处添加包含指定值的新节点。

/// </summary>

/// <param name="value"></param>

/// <returns></returns>

DoubleLinkedListNode<T> AddFirst( T value ) ;

/// <summary>

/// 在 LinkedList<(Of <(T>)>) 的开头处添加指定的新节点

/// </summary>

/// <param name="node"></param>

void AddFirst( DoubleLinkedListNode<T> node ) ;

/// <summary>

/// 在 LinkedList<(Of <(T>)>) 的结尾处添加包含指定值的新节点。

/// </summary>

/// <param name="value"></param>

/// <returns></returns>

DoubleLinkedListNode<T> AddLast( T value ) ;

/// <summary>

/// 在 LinkedList<(Of <(T>)>) 的结尾处添加指定的新节点

/// </summary>

/// <param name="node"></param>

void AddLast( DoubleLinkedListNode<T> node ) ;

/// <summary>

/// 从 LinkedList<(Of <(T>)>) 中移除指定的节点。

/// </summary>

/// <param name="node"></param>

bool Remove ( DoubleLinkedListNode<T> node );

/// <summary>

/// 从 LinkedList<(Of <(T>)>) 中按索引删除节点。

/// </summary>

/// <param name="node"></param>

bool Remove ( int index ) ;

/// <summary>

/// 从 LinkedList<(Of <(T>)>) 中查找第一个匹配项

/// </summary>

/// <param name="value"></param>

/// <returns></returns>

DoubleLinkedListNode<T> Find(T value);

/// <summary>

/// 从 LinkedList<(Of <(T>)>) 中查找最后一个匹配项

/// </summary>

/// <param name="value"></param>

/// <returns></returns>

DoubleLinkedListNode<T> FindLast( T value ) ;

/// <summary>

/// 查询元素的索引

/// </summary>

/// <param name="item"></param>

/// <returns></returns>

int IndexOf( T item ) ;

/// <summary>

/// 能过索引得到元素的元素

/// </summary>

/// <param name="index"></param>

/// <returns></returns>

T GetElementByIndex( int index ) ;

/// <summary>

/// 通过索引得到节点对象

/// </summary>

/// <param name="index"></param>

/// <returns></returns>

DoubleLinkedListNode<T> GetNodeByIndex( int index ) ;

}

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace 双向链表

{

class DoubleLinkedList<T> : ICollection<T> , ILinkedList<T>

{

private DoubleLinkedListNode<T> first ;

private DoubleLinkedListNode<T> last ;

#region ICollection<T> 成员

public void Add( T item )

{

if( item == null )

{

throw new Exception( "元素为空,元法进行添加!" ) ;

}

//要添加的节点

DoubleLinkedListNode<T> addItem = new DoubleLinkedListNode<T>( item ) ;

//把新节点添加到链表的表尾

if( this.first == null )

{

//如果没有过行过添加

this.first = addItem ;

//第一次添加时,头尾指针都应是一个对象

this.last = this.first ;

}

else

{

//如果链表中不为空

this.last.Next = addItem ;

addItem.Prev = this.last ;

this.last = addItem ;

}

}

public void Clear()

{

this.first = null ;

}

public bool Contains( T item )

{

if( item == null )

{

throw new Exception( "元素为空,无法检测对象是否存在与链表中." ) ;

}

bool flag = false ;

if( this.Count != 0 )

{

DoubleLinkedListNode<T> node = this.first ;

while( node != null )

{

if( node.ItemValue.Equals( item ) )

{

flag = true ;

break ;

}

node = node.Next ;

}

}

return flag ;

}

public void CopyTo( T[] array , int arrayIndex )

{

if( array == null )

{

throw new Exception( "目标数组不可以为空,无法复制" ) ;

}

if( arrayIndex < 0 || arrayIndex >= array.Length )

{

throw new Exception( "索引值非法,无法进行复制." ) ;

}

if( (array.Length-arrayIndex) < this.Count || array.Length < this.Count )

{

throw new Exception( "目标数组容量不够,无法进行复制." ) ;

}

for( int i = 0 ; i < this.Count ; i ++ )

{

T item = this.GetElementByIndex( i ) ;

array[ arrayIndex ] = item ;

++ arrayIndex ;

}

}

public int Count

{

get

{

int i = 0 ;

DoubleLinkedListNode<T> node = this.first ;

while( node != null )

{

++ i ;

node = node.Next ;

}

return i ;

}

}

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 != -1 )

{

flag = this.Remove( index ) ;

}

else

{

throw new Exception( "元素在链表中不存在,无法删除。" ) ;

}

return flag ;

}

#endregion

#region IEnumerable<T> 成员

public IEnumerator<T> GetEnumerator()

{

DoubleLinkedListNode<T> currNode = this.first ;

while( currNode != null )

{

yield return currNode.ItemValue ;

currNode = currNode.Next ;

}

}

#endregion

#region IEnumerable 成员

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()

{

return GetEnumerator() ;

}

#endregion

#region ILinkedList<T> 成员

public DoubleLinkedListNode<T> First

{

get { return this.first ;}

}

public DoubleLinkedListNode<T> Last

{

get { return this.last ; }

}

public void AddAfter( DoubleLinkedListNode<T> node , DoubleLinkedListNode<T> newNode )

{

this.AddAfter( node , newNode.ItemValue ) ;

}

public void AddAfter( DoubleLinkedListNode<T> node , T t )

{

if( node == null || t == null )

{

throw new Exception( "元素为空,不可以进行插入") ;

}

int index = this.IndexOf( node.ItemValue ) ;

if( index != -1 )

{

DoubleLinkedListNode<T> currNode = this.GetNodeByIndex( index ) ;

DoubleLinkedListNode<T> upNode = currNode.Prev ;

DoubleLinkedListNode<T> nextNode = currNode.Next ;

DoubleLinkedListNode<T> newNode = new DoubleLinkedListNode<T>( t ) ;

if( index == 0 )

{

this.first.Next = newNode ;

newNode.Prev = this.first ;

newNode.Next = nextNode ;

}

else if( index == this.Count - 1 )

{

newNode.Prev = this.last ;

this.last.Next = newNode ;

this.last = newNode ;

}

else

{

nextNode.Prev = newNode ;

currNode.Next = newNode ;

newNode.Prev = currNode ;

newNode.Next = nextNode ;

}

}

}

public void AddBefore(DoubleLinkedListNode<T> node, DoubleLinkedListNode<T> newNode)

{

this.AddBefore( node , newNode.ItemValue ) ;

}

public void AddBefore(DoubleLinkedListNode<T> node, T t)

{

if( node == null || t == null )

{

throw new Exception( "元素为空,不可以进行插入") ;

}

int index = this.IndexOf( node.ItemValue ) ;

if( index != -1 )

{

DoubleLinkedListNode<T> currNode = this.GetNodeByIndex( index ) ;

DoubleLinkedListNode<T> upNode = currNode.Prev ;

DoubleLinkedListNode<T> nextNode = currNode.Next ;

DoubleLinkedListNode<T> newNode = new DoubleLinkedListNode<T>( t ) ;

if( index == 0 )

{

this.first.Prev = newNode ;

newNode.Next = this.first ;

this.first = newNode ;

}

else if( index == this.Count - 1 )

{

upNode.Next = newNode ;

newNode.Next = this.last ;

this.last.Prev = newNode ;

}

else

{

upNode.Next = newNode ;

nextNode.Prev = newNode ;

currNode.Prev = upNode ;

currNode.Next = nextNode ;

}

}

}

public DoubleLinkedListNode<T> AddFirst( T value )

{

if( value == null )

{

throw new Exception( "对不起,元素为空,不可以添加到链表开头" ) ;

}

DoubleLinkedListNode<T> node = new DoubleLinkedListNode<T>( value ) ;

this.first.Prev = node ;

node.Next = this.first ;

this.first = node ;

return this.first ;

}

public void AddFirst( DoubleLinkedListNode<T> node )

{

this.AddFirst( node.ItemValue ) ;

}

public DoubleLinkedListNode<T> AddLast( T value )

{

if( value == null )

{

throw new Exception( "对不起,元素为空,不可以添加到链表开头" ) ;

}

DoubleLinkedListNode<T> node = new DoubleLinkedListNode<T>( value ) ;

this.last.Next = node ;

node.Prev = this.last ;

this.last = node ;

return this.last ;

}

public void AddLast( DoubleLinkedListNode<T> node )

{

this.AddLast( node.ItemValue ) ;

}

public bool Remove( DoubleLinkedListNode<T> node )

{

if( node == null )

{

throw new Exception( "节点不可以为空,无法进行删除" ) ;

}

int index = this.IndexOf( node.ItemValue ) ;

return this.Remove( index ) ;

}

public bool Remove( int index )

{

if( index < 0 || index >= this.Count )

{

throw new Exception( "索引值非法,无法进行删除。" ) ;

}

bool flag = false ;

DoubleLinkedListNode<T> node = this.first ;

int i = 0 ;

while( node != null )

{

if( i == index )

{

DoubleLinkedListNode<T> upNode = node.Prev ;

DoubleLinkedListNode<T> nextNode = node.Next ;

if( index == 0 )

{

this.first = node.Next ;

node = null ;

}

else if ( index == this.Count - 1 )

{

this.last = upNode ;

this.last.Next = null ;

node = null ;

}

else

{

upNode.Next = nextNode ;

nextNode.Prev = upNode ;

node = null ;

}

flag = true ;

break ;

}

node = node.Next ;

++ i ;

}

return flag ;

}

public int IndexOf( T item )

{

if( item == null || this.Count == 0 || this.first == null )

{

throw new Exception( "无法得到元素索引" ) ;

}

int reInt = -1 ;

int i = 0 ;

DoubleLinkedListNode<T> node = this.first ;

while( node != null )

{

if( node.ItemValue.Equals( item ) )

{

reInt = i ;

break ;

}

++i ;

node = node.Next ;

}

return reInt ;

}

public T GetElementByIndex( int index )

{

if( index < 0 || index>= this.Count )

{

throw new Exception( "索引值非法,无法得对象." ) ;

}

DoubleLinkedListNode<T> reNode = this.first ;

int i = 0 ;

while( reNode != null )

{

if( i == index )

{

break ;

}

reNode = reNode.Next ;

++ i ;

}

return reNode.ItemValue ;

}

public DoubleLinkedListNode<T> GetNodeByIndex(int index)

{

if( index < 0 || index>= this.Count )

{

throw new Exception( "索引值非法,无法得对象节点." ) ;

}

DoubleLinkedListNode<T> reNode = this.first ;

int i = 0 ;

while( reNode != null )

{

if( i == index )

{

break ;

}

reNode = reNode.Next ;

++ i ;

}

return reNode ;

}

public DoubleLinkedListNode<T> Find(T value)

{

throw new NotImplementedException();

}

public DoubleLinkedListNode<T> FindLast(T value)

{

throw new NotImplementedException();

}

#endregion

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: