1.程序的大致結構如下圖:
2.下面依次列出各個類的代碼
①ILISTDs.cs 這是一個接口類,列出單鏈表的方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 單鏈表 { public interface IListDs<T> { int GetLength();//求長度 void Clear();//清空操作 bool IsEmpty();//判斷線性表是否為空 void Append(T item);//附加操作 void Insert(T item,int i);//插入操作 T Delete(int i);//刪除操作 T GetElem(int i);//取表元 int Locate(T value);//按值查找 } }
②LinkList.cs 單鏈表的實現類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 單鏈表 { public class LinkList<T> : IListDs<T> { private Node<T> head;//單鏈表的頭引用 //頭引用的屬性 public Node<T> Head { get { return head; } set { head = value; } } //構造器 public LinkList() { head = null; } //求單鏈表的長度 public int GetLength() { Node<T> p = head; int len = 0; while (p != null) { p = p.Next; len++; } return len; } //清空單鏈表 public void Clear() { head = null; } //判斷是否為空 public bool IsEmpty() { return head==null;
} //在單鏈表的末尾添加新元素 public void Append(T item) { Node<T> q = new Node<T>(item); Node<T> p = new Node<T>(); if (head == null) { head = q; return; } p = head; while (p.Next != null) { p = p.Next; } p.Next = q; } //在單鏈表第i個位置前面插入一個值為item的節點 public void Insert(T item, int i) { if (IsEmpty() || i < 1) { Console.WriteLine("鏈表為空或者位置錯誤"); return; } if (i == 1) { Node<T> q = new Node<T>(item); q.Next = head; head = q; return; } Node<T> p = head; Node<T> r = new Node<T>(); int j = 1; while (p.Next != null && j < i) { r = p; p = p.Next; j++; } if (j == i) { Node<T> q = new Node<T>(item); Node<T> m = r.Next; r.Next = q; q.Next = m; } } //在單鏈表第i個位置后面插入一個值為item的節點 public void InsertPost(T item, int i) { if (IsEmpty() || i < 1) { Console.WriteLine("鏈表為空或者位置錯誤"); return; } if (i == 1) { Node<T> q = new Node<T>(item); q.Next = head.Next; head.Next = q; return; } Node<T> p = head; Node<T> r = new Node<T>(); int j = 1; while (p.Next != null && j <= i) { r = p; p = p.Next; j++; } if (j == i+1) { Node<T> q = new Node<T>(item); Node<T> m = r.Next; r.Next = q; q.Next = m; } else { Console.WriteLine("插入位置過大,error"); } } public T Delete(int i) { if (IsEmpty() || i < 1) { Console.WriteLine("鏈表為空或者位置錯誤"); return default(T); } Node<T> q = new Node<T>(); if (i == 1) { q = head; head = head.Next; return q.Data; } Node<T> p = head; int j = 1; while (p.Next != null && j < i) { q = p; p = p.Next; j++; } if (j == i) { q.Next = p.Next; return p.Data; } else { Console.WriteLine("位置不正確"); return default(T); } } //獲得單鏈表第i個元素 public T GetElem(int i) { if (IsEmpty()) { Console.WriteLine("鏈表是空鏈表"); return default(T); } Node<T> p = new Node<T>(); p = head; int j=1; while(p.Next!=null&&j<i) { p = p.Next; j++; } if (j == i) { return p.Data; } else { Console.WriteLine("位置不正確!"); } return default(T); } //在單鏈表中查找值為value的節點 public int Locate(T value) { if (IsEmpty()) { Console.WriteLine("鏈表是空鏈表!"); return -1; } Node<T> p = new Node<T>(); p = head; int i = 1; while (((p.Next!=null)&&(!p.Data.Equals(value)))) { p = p.Next; i++; } if (p == null) { Console.WriteLine("不存在這樣的節點。"); return -1; } else { return i; } } } }
③ Node.cs 節點類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 單鏈表 { public class Node<T> { private T data;//數據域 private Node<T> next;//引用域 //構造器 public Node(T val, Node<T> p) { data = val; next = p; } //構造器 public Node(Node<T> p) { next = p; } //構造器 public Node(T val) { data = val; } //構造器 public Node() { data = default(T); next = null; } //數據域屬性 public T Data { get { return data; } set { data = value; } } //引用域屬性 public Node<T> Next { get { return next; } set { next = value; } } } }
④Program.cs 主程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 單鏈表 { class Program { static void Main(string[] args) { LinkList<string> link = new LinkList<string>(); link.Append("123"); link.Append("567"); link.Append("jqk"); link.Insert("abc",2); link.InsertPost("def",2); int length = link.GetLength(); int k=link.Locate("567"); string m=link.GetElem(3); Console.WriteLine("567的位置為"+k); Console.WriteLine("位置為3的值為"+m); Console.WriteLine("鏈表的長度為"+length); Node<string> n = link.Head; while (n != null) { Console.WriteLine(n.Data); n = n.Next; } } } }
⑤運行結果如下圖,和預測結果完全一致