数据结构(C#):单链表
2008-12-06 15:43
176 查看
与顺序表相比,链表有自己的特点:插入、删除操作无需移动元素;能够高效实现动态内存分配;但不能按节点索引快速定位到节点;由于需要记录指针域,系统开销较大。
本篇介绍单链表的实现,使用上一篇定义的接口。
代码:
/*
* File : SingleLinkedList.cs
* Author : Zhenxing Zhou
* Date : 2008-12-06
* Blog : http://www.xianfen.net/ */
using System;
using System.Collections;
using System.Collections.Generic;
namespace Xianfen.Net.DataStructure
{
// 定义节点
internal class SingleLinkedListNode<T>
{
private T m_Value;
private SingleLinkedListNode<T> m_Next;
public T Value
{
get { return m_Value; }
set { m_Value = value; }
}
public SingleLinkedListNode<T> Next
{
get { return m_Next; }
set { m_Next = value; }
}
public SingleLinkedListNode()
{
m_Next = null;
}
public SingleLinkedListNode(T t)
: this()
{
Value = t;
}
}
// 实现单链表
public class SingleLinkedList<T> : ILinearList<T>
{
private int m_Count;
private SingleLinkedListNode<T> m_Head;
public SingleLinkedList()
{
m_Count = 0;
m_Head = new SingleLinkedListNode<T>();
}
public SingleLinkedList(T t)
: this()
{
m_Count = 1;
m_Head.Next = new SingleLinkedListNode<T>(t);
}
public bool IsEmpty
{
get { return m_Count == 0; }
}
public int Count
{
get { return m_Count; }
}
public void Add(T t)
{
InsertAt(t, this.Count);
}
public void AddHead(T t)
{
InsertAt(t, 0);
}
public void AddTail(T t)
{
Add(t);
}
public void InsertAt(T t, int pos)
{
if (pos > this.Count || pos < 0)
{
throw new IndexOutOfRangeException("pos");
}
if (m_Count == int.MaxValue)
{
throw new ArithmeticException();
}
SingleLinkedListNode<T> currentNode = m_Head;
while (pos-- > 0)
{
currentNode = currentNode.Next;
}
SingleLinkedListNode<T> tempNode = new SingleLinkedListNode<T>(t);
tempNode.Next = currentNode.Next;
currentNode.Next = tempNode;
m_Count++;
}
public void RemoveTail()
{
RemoveAt(this.Count - 1);
}
public void RemoveHead()
{
RemoveAt(0);
}
public void RemoveAt(int pos)
{
if (pos >= Count || pos < 0)
{
throw new IndexOutOfRangeException("pos");
}
SingleLinkedListNode<T> currentNode = m_Head;
while (pos-- > 0)
{
currentNode = currentNode.Next;
}
currentNode.Next = currentNode.Next.Next;
m_Count--;
}
public void RemoveAll()
{
m_Head.Next = null;
m_Count = 0;
}
public void Clear()
{
RemoveAll();
}
public T GetHead()
{
return GetAt(0);
}
public T GetTail()
{
return GetAt(this.Count - 1);
}
public T GetAt(int pos)
{
return GetNodeAt(pos).Value;
}
private SingleLinkedListNode<T> GetNodeAt(int pos)
{
if (pos >= Count || pos < 0)
{
throw new IndexOutOfRangeException("pos");
}
SingleLinkedListNode<T> currentNode = m_Head;
while (pos-- > 0)
{
currentNode = currentNode.Next;
}
return currentNode.Next;
}
public void SetAt(int pos, T t)
{
GetNodeAt(pos).Value = t;
}
public int Find(T t)
{
SingleLinkedListNode<T> node = m_Head;
int pos = 0;
while ((node = node.Next) != null)
{
if (node.Value.Equals(t))
{
return pos;
}
pos++;
}
return -1;
}
public IEnumerator<T> GetEnumerator()
{
SingleLinkedListNode<T> node = m_Head;
while ((node = node.Next) != null)
{
yield return node.Value;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
如果发现BUG请留言指出,下一篇介绍双链表的实现。
本篇介绍单链表的实现,使用上一篇定义的接口。
代码:
/*
* File : SingleLinkedList.cs
* Author : Zhenxing Zhou
* Date : 2008-12-06
* Blog : http://www.xianfen.net/ */
using System;
using System.Collections;
using System.Collections.Generic;
namespace Xianfen.Net.DataStructure
{
// 定义节点
internal class SingleLinkedListNode<T>
{
private T m_Value;
private SingleLinkedListNode<T> m_Next;
public T Value
{
get { return m_Value; }
set { m_Value = value; }
}
public SingleLinkedListNode<T> Next
{
get { return m_Next; }
set { m_Next = value; }
}
public SingleLinkedListNode()
{
m_Next = null;
}
public SingleLinkedListNode(T t)
: this()
{
Value = t;
}
}
// 实现单链表
public class SingleLinkedList<T> : ILinearList<T>
{
private int m_Count;
private SingleLinkedListNode<T> m_Head;
public SingleLinkedList()
{
m_Count = 0;
m_Head = new SingleLinkedListNode<T>();
}
public SingleLinkedList(T t)
: this()
{
m_Count = 1;
m_Head.Next = new SingleLinkedListNode<T>(t);
}
public bool IsEmpty
{
get { return m_Count == 0; }
}
public int Count
{
get { return m_Count; }
}
public void Add(T t)
{
InsertAt(t, this.Count);
}
public void AddHead(T t)
{
InsertAt(t, 0);
}
public void AddTail(T t)
{
Add(t);
}
public void InsertAt(T t, int pos)
{
if (pos > this.Count || pos < 0)
{
throw new IndexOutOfRangeException("pos");
}
if (m_Count == int.MaxValue)
{
throw new ArithmeticException();
}
SingleLinkedListNode<T> currentNode = m_Head;
while (pos-- > 0)
{
currentNode = currentNode.Next;
}
SingleLinkedListNode<T> tempNode = new SingleLinkedListNode<T>(t);
tempNode.Next = currentNode.Next;
currentNode.Next = tempNode;
m_Count++;
}
public void RemoveTail()
{
RemoveAt(this.Count - 1);
}
public void RemoveHead()
{
RemoveAt(0);
}
public void RemoveAt(int pos)
{
if (pos >= Count || pos < 0)
{
throw new IndexOutOfRangeException("pos");
}
SingleLinkedListNode<T> currentNode = m_Head;
while (pos-- > 0)
{
currentNode = currentNode.Next;
}
currentNode.Next = currentNode.Next.Next;
m_Count--;
}
public void RemoveAll()
{
m_Head.Next = null;
m_Count = 0;
}
public void Clear()
{
RemoveAll();
}
public T GetHead()
{
return GetAt(0);
}
public T GetTail()
{
return GetAt(this.Count - 1);
}
public T GetAt(int pos)
{
return GetNodeAt(pos).Value;
}
private SingleLinkedListNode<T> GetNodeAt(int pos)
{
if (pos >= Count || pos < 0)
{
throw new IndexOutOfRangeException("pos");
}
SingleLinkedListNode<T> currentNode = m_Head;
while (pos-- > 0)
{
currentNode = currentNode.Next;
}
return currentNode.Next;
}
public void SetAt(int pos, T t)
{
GetNodeAt(pos).Value = t;
}
public int Find(T t)
{
SingleLinkedListNode<T> node = m_Head;
int pos = 0;
while ((node = node.Next) != null)
{
if (node.Value.Equals(t))
{
return pos;
}
pos++;
}
return -1;
}
public IEnumerator<T> GetEnumerator()
{
SingleLinkedListNode<T> node = m_Head;
while ((node = node.Next) != null)
{
yield return node.Value;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
如果发现BUG请留言指出,下一篇介绍双链表的实现。
相关文章推荐
- 基本数据结构(C#实现):单链表
- 数据结构(C#)--单链表
- 数据结构单链表的实现
- 数据结构-单链表的java实现
- 数据结构 - 如何判断两个无环单链表是否相交;如果相交,给出相交的第一个结点
- 数据结构_单链表
- 数据结构->单链表的操作
- 数据结构中单链表的实现+单链表的C语言实现源代码
- 数据结构学习之单链表
- 数据结构 C#描述 第八章 前篇
- 数据结构和算法之C#
- C#数据结构和算法学习系列三----ArrayList类的特性及应用
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- C#与数据结构
- C# 一个自己写的树结构代码(2)-Array,HashTable,List,String数据结构操作封装
- C# 单链表
- c#常用数据结构解析
- 数据结构例程——单链表的建立
- 数据结构 ——单链表操作的算法
- 数据结构学习(二)——单链表的操作之头插法和尾插法创建链表