C#實現(xiàn)的簡單鏈表類實例
本文實例講述了C#實現(xiàn)的簡單鏈表類。分享給大家供大家參考。具體如下:
一、關于C#鏈表
C#鏈表可用類LinkedList來存放。本文中的類MyLinkedList只是實現(xiàn)了該類的最基本的功能。C#中沒有指針,但因為C#中類在賦值時傳遞的是地址,因此仍然可以利用這點制作一個鏈表。
二、結點類Node和鏈表類MyLinkedList代碼
/// <summary> /// 鏈表結點 /// </summary> class Node { //結點數(shù)據(jù),前后結點 public object Data; public Node PreviousNode; public Node NextNode; //構造函數(shù) public Node(object data = null) { Data = data; PreviousNode = null; NextNode = null; } //輸出結點信息 public override string ToString() { return Data.ToString(); } } /// <summary> /// 鏈表類 /// </summary> class MyLinkedList { //首結點、尾結點 public Node First; public Node Last; //下一個結點、上一個結點 public Node NextNode(Node n) { return n.NextNode; } public Node PreviousNode(Node n) { return n.PreviousNode; } //結點總數(shù) public int Count; //構造函數(shù) public MyLinkedList() { this.First = null; this.Last = null; Count = 0; } /// <summary> /// 在結點node1之后增加結點node2,如果沒有該結點則在最后增加 /// </summary> /// <param name="node1">結點1</param> /// <param name="node2">結點2</param> public void AddAfter(Node node1, Node node2) { //鏈表為空的情況 if (First == null) { Console.WriteLine("Linked-list is null! Can not find node1(" + node1 + ")"); return; } Node temp = First; do { if (temp.Data.Equals(node1.Data)) { //如果node1是尾結點 if (node1.NextNode == null) { node2.NextNode = null; node2.PreviousNode = node1; node1.NextNode = node2; } else //如果node1不是尾結點 { node2.NextNode = node1.NextNode; node2.PreviousNode = node1; node2.NextNode.PreviousNode = node2; node1.NextNode = node2; ; } Count++; Console.WriteLine("Node(" + node2 + "): Add Complete!"); return; } temp = temp.NextNode; } while (temp != null); Console.WriteLine("Can not find node(" + node1 + "), Misson defeat"); } /// <summary> /// 在鏈表尾部增加結點 /// </summary> public void AddLast(Node node) { //鏈表為空的情況 if (this.First == null) { node.NextNode = null; node.PreviousNode = null; this.First = node; this.Last = node; } else //鏈表不為空的情況 { Node temp = First; while(temp.NextNode != null) { temp = temp.NextNode; } temp.NextNode = node; node.PreviousNode = temp; Last = node; } Count++; Console.WriteLine("Node(" + node + "): Add Complete!"); } /// <summary> /// 刪除指定結點 /// </summary> /// <param name="node">被刪除結點</param> public void Delete(Node node) { if (Count == 0) { Console.WriteLine("Can not find node(" + node + ")"); return; } Node temp = First; do { //如果數(shù)據(jù)部分匹配,則刪去該結點 if (temp.Data.Equals(node.Data)) { //temp是尾結點 if (temp.NextNode == null) { temp.PreviousNode.NextNode = null; temp = null; } else //temp不是尾結點 { temp.PreviousNode.NextNode = temp.NextNode; temp.NextNode.PreviousNode = temp.PreviousNode; temp = null; } Count--; Console.WriteLine("Node(" + node + "): Delete Complete!"); return; } temp = temp.NextNode; } while (temp != null); Console.WriteLine("Can not find node(" + node + "), Misson defeat"); } /// <summary> /// 修改結點值 /// </summary> /// <param name="node">被修改結點</param> /// <param name="value">結點值</param> public void Modify(Node node, object value) { if (Count == 0) { Console.WriteLine("Can not find node(" + node + ")"); return; } Node temp = First; do { if (temp.Data.Equals(node.Data)) { Console.WriteLine("Node: " + temp.Data + " → " + value.ToString()); temp.Data = value; return; } temp = temp.NextNode; } while (temp != null); } /// <summary> /// 打印鏈表 /// </summary> public void Print() { if (First == null) { Console.WriteLine("No nodes in this linked-list."); return; } else { Console.WriteLine("Print the linked-list..."); Node temp = First; do { Console.WriteLine(temp.ToString()); temp = temp.NextNode; } while (temp != null); Console.WriteLine("Mission Complete!"); } } }
三、Main函數(shù)的調用示例
static void Main(string[] args) { MyLinkedList ll = new MyLinkedList(); //添加三個結點 1 2(在1后) 3(在2后) Node n1 = new Node("node1"); Node n2 = new Node("node2"); Node n3 = new Node("node3"); ll.AddLast(n1); ll.AddLast(n2); ll.AddLast(n3); //添加三個結點 1.5(在1后) 2.5(在2后) 3.5(在3后) Node n1dot5 = new Node("node1dot5"); Node n2dot5 = new Node("node2dot5"); Node n3dot5 = new Node("node3dot5"); ll.AddAfter(n1, n1dot5); ll.AddAfter(n2, n2dot5); ll.AddAfter(n3, n3dot5); Console.WriteLine("========================"); //打印鏈表 ll.Print(); Console.WriteLine("========================"); //刪除結點 2 和 3,將結點 2.5 的值改為 "ThisNodeIsModified!" ll.Delete(n2); ll.Delete(n3); ll.Modify(n2dot5, "ThisNodeIsModified!"); Console.WriteLine("========================"); //打印鏈表 ll.Print(); Console.ReadLine(); }
四、運行結果
希望本文所述對大家的C#程序設計有所幫助。
相關文章
探討C#中Dispose方法與Close方法的區(qū)別詳解
本篇文章是對C#中Dispose方法與Close方法的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06C#執(zhí)行存儲過程并將結果填充到GridView的方法
這篇文章主要介紹了C#執(zhí)行存儲過程并將結果填充到GridView的方法,結合實例形式分析了C#存儲過程操作及GridView控件相關操作技巧,需要的朋友可以參考下2017-02-02C#通過IComparable實現(xiàn)ListT.sort()排序
這篇文章主要介紹了C#通過IComparable實現(xiàn)ListT.sort()排序的方法,可實現(xiàn)自定義的排序方法,是非常實用的技巧,需要的朋友可以參考下2014-09-09C#實現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
這篇文章主要介紹了C#實現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法,涉及C#操作DataSet及ListView控件的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10C#?webApi創(chuàng)建與發(fā)布、部署、api調用詳細教程
這篇文章主要給大家介紹了關于C#?webApi創(chuàng)建與發(fā)布、部署、api調用的相關資料,WebApi是微軟在VS2012?MVC4版本中綁定發(fā)行的,WebApi是完全基于Restful標準的框架,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-12-12