C#实现的简单链表类实例
本文实例讲述了C#实现的简单链表类。分享给大家供大家参考。具体如下:
一、关于C#链表
C#链表可用类LinkedList来存放。本文中的类MyLinkedList只是实现了该类的最基本的功能。C#中没有指针,但因为C#中类在赋值时传递的是地址,因此仍然可以利用这点制作一个链表。
二、结点类Node和链表类MyLinkedList代码
/// <summary> /// 链表结点 /// </summary> class Node { //结点数据,前后结点 public object Data; public Node PreviousNode; public Node NextNode; //构造函数 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; } //结点总数 public int Count; //构造函数 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 { //如果数据部分匹配,则删去该结点 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函数的调用示例
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#程序设计有所帮助。
赞 (0)