【C#資料結構-從零開始】單鏈表
阿新 • • 發佈:2018-11-01
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp8 { class SNode<T> //結點,單鏈表 { private T data; //資料域 private SNode<T> next; //位置域 public SNode(T val,SNode<T> p) //普通結點 { data = val; next = p; } public SNode(SNode<T> p) //頭結點 { next = p; } public SNode(T val) //尾結點 { data = val; next = null; } public SNode() { } public T Data { get { return data; } set { data = value; } } public SNode<T> Next { get { return next; } set { next = value; } } } class SLinkList<T>:SNode<T> { private SNode<T> start; //頭引用 int length; //單鏈表的長度 //初始化單鏈表 public SLinkList() { start = null; } //在單鏈表的末尾追加資料元素 public void InsertNode(T a) { if (start == null) { start = new SNode<T>(a); length++; return; } SNode<T> current = start; while (current.Next != null) { current=current.Next; } current.Next = new SNode<T>(a); length++; } //在單鏈表的第i個數據元素的位置插入一個數據元素 public void InsertNode(T a,int i) { SNode<T> current; //當前結點 SNode<T> previous; //上一個結點 if (i < 1 || i > length + 1) { Console.WriteLine("Position is error!"); return; } SNode<T> newnode = new SNode<T>(a); //在空連結串列或第一個元素前插入第一個元素 if (i == 1) { newnode.Next = start; start = newnode; length++; return; } //單鏈表的兩個元素之間插入一個元素 current = start; previous = null; int j = 1; while (current!=null&&j<i) { previous = current; current = current.Next; j++; } if (j == i) { newnode.Next = current; previous.Next = newnode; length++; } } //刪除單鏈表的第i個數據元素 public void DeleteNode(int i) { if (IsEmpty() || i < 1) { Console.WriteLine("Link is empty or Position is error!"); } SNode<T> current = start; if (i == 1) { start = current.Next; length--; return; } SNode<T> previous = null; int j = 1; while (current.Next != null && j < i) { previous = current; current = current.Next; j++; } if (j == i) { previous.Next = current.Next; current = null; length--; } else { Console.WriteLine("The node is not exist!"); } } //獲得單鏈表的第i個數據元素 public T SearchNode(int i) { if (IsEmpty()) { Console.WriteLine("List is empty!"); return default(T); } SNode<T> current = start; int j = 1; while (current.Next != null && j < i) { current = current.Next; j++; } if (j == i) { return current.Data; } else { Console.WriteLine("The node is not exist!"); return default(T); } } //在單鏈表中查詢值為value的資料元素 public T SearchNode(T value) { if (IsEmpty()) { Console.WriteLine("List is Empty!"); return default(T); } SNode<T> current = start; while (!current.Data.ToString().Contains(value.ToString()) && current != null) { current = current.Next; } if (current != null) { return current.Data; } else { return default(T); } } //求單鏈表的長度 public int GetLength() { return length; } //清空單鏈表 public void Clear() { start = null; } //判斷單鏈表是否為空 public bool IsEmpty() { if (start == null) { return true; } else { return false; } } //列印連結串列元素 public void print_f() { //Console.Clear(); if (IsEmpty()) { Console.WriteLine("List is empty!"); return; } SNode<T> current = start; if (start == null) { Console.WriteLine("List is empty!"); return; } while (current.Next != null) { Console.WriteLine(current.Data); current = current.Next; } Console.WriteLine(current.Data); Console.WriteLine("------------------------------"); } } class Program { static void Main(string[] args) { SLinkList<string> str = new SLinkList<string>(); str.InsertNode("a"); //末尾追加元素 str.InsertNode("b"); //末尾追加元素 str.InsertNode("c"); //末尾追加元素 str.print_f(); //列印單鏈表 str.InsertNode("1",1); //在位置1插入元素1 str.InsertNode("3", 3); //在位置1插入元素3 str.print_f(); //列印單鏈表 str.DeleteNode(2); //刪除第二個元素 str.print_f(); //列印單鏈表 Console.WriteLine("單鏈表的第3個元素:"+str.SearchNode(3)); Console.WriteLine("單鏈表的長度為:"+str.GetLength()); str.print_f(); //列印單鏈表 str.Clear(); //清空連結串列 str.print_f(); //列印單鏈表 Console.ReadKey(); } } }