双向链表
结点Node类型定义:
/// <summary>
/// 双向链表的节点
/// </summary>
/// <typeparam name="T"></typeparam>
public class DbNode<T>
{
//私有变量
private T _data; //节点的值
private DbNode<T> _prev; //前驱节点
private DbNode<T> _next; //后继结点
//属性
/// <summary>
/// 节点的值
/// </summary>
public T Data
{
get { return this._data; }
set { this._data = value; }
}
/// <summary>
/// 前驱节点
/// </summary>
public DbNode<T> Prev
{
get { return this._prev; }
set { this._prev = value; }
}
/// <summary>
/// 后继结点
/// </summary>
public DbNode<T> Next
{
get { return this._next; }
set { this._next = value; }
}
// 构造函数 6个
public DbNode(T data, DbNode<T> prev, DbNode<T> next)//中间节点
{
this._data = data;
this._prev = prev;
this._next = next;
}
public DbNode(T data, DbNode<T> prev)//结尾结点
{
this._data = data;
this._prev = prev;
this._next = null; //结尾处的node
}
//public DbNode(T data, DbNode<T> next)//开头结点
//{
// this._data = data;
// this._prev = null; //开头处的node
// this._next = next;
//}
public DbNode(DbNode<T> next)
{
//使用 default 关键字,此关键字对于引用类型会返回空,对于数值类型会返回零。
//对于结构,此关键字将返回初始化为零或空的每个结构成员,具体取决于这些结构是值类型还是引用类型
this._data = default(T);
this._next = next;
this._prev = null;
}
public DbNode(T data)
{
this._data = data;
this._prev = null;
this._next = null;
}
public DbNode()
{
this._data = default(T);
this._prev = null;
this._next = null;
}
}
循环链表:
循环单链表
Node类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
public class Node<T>
{
private T data; //数据域
private Node<T> next; //引用域
//数据域属性
public T Data { get => data; set => data = value; }
//引用域属性
internal Node<T> Next { get => next; set => next = value; }
//构造器
public Node(T val, Node<T> p) {
Data = val;
Next = p;
}
//构造器
public Node(Node<T> p) {
Next = p;
}
//构造器
public Node(T val) {
Data = val;
}
//构造器
public Node() {
Data = default(T);
Next = null;
}
}
}
SingleCycle接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
interface SingleCycleDs<T>
{
int GetLength(); //求长度
void Clear(); //清空操作
bool IsEmpty(); //判断线性表是否为空
void Append(T item); //附加操作
void Insert(T item, int i); //插入
void Delete(int i); //删除操作
T GetElem(int i); //取表元
int Locate(T value); //按值查找
void Reverse(); //逆置
}
}
SingleCycle类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class SingleCycle<T> : SingleCycleDs<T>
{
Node<T> head; //头节点
//头节点属性
public Node<T> Head { get => head; set => head = value; }
//构造器
public SingleCycle() {
head = new Node<T>();
head.Next = head;
}
/// <summary>
/// 附加元素到尾部
/// </summary>
/// <param name="item">元素</param>
public void Append(T item)
{
Node<T> p = new Node<T>(item);
Node<T> c = head;
//头节点为空时,直接修改头节点和新增节点的引用
if (IsEmpty()) {
head.Next = p;
p.Next = head;
return;
}
//遍历至最后一个节点
while (c.Next != head) {
c = c.Next;
}
//将新增元素挂载到末尾
c.Next = p;
p.Next = head;
}
/// <summary>
/// 清空链表
/// </summary>
public void Clear()
{
head.Next = head;
}
/// <summary>
/// 删除节点
/// </summary>
/// <param name="i">删除节点的位置</param>
public void Delete(int i)
{
//链表为空
if (IsEmpty() || i < 1)
{
Console.WriteLine("链表为空或位置错误");
return;
}
//删除头节点
if (i == 1)
{
head.Next = head.Next.Next;
return;
}
//遍历位置删除节点
Node<T> c = head.Next; //目标节点
Node<T> p = new Node<T>(); //目标节点的前驱节点
int j = 1;
while (c.Next != head && j < i)
{
/* 记录当前节点,
* 因为当前节点为下一次遍历的前驱节点,
* 当遍历到目标节点时,可以直接用其前驱节点操作。
*/
p = c;
c = c.Next;
++j;
}
//遍历到位置
if (j == i)
{
Console.WriteLine(j);
Console.WriteLine(p.Next.Data);
//用前驱节点来跳过目标节点,直接挂载目标节点的后继节点到其前驱节点上
p.Next = p.Next.Next;
Console.WriteLine("删除成功");
}
//未找到位置
else {
Console.WriteLine("位置不正确");
}
}
/// <summary>
/// 根据位置获取元素值
/// </summary>
/// <param name="i">元素位置</param>
/// <returns>元素数据域值</returns>
public T GetElem(int i)
{
//链表为空时返回默认值。
if (IsEmpty()) {
Console.WriteLine();
return default(T);
}
//返回第一个元素的值
if (i == 1) {
return head.Next.Data;
}
//遍历查找并返回
Node<T> c = head.Next;
int j = 1;
while (c.Next != head && j < i) {
c = c.Next;
++j;
}
//遍历找到
if (j == i)
{
return c.Data;
}
else {
Console.WriteLine("位置不正确");
}
return default(T);
}
/// <summary>
/// 获取列表长度
/// </summary>
/// <returns></returns>
public int GetLength()
{
if (IsEmpty()) {
return 0;
}
Node<T> c = head.Next;
int i = 1;
while (c.Next != head) {
c = c.Next;
++i;
}
return i;
}
/// <summary>
/// 插入节点
/// </summary>
/// <param name="item">插入的元素值</param>
/// <param name="i">插入位置</param>
public void Insert(T item, int i)
{
if (IsEmpty() || i < 1)
{
Console.WriteLine("链表为空或位置错误");
}
//插入到首节点位置
if (i == 1) {
Node<T> c = head.Next; //保存首节点
head.Next = new Node<T>(item); //将新节点插入到头节点后
head.Next.Next = c; //将首节点设为新节点的后继
return;
}
//遍历位置插入
Node<T> c1 = head.Next;
Node<T> p = new Node<T>();
int j = 1;
while (c1.Next != head && j < i)
{
p = c1;
c1 = c1.Next;
++j;
}
//遍历找到
if (j == i)
{
p.Next = new Node<T>(item);
p.Next.Next = c1;
}
else
{
Console.WriteLine("位置不正确");
}
}
//判断链表是否为空
public bool IsEmpty()
{
return head.Next == head;
}
/// <summary>
/// 根据节点值查找位置
/// </summary>
/// <param name="value">节点值</param>
/// <returns>节点位置</returns>
public int Locate(T value)
{
int i = 1;
Node<T> c = head.Next;
/* 这里的判断条件要不能写c.Next,
* 因为当目标值为最后一个节点的值时,c.Next==head会成立
* 所以要让循环多遍历一次,
* 当c时头节点时说明列表已遍历完毕,这时如果还未找到,说明不存在。
* 注意:要将比较语句写在逻辑判断后面,
* 因为head=null,当c=head时,比较语句会抛异常。
* 所以要先比较c==head来中断后面的语句。
*/
while (c != head && !c.Data.Equals(value))
{
c = c.Next;
++i;
}
if (c == head)
{
Console.WriteLine("不存在这样的节点");
return -1;
}
else {
return i;
}
}
public void Reverse()
{
throw new NotImplementedException();
}
}
}
Program类
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
SingleCycle<string> link = new SingleCycle<string>();
//添加元素
link.Append("123");
link.Append("567");
link.Append("jqk");
link.Append("qwe");
//link.Clear();
//link.Delete(5);
//Console.WriteLine(link.GetElem(4));
//Console.WriteLine(link.GetLength());
//link.Insert("asd", 4);
Console.WriteLine(link.Locate("1231"));
//输出
Node<string> c = link.Head.Next;
while (c != link.Head)
{
Console.WriteLine(c.Data);
c = c.Next;
}
Console.Read();
}
}
}
循环双链表
节点类(DoubleLinkedListNode.cs)
public class DoubleLinkedListNode<T>
{
//前驱
private DoubleLinkedListNode<T> _nextDoubleLinkedListNode;
private DoubleLinkedListNode<T> _prevDoubleLinkedListNode;
//数据
private T dataValue;
/// <summary>
/// 无参构造函数 该节点只有默认值,前驱和后继都 ——>null
/// </summary>
public DoubleLinkedListNode()
{
this.dataValue = default(T);
this._nextDoubleLinkedListNode = null;
this._prevDoubleLinkedListNode = null;
}
/// <summary>
/// 构造方法:实例化该节点只有传入的data域,前驱和后继都指向null
/// </summary>
/// <param name="value"></param>
public DoubleLinkedListNode(T value)
{
this._prevDoubleLinkedListNode = null;
this.dataValue = value;
this._nextDoubleLinkedListNode = null;
}
/// <summary>
/// 构造函数:实例化一个正常的Node 数据域 前驱后继都有值
/// </summary>
/// <param name="value"></param>
/// <param name="prev"></param>
/// <param name="next"></param>
public DoubleLinkedListNode(T value, DoubleLinkedListNode<T> prev, DoubleLinkedListNode<T> next)
{
this._prevDoubleLinkedListNode = prev;
this.dataValue = value;
this._nextDoubleLinkedListNode = next;
}
public DoubleLinkedListNode<T> PrevDoubleLinkedListNode
{
get { return this._prevDoubleLinkedListNode; }
set { this._prevDoubleLinkedListNode = value; }
}
public T DataValue
{
get { return this.dataValue; }
set { this.dataValue = value; }
}
public DoubleLinkedListNode<T> NextDoubleLinkedListNode
{
get { return this._nextDoubleLinkedListNode; }
set { this._nextDoubleLinkedListNode = value; }
}
/// <summary>
/// Show 方法,调试用
/// </summary>
/// <returns></returns>
public override string ToString()
{
T p = this._prevDoubleLinkedListNode == null ? default(T) : this._prevDoubleLinkedListNode.dataValue;
T n = this._nextDoubleLinkedListNode == null ? default(T) : this._nextDoubleLinkedListNode.dataValue;
string s = string.Format("Data:{0},Prev:{1},Next:{2}", this.dataValue, p, n);
return s;
}
}
接口(ILinkList.cs)
internal interface ILinkedList<T>
{
/// <summary>
/// 头元素
/// </summary>
DoubleLinkedListNode<T> HeadDoubleLinkedListNode { get; }
/// <summary>
/// 尾元素
/// </summary>
DoubleLinkedListNode<T> EndDoubleLinkedListNode { 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>
/// 移除头结点
/// </summary>
void RemoveHeadNode();
/// <summary>
/// 移除尾节点
/// </summary>
void RemoveEndNode();
/// <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);
}
**双向循环链表(DoubleCircularLinkList.cs)
**
public class DoubleCircularLinkedList<T> : IEnumerable<T>
{
private DoubleLinkedListNode<T> _headNode;
private DoubleLinkedListNode<T> _nextNode;
private DoubleLinkedListNode<T> current;
private int currentIndex;
public DoubleLinkedListNode<T> HeadNode { get; set; }
public DoubleLinkedListNode<T> NextNode { get; set; }
public int Count { get; private set; }
/// <summary>
/// 根据索引获取链表中的节点
/// </summary>
/// <param name="index">整型索引</param>
/// <returns>节点</returns>
public DoubleLinkedListNode<T> this[int index]
{
get
{
if (index < 0 || index >= this.Count)
{
throw new Exception("索引值非法,无法得对象节点.");
}
DoubleLinkedListNode<T> reNode = this._headNode;
int i = 0;
while (reNode != null)
{
if (i == index)
{
break;
}
reNode = reNode.NextDoubleLinkedListNode;
++i;
}
return reNode;
}
}
/// <summary>
/// 判断链表是否是空的
/// </summary>
public bool IsEmpty
{
get { return this._headNode == null; }
}
public void AddAfter(DoubleLinkedListNode<T> node, DoubleLinkedListNode<T> newNode)
{
this.AddAfter(node, newNode.DataValue);
}
public void AddAfter(DoubleLinkedListNode<T> node, T t)
{
if (node == null || t == null)
{
throw new Exception("元素为空,不可以进行插入");
}
int index = this.IndexOf(node.DataValue);
if (index != -1)
{
DoubleLinkedListNode<T> currNode = this.GetNodeByIndex(index);
DoubleLinkedListNode<T> upNode = currNode.PrevDoubleLinkedListNode;
DoubleLinkedListNode<T> nextNode = currNode.NextDoubleLinkedListNode;
var newNode = new DoubleLinkedListNode<T>(t);
if (index == 0)
{
this._headNode.NextDoubleLinkedListNode = newNode;
newNode.PrevDoubleLinkedListNode = this._headNode;
newNode.NextDoubleLinkedListNode = nextNode;
}
else if (index == this.Count - 1)
{
newNode.PrevDoubleLinkedListNode = this._nextNode;
this._nextNode.NextDoubleLinkedListNode = newNode;
this._nextNode = newNode;
}
else
{
nextNode.PrevDoubleLinkedListNode = newNode;
currNode.NextDoubleLinkedListNode = newNode;
newNode.PrevDoubleLinkedListNode = currNode;
newNode.NextDoubleLinkedListNode = nextNode;
}
}
}
public void AddBefore(DoubleLinkedListNode<T> node, DoubleLinkedListNode<T> newNode)
{
this.AddBefore(node, newNode.DataValue);
}
public void AddBefore(DoubleLinkedListNode<T> node, T t)
{
if (node == null || t == null)
{
throw new Exception("元素为空,不可以进行插入");
}
int index = this.IndexOf(node.DataValue);
if (index != -1)
{
DoubleLinkedListNode<T> currNode = this.GetNodeByIndex(index);
DoubleLinkedListNode<T> upNode = currNode.PrevDoubleLinkedListNode;
DoubleLinkedListNode<T> nextNode = currNode.NextDoubleLinkedListNode;
var newNode = new DoubleLinkedListNode<T>(t);
if (index == 0)
{
this._headNode.PrevDoubleLinkedListNode = newNode;
newNode.NextDoubleLinkedListNode = this._headNode;
this._headNode = newNode;
}
else if (index == this.Count - 1)
{
upNode.NextDoubleLinkedListNode = newNode;
newNode.NextDoubleLinkedListNode = this._nextNode;
this._nextNode.PrevDoubleLinkedListNode = newNode;
}
else
{
upNode.NextDoubleLinkedListNode = newNode;
nextNode.PrevDoubleLinkedListNode = newNode;
currNode.PrevDoubleLinkedListNode = upNode;
currNode.NextDoubleLinkedListNode = nextNode;
}
}
}
/// <summary>
/// 添加节点到链表的开头
/// </summary>
/// <param name="t"></param>
public DoubleLinkedListNode<T> AddFirst(T value)
{
var doubleLinkedListNode = new DoubleLinkedListNode<T>(value);
//如果头为null
if (this._headNode == null)
{
//把头节点设置为node
this._headNode = doubleLinkedListNode;
//因为是空链表,所以头尾一致
this._headNode = doubleLinkedListNode;
//大小加一
this.Count ++;
return null;
}
//原来头节点的上一个为新节点
this._headNode.PrevDoubleLinkedListNode = doubleLinkedListNode;
//新节点的下一个为原来的头节点
doubleLinkedListNode.NextDoubleLinkedListNode = this._headNode;
//新头节点为新节点
this._headNode = doubleLinkedListNode;
//大小加一
this.Count++;
return this._headNode;
}
public void AddFirst(DoubleLinkedListNode<T> node)
{
this.AddFirst(node.DataValue);
}
/// <summary>
/// 添加节点到链表的末尾
/// </summary>
/// <param name="t">要添加的数据</param>
public DoubleLinkedListNode<T> AddLast(T t)
{
var doubleLinkedListNode = new DoubleLinkedListNode<T>(t);
//当前Node
this.current = doubleLinkedListNode;
//如果头为null Count =0
if (this._headNode == null)
{
//把头节点设置为当前node
this._headNode = this.current;
//循环双向链表头尾相连 前驱和后继都为当前node
this._headNode.PrevDoubleLinkedListNode = this.current;
this._headNode.NextDoubleLinkedListNode = this.current;
//因为是空链表,所以头尾一致
//大小加一
this.Count++;
return this.current;
}
//只包含一个头结点 Count=1
if (this.Count == 1)
{
for (int index = 0; index < this.Count; index++)
{
//判定检索原有链表中的尾节点 尾节点必定指向首节点
if (this[index].NextDoubleLinkedListNode == this._headNode)
{
//原尾节点的后置指向当前插入节点
this[index].NextDoubleLinkedListNode = this.current;
this[index].PrevDoubleLinkedListNode = this.current;
this.current.PrevDoubleLinkedListNode = this[index];
this.current.NextDoubleLinkedListNode = this[index];
this.Count++;
return this.current;
}
}
}
//两个节点以上 Count>=2
if (this.Count >= 2)
{
for (int index = 0; index < this.Count; index++)
{
//判定检索原有链表中的尾节点 尾节点必定指向首节点
if (this[index].NextDoubleLinkedListNode == this._headNode)
{
//原尾节点的后置指向当前插入节点
this._headNode.PrevDoubleLinkedListNode = this.current;
this[index].NextDoubleLinkedListNode = this.current;
this.current.PrevDoubleLinkedListNode = this[index];
this.current.NextDoubleLinkedListNode = this._headNode;
this.Count++;
return this.current;
}
}
}
return this.current;
}
public void AddLast(DoubleLinkedListNode<T> node)
{
this.AddLast(node.DataValue);
}
/// <summary>
/// 移除链表中的节点
/// </summary>
/// <param name="index">要移除的节点的索引</param>
public bool Remove(DoubleLinkedListNode<T> node)
{
if (node == null)
{
throw new Exception("节点不可以为空,无法进行删除");
}
int index = this.IndexOf(node.DataValue);
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._headNode;
int i = 0;
while (node != null)
{
if (i == index)
{
DoubleLinkedListNode<T> upNode = node.PrevDoubleLinkedListNode;
DoubleLinkedListNode<T> nextNode = node.NextDoubleLinkedListNode;
if (index == 0)
{
this._headNode = node.NextDoubleLinkedListNode;
node = null;
}
else if (index == this.Count - 1)
{
this._nextNode = upNode;
this._nextNode.NextDoubleLinkedListNode = null;
node = null;
}
else
{
upNode.NextDoubleLinkedListNode = nextNode;
nextNode.PrevDoubleLinkedListNode = upNode;
node = null;
}
flag = true;
break;
}
node = node.NextDoubleLinkedListNode;
++i;
}
return flag;
}
/// <summary>
/// 移除头节点
/// </summary>
public void RemoveHeadNode()
{
//链表头节点是空的
if (this.IsEmpty)
{
throw new Exception("链表是空的。");
}
//如果size为1,那就是清空链表。
if (this.Count == 1)
{
this.Clear();
return;
}
//将头节点设为原头结点的下一个节点,就是下一个节点上移
this._headNode = this._headNode.NextDoubleLinkedListNode;
//处理上一步遗留问题,原来的第二个节点的上一个是头结点,现在第二个要变成头节点,那要把它的Prev设为null才能成为头节点
this._headNode.PrevDoubleLinkedListNode = null;
//大小减一
this.Count--;
}
/// <summary>
/// 移除尾节点
/// </summary>
public void RemoveEndNode()
{
//链表头节点是空的
if (this.IsEmpty)
{
throw new Exception("链表是空的。");
}
//如果size为1,那就是清空链表。
if (this.Count == 1)
{
this.Clear();
return;
}
//尾节点设置为倒数第二个节点
this._nextNode = this._headNode.PrevDoubleLinkedListNode;
//将新尾节点的Next设为null,表示它是新的尾节点
this._headNode.NextDoubleLinkedListNode = null;
//大小减一
this.Count--;
}
public DoubleLinkedListNode<T> Find(T value)
{
throw new NotImplementedException();
}
public DoubleLinkedListNode<T> FindLast(T value)
{
throw new NotImplementedException();
}
public int IndexOf(T item)
{
if (item == null || this.Count == 0 || this._headNode == null)
{
throw new Exception("无法得到元素索引");
}
int reInt = -1;
int i = 0;
DoubleLinkedListNode<T> node = this._headNode;
while (node != null)
{
if (node.DataValue.Equals(item))
{
reInt = i;
break;
}
++i;
node = node.NextDoubleLinkedListNode;
}
return reInt;
}
public T GetElementByIndex(int index)
{
if (index < 0 || index >= this.Count)
{
throw new Exception("索引值非法,无法得对象.");
}
DoubleLinkedListNode<T> reNode = this._headNode;
int i = 0;
while (reNode != null)
{
if (i == index)
{
break;
}
reNode = reNode.NextDoubleLinkedListNode;
++i;
}
return reNode.DataValue;
}
public DoubleLinkedListNode<T> GetNodeByIndex(int index)
{
if (index < 0 || index >= this.Count)
{
throw new Exception("索引值非法,无法得对象节点.");
}
DoubleLinkedListNode<T> reNode = this._headNode;
int i = 0;
while (reNode != null)
{
if (i == index)
{
break;
}
reNode = reNode.NextDoubleLinkedListNode;
++i;
}
return reNode;
}
/// <summary>
/// 清除链表
/// </summary>
public void Clear()
{
this.Count = 0;
this.HeadNode = null;
this.NextNode = null;
}
private void Reset()
{
this.currentIndex = 0;
this.current = this.HeadNode;
}
public void ShowtheLinkList()
{
var p = new DoubleLinkedListNode<T>();
p = this._headNode;
int index = 0;
int index2 = 0;
while (p != null)
{
string prev = " ";
string data = " ";
string next = " ";
if (p.PrevDoubleLinkedListNode == null)
{
prev = "null";
}
else
{
prev = Convert.ToString((p.PrevDoubleLinkedListNode.DataValue as PersonModel).id);
}
if (p.DataValue == null)
{
data = "null";
}
else
{
data = Convert.ToString((p.DataValue as PersonModel).id);
}
if (p.NextDoubleLinkedListNode == null)
{
next = "null";
}
else
{
next = Convert.ToString((p.NextDoubleLinkedListNode.DataValue as PersonModel).id);
}
Console.Write("前驱为:{0},数据为:{1},后继为:{2},索引为:{3}\n", prev, data, next, index);
p = p.NextDoubleLinkedListNode;
if (index == this.Count - 1)
{
Console.Write("一个轮回");
Console.ReadKey();
}
++index;
}
}
#region IEnumerable<T> 成员
public IEnumerator<T> GetEnumerator()
{
DoubleLinkedListNode<T> currNode = this._headNode;
while (currNode != null)
{
yield return currNode.DataValue;
currNode = currNode.NextDoubleLinkedListNode;
}
}
#endregion
#region IEnumerable 成员
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}