/// <summary>
/// 节点类
/// </summary>
public class Node<T>
{
private T _data;
private Node<T> _next;
public T Data
{
get { return _data; }
set { _data = value; }
}
public Node<T> Next
{
get { return _next; }
set { _next = value; }
}
public Node()
{
this._data = default(T);
this._next = null;
}
public Node(T data)
{
this._data = data;
this._next = null;
}
}
/// <summary>
/// 单链表
/// </summary>
public class SingleLinked<T>
{
private Node<T> _head;
private int _count;
public Node<T> Head
{
get { return _head; }
}
public int Count
{
get { return _count; }
}
public SingleLinked()
{
_head = null;
}
/// <summary>
/// 清空
/// </summary>
public void Clear()
{
_head = null;
_count=0;
}
/// <summary>
/// 判断链表是否为空
/// </summary>
/// <returns></returns>
public bool IsEmpty()
{
if (_head == null)
{
return true;
}
return false;
}
/// <summary>
/// 添加元素
/// </summary>
/// <param name="data"></param>
public void Add(T data)
{
Node<T> newNode = new Node<T>(data);//将数据放入节点
if (_head == null)
{
_head = newNode;
_count++;
return;
}
Node<T> p = new Node<T>();
p = _head;
while (p.Next != null)
{
p = p.Next;
}
p.Next = newNode;
_count++;
}
/// <summary>
/// 移除指定的节点
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T RemoveAt(int index)
{
if (IsEmpty() || index > _count - 1 || index < 0)
{
Console.WriteLine("SingleLinked is empty or index is error!");
throw new Exception("SingleLinked is empty or index is error!");
}
Node<T> p = new Node<T>();
p = _head;
if (index == 0) //删除头节点
{
_head = p.Next;
_count--;
return p.Data;
}
int i = 0;
Node<T> frontNode = new Node<T>();//要删除节点的前一个节点
while (p.Next != null)
{
frontNode = p;
p = p.Next;
if (index == ++i)
{
break;
}
}
frontNode.Next = p.Next;
_count--;
return p.Data;
}
/// <summary>
/// 在指定位置插入节点(前插)
/// </summary>
/// <param name="data"></param>
/// <param name="index"></param>
/// <returns></returns>
public bool Insert(int index, T data)
{
if (IsEmpty() || index > _count - 1 || index < 0)
{
Console.WriteLine("SingleLinked is empty or index is error!");
throw new Exception("SingleLinked is empty or index is error!");
}
Node<T> newNode = new Node<T>(data);
Node<T> p = _head;
if (index == 0)
{
_head = newNode;
_head.Next = p;
_count++;
return true;
}
Node<T> frontNode = new Node<T>();
int i = 0;
while (p.Next != null)
{
frontNode = p;
p = p.Next;
if (index == ++i)
{
break;
}
}
frontNode.Next = newNode;
newNode.Next = p;
_count++;
return true;
}
/// <summary>
/// 获取指定节点的元素
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T GetData(int index)
{
if (IsEmpty() || index > _count - 1 || index < 0)
{
Console.WriteLine("SingleLinked is empty or index is error!");
throw new Exception("SingleLinked is empty or index is error!");
}
Node<T> p = new Node<T>();
p = _head;
if (index == 0)
{
return p.Data;
}
int i = 0;
while (p.Next != null)
{
p = p.Next;
if (index == ++i)
{
break;
}
}
return p.Data;
}
/// <summary>
/// 判断节点时候包含指定元素
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public bool Contains(T data)
{
if (IsEmpty()) return false;
if (_head.Data.Equals(data)) return true;
Node<T> p = _head;
while (p.Next != null)
{
p = p.Next;
if (p.Data.Equals(data))
{
return true;
}
}
return false;
}
/// <summary>
/// 获取与元指定素匹配的最后一个元素的索引
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public int LastIndexOf(T data)
{
if (IsEmpty()) return -1;
Node<T> p = _head;
int index = -1;
int lastIndex = -1;
while (p != null)
{
index++;
if (p.Data.Equals(data))
{
lastIndex = index;
}
p = p.Next;
}
return lastIndex;
}
/// <summary>
/// 获取与元指定素匹配的第一个元素的索引
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public int FirstIndexOf(T data)
{
if (IsEmpty()) return -1;
Node<T> p = _head;
int index = -1;
while (p!=null)
{
index++;
if (p.Data.Equals(data))
{
return index;
}
p = p.Next;
}
return index;
}
/// <summary>
/// 显示链表节点
/// </summary>
public void Display()
{
Node<T> p = new Node<T>();
p = _head;
while (p != null)
{
Console.WriteLine(p.Data);
p = p.Next;
}
}
/// <summary>
/// 逆置
/// </summary>
public void Reverse()
{
//头插法:将原链表的头节点的下一个节点插入到当前头接点的前面
Node<T> oriHead = _head;//记录原始头节点
Node<T> tempCurHead = _head;//当前临时头节点
Node<T> nextNode=new Node<T>();//原始节点的下一个节点
//将原始头节点的下一个节点设置为当前的头节点
while (oriHead.Next != null)
{
nextNode = oriHead.Next;//记录原始节点的下一个节点
oriHead.Next = nextNode.Next;//原始节点的下一个节点指向下下个节点
nextNode.Next = tempCurHead;//原始节点的下一个节点指向当前头节点
tempCurHead = nextNode;//原始节点的下一个节点设置为临时头节点
}
_head = tempCurHead;
}
}
C# 单链表的实现
最新推荐文章于 2021-09-30 10:54:41 发布