C#实现单链表
C#实现单链表的全部功能。
在C#中,LinkedList<T>实现的是数据结构中的双链表,并没有对单链表的实现,查找了一下网络中的各种C#实现的单链表,通常都是简单的实现一点儿功能,在教学中讲授集合的时候,笔者将C#实现单链表完整的实现,与大家共享。
一共包含三个类,类之间的继承关系及实现的接口如下图所示:
SinglyLinkedListNode类:实现单链表中的每一个节点,代码如下:
Code
namespace CollectionSample.SinglyLinkedListStruct
{
/// <summary>
/// 封装单链表中的每一个元素
/// </summary>
/// <author>Hailong.Li @ Dalian Hi-Think Computer Technology Corp</author>
/// <author>李海龙 大连华信计算机新技术培训中心</author>
/// <typeparam name="T"></typeparam>
public sealed class SinglyLinkedListNode<T>
{
private T m_ItemValue;
private SinglyLinkedListNode<T> m_NextItem;
/// <summary>
/// 表示元素的值
/// </summary>
public T ItemValue
{
set
{
m_ItemValue = value;
}
get
{
return m_ItemValue;
}
}
/// <summary>
/// 引用到此元素的下一个元素
/// </summary>
public SinglyLinkedListNode<T> NextItem
{
set
{
m_NextItem = value;
}
get
{
return m_NextItem;
}
}
/// <summary>
/// 构造方法,为ItemValue和NextItem赋值
/// </summary>
/// <param name="m_ItemValue"></param>
/// <param name="m_NextItem"></param>
public SinglyLinkedListNode(T m_ItemValue, SinglyLinkedListNode<T> m_NextItem)
{
this.m_ItemValue = m_ItemValue;
this.m_NextItem = m_NextItem;
}
/// <summary>
/// 构造方法,为NextItem赋值,ItemValue为默认值
/// </summary>
/// <param name="m_NextItem"></param>
public SinglyLinkedListNode(SinglyLinkedListNode<T> m_NextItem)
: this(default(T), m_NextItem)
{
}
/// <summary>
/// 构造方法,为ItemValue赋值,NextItem为默认值
/// </summary>
/// <param name="m_ItemValue"></param>
public SinglyLinkedListNode(T m_ItemValue)
: this(m_ItemValue, null)
{
}
/// <summary>
/// 构造方法,NextItem和ItemValue均为默认值
/// </summary>
public SinglyLinkedListNode()
: this(default(T), null)
{
}
}
}
namespace CollectionSample.SinglyLinkedListStruct
{
/// <summary>
/// 封装单链表中的每一个元素
/// </summary>
/// <author>Hailong.Li @ Dalian Hi-Think Computer Technology Corp</author>
/// <author>李海龙 大连华信计算机新技术培训中心</author>
/// <typeparam name="T"></typeparam>
public sealed class SinglyLinkedListNode<T>
{
private T m_ItemValue;
private SinglyLinkedListNode<T> m_NextItem;
/// <summary>
/// 表示元素的值
/// </summary>
public T ItemValue
{
set
{
m_ItemValue = value;
}
get
{
return m_ItemValue;
}
}
/// <summary>
/// 引用到此元素的下一个元素
/// </summary>
public SinglyLinkedListNode<T> NextItem
{
set
{
m_NextItem = value;
}
get
{
return m_NextItem;
}
}
/// <summary>
/// 构造方法,为ItemValue和NextItem赋值
/// </summary>
/// <param name="m_ItemValue"></param>
/// <param name="m_NextItem"></param>
public SinglyLinkedListNode(T m_ItemValue, SinglyLinkedListNode<T> m_NextItem)
{
this.m_ItemValue = m_ItemValue;
this.m_NextItem = m_NextItem;
}
/// <summary>
/// 构造方法,为NextItem赋值,ItemValue为默认值
/// </summary>
/// <param name="m_NextItem"></param>
public SinglyLinkedListNode(SinglyLinkedListNode<T> m_NextItem)
: this(default(T), m_NextItem)
{
}
/// <summary>
/// 构造方法,为ItemValue赋值,NextItem为默认值
/// </summary>
/// <param name="m_ItemValue"></param>
public SinglyLinkedListNode(T m_ItemValue)
: this(m_ItemValue, null)
{
}
/// <summary>
/// 构造方法,NextItem和ItemValue均为默认值
/// </summary>
public SinglyLinkedListNode()
: this(default(T), null)
{
}
}
}
SinglyLinkedListEnumerator类:实现对单链表的迭代(即可以使用foreach循环遍历),代码如下:
Code
using System.Collections;
using System.Collections.Generic;
namespace CollectionSample.SinglyLinkedListStruct
{
/// <summary>
/// 实现对单链表的迭代
/// </summary>
/// <author>Hailong.Li @ Dalian Hi-Think Computer Technology Corp</author>
/// <author>李海龙 大连华信计算机新技术培训中心</author>
/// <typeparam name="T"></typeparam>
public sealed class SinglyLinkedListEnumberator<T> : IEnumerator<T>
{
private SinglyLinkedListNode<T> m_CurrentNode;
private readonly SinglyLinkedList<T> list;
public SinglyLinkedListEnumberator(SinglyLinkedList<T> list)
{
this.list = list;
}
#region IEnumerator<T> Members
/// <summary>
/// 返回当前元素的值
/// </summary>
public T Current
{
get
{
return m_CurrentNode.ItemValue;
}
}
#endregion
#region IDisposable Members
public void Dispose()
{
m_CurrentNode = null;
}
#endregion
#region IEnumerator Members
object IEnumerator.Current
{
get
{
return Current;
}
}
/// <summary>
/// 移到下一个元素,返回是否存在下一个元素
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
if (m_CurrentNode == null)
{
m_CurrentNode = list.HeadElement;
}
else
{
m_CurrentNode = m_CurrentNode.NextItem;
}
return m_CurrentNode.NextItem != null;
}
/// <summary>
/// 重置迭代器
/// </summary>
public void Reset()
{
m_CurrentNode = null;
}
#endregion
}
}
using System.Collections;
using System.Collections.Generic;
namespace CollectionSample.SinglyLinkedListStruct
{
/// <summary>
/// 实现对单链表的迭代
/// </summary>
/// <author>Hailong.Li @ Dalian Hi-Think Computer Technology Corp</author>
/// <author>李海龙 大连华信计算机新技术培训中心</author>
/// <typeparam name="T"></typeparam>
public sealed class SinglyLinkedListEnumberator<T> : IEnumerator<T>
{
private SinglyLinkedListNode<T> m_CurrentNode;
private readonly SinglyLinkedList<T> list;
public SinglyLinkedListEnumberator(SinglyLinkedList<T> list)
{
this.list = list;
}
#region IEnumerator<T> Members
/// <summary>
/// 返回当前元素的值
/// </summary>
public T Current
{
get
{
return m_CurrentNode.ItemValue;
}
}
#endregion
#region IDisposable Members
public void Dispose()
{
m_CurrentNode = null;
}
#endregion
#region IEnumerator Members
object IEnumerator.Current
{
get
{
return Current;
}
}
/// <summary>
/// 移到下一个元素,返回是否存在下一个元素
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
if (m_CurrentNode == null)
{
m_CurrentNode = list.HeadElement;
}
else
{
m_CurrentNode = m_CurrentNode.NextItem;
}
return m_CurrentNode.NextItem != null;
}
/// <summary>
/// 重置迭代器
/// </summary>
public void Reset()
{
m_CurrentNode = null;
}
#endregion
}
}
SinglyLinkedList类:封装单链表,代码如下:
Code
using System;
using System.Collections;
using System.Collections.Generic;
namespace CollectionSample.SinglyLinkedListStruct
{
/// <summary>
/// 实现单链表数据结构对数据的存储
/// </summary>
/// <author>Hailong.Li @ Dalian Hi-Think Computer Technology Corp</author>
/// <author>李海龙 大连华信计算机新技术培训中心</author>
/// <typeparam name="T"></typeparam>
public sealed class SinglyLinkedList<T> : ICollection<T>
{
private SinglyLinkedListNode<T> m_HeadElement;
/// <summary>
/// 构造方法
/// </summary>
public SinglyLinkedList()
{
m_HeadElement = null;
}
/// <summary>
/// 链表中的头元素
/// </summary>
public SinglyLinkedListNode<T> HeadElement
{
set
{
m_HeadElement = value;
}
get
{
return m_HeadElement;
}
}
/// <summary>
/// 返回某元素在链表中的“下标”
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public int IndexOf(T value)
{
int result = -1;
for (int i = 0; i < Count; i++)
{
SinglyLinkedListNode<T> node = ElementAt(i);
if (node.ItemValue.Equals(value))
{
result = i;
break;
}
}
return result;
}
/// <summary>
/// 返回某位置的元素值
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public SinglyLinkedListNode<T> ElementAt(int index)
{
if (HeadElement == null || index < 0 || index >= Count)
{
throw new Exception("No item or index out of range");
}
int i = 0;
SinglyLinkedListNode<T> result = HeadElement;
while (result.NextItem != null && i < index)
{
result = result.NextItem;
i++;
}
return result;
}
/// <summary>
/// 将某元素添加到某位置
/// </summary>
/// <param name="insertItem"></param>
/// <param name="index"></param>
public void Insert(T insertItem,int index)
{
SinglyLinkedListNode<T> node = new SinglyLinkedListNode<T>(insertItem);
if (index == 0)
{
node.NextItem = HeadElement;
HeadElement = node;
}
else
{
SinglyLinkedListNode<T> beforeInsertNode = ElementAt(index - 1);
SinglyLinkedListNode<T> currentNode = ElementAt(index);
beforeInsertNode.NextItem = node;
node.NextItem = currentNode;
}
}
/// <summary>
/// 将某元素添加到某元素前
/// </summary>
/// <param name="insertItem"></param>
/// <param name="item"></param>
public void Insert(T insertItem,T item)
{
int index = IndexOf(item);
Insert(insertItem, index);
}
#region ICollection<T> Members
/// <summary>
/// 实现ICollection<T>接口中的Add方法,
/// 将参数对象添加到链表的最后一位
/// </summary>
/// <param name="item"></param>
public void Add(T item)
{
SinglyLinkedListNode<T> node = new SinglyLinkedListNode<T>(item);
if (HeadElement == null)
{
HeadElement = node;
node.NextItem = null;
}
else
{
int lastIndex = Count - 1;
SinglyLinkedListNode<T> lastNode = ElementAt(lastIndex);
lastNode.NextItem = node;
node.NextItem = null;
}
}
/// <summary>
/// 实现ICollection<T>接口中的Clear方法,
/// 清空链表中的所有成员
/// </summary>
public void Clear()
{
HeadElement = null;
}
/// <summary>
/// 实现ICollection<T>接口中的Contains方法,
/// 返回在链表中是否存在参数对象
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Contains(T item)
{
return IndexOf(item) != -1;
}
/// <summary>
/// 实现ICollection<T>接口中的CopyTo方法,
/// 将链表中的元素复制到数组中
/// </summary>
/// <param name="array"></param>
/// <param name="arrayIndex"></param>
public void CopyTo(T[] array, int arrayIndex)
{
for (int i = 0; i < Count; i++)
{
array[arrayIndex + i] = ElementAt(i).ItemValue;
}
}
/// <summary>
/// 实现ICollection<T>接口中的Count只读属性
/// 返回链表中的元素数量
/// </summary>
public int Count
{
get
{
int result = 0;
if (HeadElement != null)
{
SinglyLinkedListNode<T> node = HeadElement;
while (node != null)
{
result++;
node = node.NextItem;
}
}
return result;
}
}
/// <summary>
/// 实现ICollection<T>接口中的IsReadOnly属性
/// 返回链表是否只读(返回False)
/// </summary>
public bool IsReadOnly
{
get
{
return false;
}
}
/// <summary>
/// 实现ICollection<T>接口中的Remove方法
/// 移除链中中能参数对象
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(T item)
{
bool result = false;
int index = IndexOf(item);
if (index != -1)
{
if (index == 0)
{
HeadElement = HeadElement.NextItem;
}
else
{
SinglyLinkedListNode<T> node = ElementAt(index);
SinglyLinkedListNode<T> beforeNode = ElementAt(index - 1);
beforeNode.NextItem = node.NextItem;
}
result = true;
}
return result;
}
#endregion
#region IEnumerable<T> Members
/// <summary>
/// 实现IEnumerable<T>接口中的GetEnumerator方法
/// 返回迭代器
/// </summary>
/// <returns></returns>
public IEnumerator<T> GetEnumerator()
{
return new SinglyLinkedListEnumberator<T>(this);
}
#endregion
#region IEnumerable Members
/// <summary>
/// 实现IEnumerable接口中的GetEnumerator方法
/// 返回迭代器
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace CollectionSample.SinglyLinkedListStruct
{
/// <summary>
/// 实现单链表数据结构对数据的存储
/// </summary>
/// <author>Hailong.Li @ Dalian Hi-Think Computer Technology Corp</author>
/// <author>李海龙 大连华信计算机新技术培训中心</author>
/// <typeparam name="T"></typeparam>
public sealed class SinglyLinkedList<T> : ICollection<T>
{
private SinglyLinkedListNode<T> m_HeadElement;
/// <summary>
/// 构造方法
/// </summary>
public SinglyLinkedList()
{
m_HeadElement = null;
}
/// <summary>
/// 链表中的头元素
/// </summary>
public SinglyLinkedListNode<T> HeadElement
{
set
{
m_HeadElement = value;
}
get
{
return m_HeadElement;
}
}
/// <summary>
/// 返回某元素在链表中的“下标”
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public int IndexOf(T value)
{
int result = -1;
for (int i = 0; i < Count; i++)
{
SinglyLinkedListNode<T> node = ElementAt(i);
if (node.ItemValue.Equals(value))
{
result = i;
break;
}
}
return result;
}
/// <summary>
/// 返回某位置的元素值
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public SinglyLinkedListNode<T> ElementAt(int index)
{
if (HeadElement == null || index < 0 || index >= Count)
{
throw new Exception("No item or index out of range");
}
int i = 0;
SinglyLinkedListNode<T> result = HeadElement;
while (result.NextItem != null && i < index)
{
result = result.NextItem;
i++;
}
return result;
}
/// <summary>
/// 将某元素添加到某位置
/// </summary>
/// <param name="insertItem"></param>
/// <param name="index"></param>
public void Insert(T insertItem,int index)
{
SinglyLinkedListNode<T> node = new SinglyLinkedListNode<T>(insertItem);
if (index == 0)
{
node.NextItem = HeadElement;
HeadElement = node;
}
else
{
SinglyLinkedListNode<T> beforeInsertNode = ElementAt(index - 1);
SinglyLinkedListNode<T> currentNode = ElementAt(index);
beforeInsertNode.NextItem = node;
node.NextItem = currentNode;
}
}
/// <summary>
/// 将某元素添加到某元素前
/// </summary>
/// <param name="insertItem"></param>
/// <param name="item"></param>
public void Insert(T insertItem,T item)
{
int index = IndexOf(item);
Insert(insertItem, index);
}
#region ICollection<T> Members
/// <summary>
/// 实现ICollection<T>接口中的Add方法,
/// 将参数对象添加到链表的最后一位
/// </summary>
/// <param name="item"></param>
public void Add(T item)
{
SinglyLinkedListNode<T> node = new SinglyLinkedListNode<T>(item);
if (HeadElement == null)
{
HeadElement = node;
node.NextItem = null;
}
else
{
int lastIndex = Count - 1;
SinglyLinkedListNode<T> lastNode = ElementAt(lastIndex);
lastNode.NextItem = node;
node.NextItem = null;
}
}
/// <summary>
/// 实现ICollection<T>接口中的Clear方法,
/// 清空链表中的所有成员
/// </summary>
public void Clear()
{
HeadElement = null;
}
/// <summary>
/// 实现ICollection<T>接口中的Contains方法,
/// 返回在链表中是否存在参数对象
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Contains(T item)
{
return IndexOf(item) != -1;
}
/// <summary>
/// 实现ICollection<T>接口中的CopyTo方法,
/// 将链表中的元素复制到数组中
/// </summary>
/// <param name="array"></param>
/// <param name="arrayIndex"></param>
public void CopyTo(T[] array, int arrayIndex)
{
for (int i = 0; i < Count; i++)
{
array[arrayIndex + i] = ElementAt(i).ItemValue;
}
}
/// <summary>
/// 实现ICollection<T>接口中的Count只读属性
/// 返回链表中的元素数量
/// </summary>
public int Count
{
get
{
int result = 0;
if (HeadElement != null)
{
SinglyLinkedListNode<T> node = HeadElement;
while (node != null)
{
result++;
node = node.NextItem;
}
}
return result;
}
}
/// <summary>
/// 实现ICollection<T>接口中的IsReadOnly属性
/// 返回链表是否只读(返回False)
/// </summary>
public bool IsReadOnly
{
get
{
return false;
}
}
/// <summary>
/// 实现ICollection<T>接口中的Remove方法
/// 移除链中中能参数对象
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(T item)
{
bool result = false;
int index = IndexOf(item);
if (index != -1)
{
if (index == 0)
{
HeadElement = HeadElement.NextItem;
}
else
{
SinglyLinkedListNode<T> node = ElementAt(index);
SinglyLinkedListNode<T> beforeNode = ElementAt(index - 1);
beforeNode.NextItem = node.NextItem;
}
result = true;
}
return result;
}
#endregion
#region IEnumerable<T> Members
/// <summary>
/// 实现IEnumerable<T>接口中的GetEnumerator方法
/// 返回迭代器
/// </summary>
/// <returns></returns>
public IEnumerator<T> GetEnumerator()
{
return new SinglyLinkedListEnumberator<T>(this);
}
#endregion
#region IEnumerable Members
/// <summary>
/// 实现IEnumerable接口中的GetEnumerator方法
/// 返回迭代器
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}
如果代码中存在问题,请各位不吝赐教!!!
本文版权归作者所有,未经同意,请勿用作商业用途。