1.

Node.cs
namespace 鏈表 { public class Node<T> { public T Data; //這個就是地址
public Node<T> Next; //構造函數用來初始化
public Node() { Data = default(T); Next = null; } public Node(T value) { Data = value; Next = null; } } }
2.LinkList.cs
namespace 鏈表 { //一般鏈表都是有頭部節點的,簡稱頭結點,頭結點不參與運算
public class LinkList<T> { private Node<T> _head; private int _count; public LinkList() { _head = new Node<T>(); _count = 0; } public void AddItem(Node<T> newNode) { Node<T> tmpNode = _head; //找到頭結點
while (tmpNode.Next != null) //循環找到最后節點
{ tmpNode = tmpNode.Next; //一直下移
} tmpNode.Next = newNode; //將最后節點和即將插入的節點連接
_count++; } public int GetLength() { return _count; } public void Insert(int index, Node<T> newNode) //插
{ if(index<0||index>_count) { Console.WriteLine("Over"); return; } Node<T> tmpNode = _head; for (int i = 0; i < index; i++) { tmpNode = tmpNode.Next; } //tmpNode (index的前一個節點)
newNode.Next = tmpNode.Next; tmpNode.Next = newNode; _count++; } //第一個為index,第二個為value
public void ShowItem(Action<int,T> ac) { if (_count ==0) { Console.WriteLine("空"); return; } Node<T> tmpNode = _head.Next; for (int i = 0; i < _count; i++) { ac(i, tmpNode.Data); tmpNode = tmpNode.Next; } } public T RemoveAt(int index) { T returnValue = default(T); // 定義一個data的返回值
if (index < 0 || index >= _count)//判斷是否出界
{ Console.WriteLine("error"); goto returnTip; } Node<T> tmpNode = _head; //刪除節點的前一個節點
for (int i = 0; i < index; i++)//循環走
{ tmpNode = tmpNode.Next; } Node<T> deleteNode = tmpNode.Next; //要刪除的節點
tmpNode.Next = tmpNode.Next.Next;//牽手刪除節點的后一個節點
deleteNode.Next = null;//不讓其連接
_count--; returnValue = deleteNode.Data;//返回刪除節點的數據data
returnTip: return returnValue; } public void Reverse() //鏈表反轉
{ if (_count < 1) { Console.WriteLine("鏈表長度不足"); return; } Node<T> x1, x2; x2 = _head.Next; _head.Next = null; while (x2 != null) { x1 = x2.Next; x2.Next = _head.Next; _head.Next = x2; x2 = x1; } } public T RemoveMinDemo(Func<Node<T>,Node<T>,Boolean> _func) //刪除最小值
{ Node<T> deletePreMin, deleteMin, PreMin, Min; deletePreMin = PreMin = _head; deleteMin = Min = _head.Next; while (Min != null) { if (_func(Min,deleteMin)) { deletePreMin = PreMin; deleteMin = Min; } PreMin = PreMin.Next; Min = Min.Next; } deletePreMin.Next = deletePreMin.Next.Next; deleteMin.Next = null; _count--; return deleteMin.Data; } public void Clear() { _head.Next = null; _count = 0; } } }
3.Program.cs
namespace 鏈表 { class Program { public static bool SH(Node<int> a, Node<int> b) { if (a.Data > b.Data) { return false; } return true; } public static void Show(int index, int value) { Console.WriteLine("第{0}個元素是{1}",index+1,value); } static void Main(string[] args) { LinkList<int> linklist = new LinkList<int>(); linklist.AddItem(new Node<int>(9)); linklist.AddItem(new Node<int>(3)); linklist.AddItem(new Node<int>(10)); linklist.AddItem(new Node<int>(4)); linklist.AddItem(new Node<int>(21)); linklist.AddItem(new Node<int>(100)); linklist.Reverse();//反轉 //9 3 10 4 21 100
linklist.RemoveMinDemo(SH); //刪除最小值
linklist.ShowItem(Show); Console.WriteLine(linklist.GetLength()); Console.ReadLine(); } } }
輸出結果:
5為鏈表的長度

