- 浏览: 238263 次
- 性别:
- 来自: 济南
文章分类
- 全部博客 (205)
- jQuery (27)
- Flash AS3.0 (0)
- Html5+CSS3 (12)
- C# .Net (19)
- Xml XPath XSLT (5)
- Sql (3)
- JavaScript (20)
- struts2 (23)
- hibernate (24)
- spring (11)
- j2ee (16)
- jsp/java (11)
- 设计模式 (5)
- json (3)
- Java.IO (7)
- Java.Util (7)
- android (8)
- WebService (10)
- MyEclipse SVN (3)
- servlet (1)
- Exception (3)
- 自我学习 (2)
- UML (2)
- java泛型 (1)
- Lucene (7)
- HtmlParser (2)
- 概念理解 (3)
- 正则表达式 (1)
- EMail (1)
最新评论
-
hanmiao:
没用,干巴巴的壹堆代码,没明白到底区别在哪里。
List Set Map 区别! -
e421083458:
偶来过了!
C#单向链表的实现
/// <summary>
/// 双向链表节点类
/// </summary>
/// <typeparam name="T">节点中的存放的数据类型</typeparam>
public class Node<T>
{
/// <summary>
/// 当前节点的数据
/// </summary>
T data;
/// <summary>
/// 节点中存放的数据
/// </summary>
public T Data
{
get { return this.data; }
set { this.data = value; }
}
/// <summary>
/// 当前节点的下一个节点
/// </summary>
Node<T> next;
/// <summary>
/// 下一个节点
/// </summary>
public Node<T> Next
{
get { return this.next; }
set { this.next = value; }
}
/// <summary>
/// 当前节点的上一个节点
/// </summary>
Node<T> prev;
/// <summary>
/// 上一个节点
/// </summary>
public Node<T> Prev
{
get { return prev; }
set { prev = value; }
}
/// <summary>
/// 无参构造:数据为默认值,下一个节点为null,上一个节点也为null
/// </summary>
public Node()
{
this.data = default(T);
this.next = null;
this.prev = null;
}
/// <summary>
/// 构造方法:数据为传过来的t,下一个节点为null,上一个节点也为null
/// </summary>
/// <param name="t">传入的元素值</param>
public Node(T t)
{
this.data = t;
this.next = null;
this.prev = null;
}
/// <summary>
/// 构造方法:数据为t,下一个节点为node
/// </summary>
/// <param name="t">传入的元素值</param>
/// <param name="next">上一个节点</param>
/// <param name="prev">下一个节点</param>
public Node(T t, Node<T> next,Node<T> prev)
{
this.data = t;
this.next = next;
this.prev = prev;
}
/// <summary>
/// 此方法在调试过程中使用,可以删掉
/// </summary>
/// <returns></returns>
public override string ToString()
{
T p = this.prev == null ? default(T) : this.prev.data;
T n = this.next == null ? default(T) : this.next.data;
string s = string.Format("Data:{0},Prev:{1},Next:{2}",data,p,n);
return s;
}
}
/// <summary>
/// 双向链表接口
/// </summary>
/// <typeparam name="T">链表中元素的类型</typeparam>
public interface ILinkList<T>
{
void AddFirst(T t);
void AddLast(T t);
void Clear();
int Count { get; }
Node<T> Head { get; set; }
Node<T> Tail { get;set;}
void Insert(int index, T t);
bool IsEmpty { get; }
void RemoveAt(int index);
void RemoveFirst();
void RemoveLast();
Node<T> this[int index] { get; }
}
/// <summary>
/// 双向链表操作类
/// </summary>
/// <typeparam name="T">链表中元素的类型</typeparam>
public class LinkList<T> : ILinkList<T>
{
/// <summary>
/// 链表头节点
/// </summary>
Node<T> head;
/// <summary>
/// 链表头节点
/// </summary>
public Node<T> Head
{
get { return head; }
set { head = value; }
}
/// <summary>
/// 链表尾节点
/// </summary>
Node<T> tail;
/// <summary>
/// 链表尾节点
/// </summary>
public Node<T> Tail
{
get { return tail; }
set { tail = value; }
}
/// <summary>
/// 链表大小
/// </summary>
int size = 0;
/// <summary>
/// 添加节点到链表的开头
/// </summary>
/// <param name="t">要添加的数据</param>
public void AddFirst(T t)
{
Node<T> node = new Node<T>(t);
//如果头为null
if (head == null)
{
//把头节点设置为node
head = node;
//因为是空链表,所以头尾一致
tail = node;
//大小加一
size++;
return;
}
//原来头节点的上一个为新节点
head.Prev = node;
//新节点的下一个为原来的头节点
node.Next = head;
//新头节点为新节点
head = node;
//大小加一
size++;
}
/// <summary>
/// 添加节点到链表的末尾
/// </summary>
/// <param name="t">要添加的数据</param>
public void AddLast(T t)
{
Node<T> node = new Node<T>(t);
//如果头为null
if (head == null)
{
//把头节点设置为node
head = node;
//因为是空链表,所以头尾一致
tail = node;
//大小加一
size++;
return;
}
//将原尾节点的下一个设置为新节点
tail.Next = node;
//将新节点的上一个设置为原尾节点
node.Prev = tail;
//将尾节点重新设置为新节点
tail = node;
//大小加一
size++;
}
/// <summary>
/// 在给定的索引处插入数据
/// </summary>
/// <param name="index">索引</param>
/// <param name="t">要插入的数据</param>
public void Insert(int index, T t)
{
Node<T> node = new Node<T>(t);
//索引过小
if (index < 0)
{
throw new IndexOutOfRangeException();
}
//索引过大
if (index >= Count)
{
throw new IndexOutOfRangeException();
}
//如果链表是空的,而且索引大于0
if (IsEmpty && index > 0)
{
throw new IndexOutOfRangeException();
}
//如果索引为0,意味着向链表头部添加节点。
if (index == 0)
{
AddFirst(t);
return;
}
//要插入位置的节点
Node<T> current = head;
int i = 0;
while (true)
{
if (i == index)
{
break;
}
i++;
current = current.Next;
}
//此处非常重要,特别要注意先后次序
//当前节点的上一个的下一个设置为新节点
current.Prev.Next = node;
//新节点的上一个设置为当前节点的上一个
node.Prev = current.Prev;
//新节点的下一个设置为当前节点
node.Next = current;
//当前节点的上一个设置为新节点
current.Prev = node;
//大小加一
size++;
}
/// <summary>
/// 移除链表中的节点
/// </summary>
/// <param name="index">要移除的节点的索引</param>
public void RemoveAt(int index)
{
//链表头节点是空的
if (IsEmpty)
{
throw new Exception("链表是空的。");
}
//索引过小
if (index < 0)
{
throw new IndexOutOfRangeException();
}
//索引过大
if (index >= Count)
{
throw new IndexOutOfRangeException();
}
//如果要移除的是头节点
if (index == 0)
{
RemoveFirst();
return;
}
if (index==size-1)
{
RemoveLast();
return;
}
//要移除的节点
Node<T> current = head;
int i = 0;
while (true)
{
if (i == index)
{
break;
}
i++;
current = current.Next;
}
//当前节点的上一个的Next设置为当前节点的Next
current.Prev.Next = current.Next;
//当前节点的下一个的Prev设置为当前节点的Prev
current.Next.Prev = current.Prev;
//大小减一
size--;
}
/// <summary>
/// 移除头节点
/// </summary>
public void RemoveFirst()
{
//链表头节点是空的
if (IsEmpty)
{
throw new Exception("链表是空的。");
}
//如果size为1,那就是清空链表。
if (size==1)
{
Clear();
return;
}
//将头节点设为原头结点的下一个节点,就是下一个节点上移
head = head.Next;
//处理上一步遗留问题,原来的第二个节点的上一个是头结点,现在第二个要变成头节点,那要把它的Prev设为null才能成为头节点
head.Prev = null;
//大小减一
size--;
}
/// <summary>
/// 移除尾节点
/// </summary>
public void RemoveLast()
{
//链表头节点是空的
if (IsEmpty)
{
throw new Exception("链表是空的。");
}
//如果size为1,那就是清空链表。
if (size == 1)
{
Clear();
return;
}
//尾节点设置为倒数第二个节点
tail = tail.Prev;
//将新尾节点的Next设为null,表示它是新的尾节点
tail.Next = null;
//大小减一
size--;
}
/// <summary>
/// 判断链表是否是空的
/// </summary>
public bool IsEmpty
{
get
{
return head == null;
}
}
/// <summary>
/// 链表中元素的个数
/// </summary>
public int Count
{
get
{
////也可以采用遍历的方法获得长度,遍历可以从前向后,也可以从后向前
//int count = 0;
////取得链表头部节点
//Node<T> current = new Node<T>();
//current = head;
////遍历整个链表,直到最后一个Next为null的节点为止
//while (current!=null)
//{
// count++;
// current = current.Next;
//}
//return count;
return size;
}
}
/// <summary>
/// 清除链表中的数据
/// </summary>
public void Clear()
{
head = null;
tail = null;
size = 0;
}
/// <summary>
/// 根据索引获取链表中的节点
/// </summary>
/// <param name="index">整型索引</param>
/// <returns>节点</returns>
public Node<T> this[int index]
{
get
{
//链表头节点是空的
if (head == null)
{
throw new Exception("链表是空的。");
}
//索引过小
if (index < 0)
{
throw new IndexOutOfRangeException();
}
//索引过大
if (index >= Count)
{
throw new IndexOutOfRangeException();
}
//取得头节点
Node<T> current = new Node<T>();
//如果索引在前一半,那么从前向后找
if (index<size/2)
{
current = head;
int i = 0;
//遍历链表
while (true)
{
//找到第index个节点
if (i == index)
{
break;
}
current = current.Next;
i++;
}
return current;
}
else//如果索引在后一半,那么从后向前找
{
current = tail;
int i = size;
//遍历链表
while (true)
{
//找到第index个节点
if (i == index)
{
break;
}
current = current.Prev;
i--;
}
return current.Next;
}
}
}
}
/// 双向链表节点类
/// </summary>
/// <typeparam name="T">节点中的存放的数据类型</typeparam>
public class Node<T>
{
/// <summary>
/// 当前节点的数据
/// </summary>
T data;
/// <summary>
/// 节点中存放的数据
/// </summary>
public T Data
{
get { return this.data; }
set { this.data = value; }
}
/// <summary>
/// 当前节点的下一个节点
/// </summary>
Node<T> next;
/// <summary>
/// 下一个节点
/// </summary>
public Node<T> Next
{
get { return this.next; }
set { this.next = value; }
}
/// <summary>
/// 当前节点的上一个节点
/// </summary>
Node<T> prev;
/// <summary>
/// 上一个节点
/// </summary>
public Node<T> Prev
{
get { return prev; }
set { prev = value; }
}
/// <summary>
/// 无参构造:数据为默认值,下一个节点为null,上一个节点也为null
/// </summary>
public Node()
{
this.data = default(T);
this.next = null;
this.prev = null;
}
/// <summary>
/// 构造方法:数据为传过来的t,下一个节点为null,上一个节点也为null
/// </summary>
/// <param name="t">传入的元素值</param>
public Node(T t)
{
this.data = t;
this.next = null;
this.prev = null;
}
/// <summary>
/// 构造方法:数据为t,下一个节点为node
/// </summary>
/// <param name="t">传入的元素值</param>
/// <param name="next">上一个节点</param>
/// <param name="prev">下一个节点</param>
public Node(T t, Node<T> next,Node<T> prev)
{
this.data = t;
this.next = next;
this.prev = prev;
}
/// <summary>
/// 此方法在调试过程中使用,可以删掉
/// </summary>
/// <returns></returns>
public override string ToString()
{
T p = this.prev == null ? default(T) : this.prev.data;
T n = this.next == null ? default(T) : this.next.data;
string s = string.Format("Data:{0},Prev:{1},Next:{2}",data,p,n);
return s;
}
}
/// <summary>
/// 双向链表接口
/// </summary>
/// <typeparam name="T">链表中元素的类型</typeparam>
public interface ILinkList<T>
{
void AddFirst(T t);
void AddLast(T t);
void Clear();
int Count { get; }
Node<T> Head { get; set; }
Node<T> Tail { get;set;}
void Insert(int index, T t);
bool IsEmpty { get; }
void RemoveAt(int index);
void RemoveFirst();
void RemoveLast();
Node<T> this[int index] { get; }
}
/// <summary>
/// 双向链表操作类
/// </summary>
/// <typeparam name="T">链表中元素的类型</typeparam>
public class LinkList<T> : ILinkList<T>
{
/// <summary>
/// 链表头节点
/// </summary>
Node<T> head;
/// <summary>
/// 链表头节点
/// </summary>
public Node<T> Head
{
get { return head; }
set { head = value; }
}
/// <summary>
/// 链表尾节点
/// </summary>
Node<T> tail;
/// <summary>
/// 链表尾节点
/// </summary>
public Node<T> Tail
{
get { return tail; }
set { tail = value; }
}
/// <summary>
/// 链表大小
/// </summary>
int size = 0;
/// <summary>
/// 添加节点到链表的开头
/// </summary>
/// <param name="t">要添加的数据</param>
public void AddFirst(T t)
{
Node<T> node = new Node<T>(t);
//如果头为null
if (head == null)
{
//把头节点设置为node
head = node;
//因为是空链表,所以头尾一致
tail = node;
//大小加一
size++;
return;
}
//原来头节点的上一个为新节点
head.Prev = node;
//新节点的下一个为原来的头节点
node.Next = head;
//新头节点为新节点
head = node;
//大小加一
size++;
}
/// <summary>
/// 添加节点到链表的末尾
/// </summary>
/// <param name="t">要添加的数据</param>
public void AddLast(T t)
{
Node<T> node = new Node<T>(t);
//如果头为null
if (head == null)
{
//把头节点设置为node
head = node;
//因为是空链表,所以头尾一致
tail = node;
//大小加一
size++;
return;
}
//将原尾节点的下一个设置为新节点
tail.Next = node;
//将新节点的上一个设置为原尾节点
node.Prev = tail;
//将尾节点重新设置为新节点
tail = node;
//大小加一
size++;
}
/// <summary>
/// 在给定的索引处插入数据
/// </summary>
/// <param name="index">索引</param>
/// <param name="t">要插入的数据</param>
public void Insert(int index, T t)
{
Node<T> node = new Node<T>(t);
//索引过小
if (index < 0)
{
throw new IndexOutOfRangeException();
}
//索引过大
if (index >= Count)
{
throw new IndexOutOfRangeException();
}
//如果链表是空的,而且索引大于0
if (IsEmpty && index > 0)
{
throw new IndexOutOfRangeException();
}
//如果索引为0,意味着向链表头部添加节点。
if (index == 0)
{
AddFirst(t);
return;
}
//要插入位置的节点
Node<T> current = head;
int i = 0;
while (true)
{
if (i == index)
{
break;
}
i++;
current = current.Next;
}
//此处非常重要,特别要注意先后次序
//当前节点的上一个的下一个设置为新节点
current.Prev.Next = node;
//新节点的上一个设置为当前节点的上一个
node.Prev = current.Prev;
//新节点的下一个设置为当前节点
node.Next = current;
//当前节点的上一个设置为新节点
current.Prev = node;
//大小加一
size++;
}
/// <summary>
/// 移除链表中的节点
/// </summary>
/// <param name="index">要移除的节点的索引</param>
public void RemoveAt(int index)
{
//链表头节点是空的
if (IsEmpty)
{
throw new Exception("链表是空的。");
}
//索引过小
if (index < 0)
{
throw new IndexOutOfRangeException();
}
//索引过大
if (index >= Count)
{
throw new IndexOutOfRangeException();
}
//如果要移除的是头节点
if (index == 0)
{
RemoveFirst();
return;
}
if (index==size-1)
{
RemoveLast();
return;
}
//要移除的节点
Node<T> current = head;
int i = 0;
while (true)
{
if (i == index)
{
break;
}
i++;
current = current.Next;
}
//当前节点的上一个的Next设置为当前节点的Next
current.Prev.Next = current.Next;
//当前节点的下一个的Prev设置为当前节点的Prev
current.Next.Prev = current.Prev;
//大小减一
size--;
}
/// <summary>
/// 移除头节点
/// </summary>
public void RemoveFirst()
{
//链表头节点是空的
if (IsEmpty)
{
throw new Exception("链表是空的。");
}
//如果size为1,那就是清空链表。
if (size==1)
{
Clear();
return;
}
//将头节点设为原头结点的下一个节点,就是下一个节点上移
head = head.Next;
//处理上一步遗留问题,原来的第二个节点的上一个是头结点,现在第二个要变成头节点,那要把它的Prev设为null才能成为头节点
head.Prev = null;
//大小减一
size--;
}
/// <summary>
/// 移除尾节点
/// </summary>
public void RemoveLast()
{
//链表头节点是空的
if (IsEmpty)
{
throw new Exception("链表是空的。");
}
//如果size为1,那就是清空链表。
if (size == 1)
{
Clear();
return;
}
//尾节点设置为倒数第二个节点
tail = tail.Prev;
//将新尾节点的Next设为null,表示它是新的尾节点
tail.Next = null;
//大小减一
size--;
}
/// <summary>
/// 判断链表是否是空的
/// </summary>
public bool IsEmpty
{
get
{
return head == null;
}
}
/// <summary>
/// 链表中元素的个数
/// </summary>
public int Count
{
get
{
////也可以采用遍历的方法获得长度,遍历可以从前向后,也可以从后向前
//int count = 0;
////取得链表头部节点
//Node<T> current = new Node<T>();
//current = head;
////遍历整个链表,直到最后一个Next为null的节点为止
//while (current!=null)
//{
// count++;
// current = current.Next;
//}
//return count;
return size;
}
}
/// <summary>
/// 清除链表中的数据
/// </summary>
public void Clear()
{
head = null;
tail = null;
size = 0;
}
/// <summary>
/// 根据索引获取链表中的节点
/// </summary>
/// <param name="index">整型索引</param>
/// <returns>节点</returns>
public Node<T> this[int index]
{
get
{
//链表头节点是空的
if (head == null)
{
throw new Exception("链表是空的。");
}
//索引过小
if (index < 0)
{
throw new IndexOutOfRangeException();
}
//索引过大
if (index >= Count)
{
throw new IndexOutOfRangeException();
}
//取得头节点
Node<T> current = new Node<T>();
//如果索引在前一半,那么从前向后找
if (index<size/2)
{
current = head;
int i = 0;
//遍历链表
while (true)
{
//找到第index个节点
if (i == index)
{
break;
}
current = current.Next;
i++;
}
return current;
}
else//如果索引在后一半,那么从后向前找
{
current = tail;
int i = size;
//遍历链表
while (true)
{
//找到第index个节点
if (i == index)
{
break;
}
current = current.Prev;
i--;
}
return current.Next;
}
}
}
}
发表评论
-
经典.net面试题目
2011-08-09 14:14 6931. 简述 private、 protected、 publi ... -
.net面试问答(大汇总)
2011-08-09 13:57 1012用.net做B/S结构的系统, ... -
C#单向链表的实现
2011-07-20 16:39 1367public class UserTbl { priv ... -
js在线编辑器其中一个上传图片功能
2011-07-13 22:04 1259img.html <script language=&q ... -
Asp.net 中如何调用母版中的变量和变量
2011-07-07 11:27 2562((test)this.master).str: test:当 ... -
C# 循环DataSet表里的数据
2011-07-05 22:22 1900cs:DataSet ds = DbSqlClient.Que ... -
C#jQuery解决传值时获取的代码问题
2011-07-05 11:34 1039传值:escape($("input[name=co ... -
C# 生成 添加 修改 删除 XML节点操作
2011-07-04 20:56 2443XmlDocument xmldoc; Xml ... -
C# 正则表达式语法定义
2011-07-04 14:02 831Regex reg = new Regex("-&q ... -
C#修饰符
2011-06-17 13:29 7611, 访问修饰符 指定声 ... -
字符串处理Utils
2011-06-17 10:16 878using System; using System.Coll ... -
asp.net性能优化的几个方面
2011-06-17 09:48 862c#(或vb.net)程序改进 ... -
StringJoiner 拯救那些性能低下的字符串拼装代码
2011-06-17 09:44 966作者:Bruce(编程的艺 ... -
HttpModule
2011-06-17 09:04 856HttpModule & HttpHandle TE& ... -
C# CS 语法
2011-06-16 14:13 819C#Md5加密16位的: System.W ... -
ASP.NET页面与IIS底层交互和工作原理详解 (二)
2011-06-15 16:12 815第三回: 引言 Http 请求 ... -
ASP.NET页面与IIS底层交互和工作原理详解(一)
2011-06-15 16:10 1035第一回: 引言 我查阅 ... -
Web.config的配置级原理
2011-06-15 15:50 754标签解释: configuration配置 Appsettin ...
相关推荐
实现双向链表,符合双向链表的思路且易懂,拓展方便。
C#双向链表的实现的源码
操作系统c++编程实现安全型双向链表,线程的创建,利用线程对链表进行增删改操作,并检验结果是否正确
C#,双向链表(Doubly Linked List)归并排序(Merge Sort)算法与源代码 1 双向链表 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一...
C#,双向链表(Doubly Linked List)快速排序(Quick Sort)算法与源代码。双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始...
该代码应用到了模板、接口、结构体等,具有较好的复用性
采用双向链表实现的AOI,可支持大量实体的范围检测,实现了进入AOI和离开AOI采用不同的半径
主要介绍了C#数据结构之双向链表(DbLinkList),结合实例形式较为详细的讲解了双向链表的概念及C#实现双向链表的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
主要介绍了C#双向链表LinkedList排序实现方法,涉及C#双向链表的定义与排序技巧,具有一定参考借鉴价值,需要的朋友可以参考下
排序 插入 删除 查找 底层代码,内有详细讲解。 初步调试成功
C#,递归方法实现双向链表(Doubly Linked List)的反转(Reverse)算法与源代码 递归算法的执行过程分递推和回归两个阶段。在递推阶段,把较复杂的问题(规模为n)的求解推到比原问题简单一些的问题(规模小于n)的...
二元查找树转变成排序的双向链表 题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。 要求不能创建任何新的结点,只调整指针的指向。 10 / \ 6 14 / \ / \ 4 8 12 16 转换成双向链表 4...
本c#程序源码包含单链表,双向链表,循环链表的完整应用。
C语言实现的双向动态链表: 该资源包含.c和.h文件,可直接使用或者编译为库函数进行使用。实现了动态双向链表的各种基本功能,e.g. 增删节点,访问某一节点等。
首先,明白什么是双向链表。所谓双向链表是如果希望找直接前驱结点和直接后继结点的时间复杂度都是 O(1),那么,需要...双向链表结点类的实现如下所示 //一个链条的类 public class DbNode<T> { //当前的数据所在priv
实现单链表的增删改查,,仅供参考,并不太完善
实现了插入和查找,包括示例,有待完善(倒序插入会出问题)
链表:单链表,双向链表,循环链表 栈,队列 二叉树应用-表达式求值 树的操作 图 二分查找 排序算法:插入排序,选择排序,冒泡排序 -全是C#,附上Viso图和一些解释