C#完成次序表(線性表)完全實例。本站提示廣大學習愛好者:(C#完成次序表(線性表)完全實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成次序表(線性表)完全實例正文
本文實例講述了C#完成次序表(線性表)的辦法。分享給年夜家供年夜家參考,詳細以下:
根本思惟是應用數組作為盛放元素的容器,數組一開端的年夜小要完成肯定,並應用一個Pointer指向次序表中最初的元素。次序表中的元素是數組中元素的子集。次序表在內存中是持續的,優勢是查找,弱勢是拔出元素和刪除元素。
為防止裝箱拆箱,這裡應用泛型,取代object。應用object的例子可以參照本站這篇文章:http://www.jb51.net/article/87603.htm,這個鏈接中的例籽實現的是隊列,並沒 有應用Pointer來標識 次序表中最初一個元素,而是靜態的調劑數組的年夜小,這與本例顯著分歧,靜態調劑數組年夜小開支較年夜。應用object異樣可以完成次序表數據構造,然則頻仍裝箱拆箱形成較年夜的開支,應應用泛型取代。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinearList { public interface IListDS<T> { int GetLength(); void Insert(T item, int i); void Add(T item); bool IsEmpty(); T GetElement(int i); void Delete(int i); void Clear(); int LocateElement(T item); void Reverse(); } //次序表類 class SequenceList<T>:IListDS<T> { private int intMaxSize;//最年夜容量事前肯定,應用數組必需先肯定容量 private T[] tItems;//應用數組盛放元素 private int intPointerLast;//一直指向最初一個元素的地位 public int MaxSize { get { return this.intMaxSize; } set { this.intMaxSize = value; } } public T this[int i]//索引器便利前往 { get { return this.tItems[i]; } } public int PointerLast { get { return this.intPointerLast; } } public SequenceList(int size) { this.intMaxSize = size; this.tItems = new T[size];//在這裡初始化最公道 this.intPointerLast = -1;//初始值設為-1,此時數組中元素個數為0 } public bool IsFull()//斷定能否超越容量 { return this.intPointerLast+1 == this.intMaxSize; } #region IListDS<T> 成員 public int GetLength() { return this.intPointerLast + 1;//不克不及前往tItems的長度 } public void Insert(T item, int i)//設i為第i個元素,從1開端。該函數表現在第i個元素前面拔出item { if (i < 1 || i > this.intPointerLast + 1) { Console.WriteLine("The inserting location is wrong!"); return; } if (this.IsFull()) { Console.WriteLine("This linear list is full! Can't insert any new items!"); return; } //假如可以添加 this.intPointerLast++; for(int j=this.intPointerLast;j>=i+1;j--) { this.tItems[j] = this.tItems[j - 1]; } this.tItems[i] = item; } public void Add(T item) { if (this.IsFull())//假如超越最年夜容量,則沒法添加新元素 { Console.WriteLine("This linear list is full! Can't add any new items!"); } else { this.tItems[++this.intPointerLast] = item;//表長+1 } } public bool IsEmpty() { return this.intPointerLast == -1; } public T GetElement(int i)//設i最小從0開端 { if(this.intPointerLast == -1) { Console.WriteLine("There are no elements in this linear list!"); return default(T); } if (i > this.intPointerLast||i<0) { Console.WriteLine("Exceed the capability!"); return default(T); } return this.tItems[i]; } public void Delete(int i)//設i最小從0開端 { if (this.intPointerLast == -1) { Console.WriteLine("There are no elements in this linear list!"); return; } if (i > this.intPointerLast || i < 0) { Console.WriteLine("Deleting location is wrong!"); return; } for (int j = i; j < this.intPointerLast; j++) { this.tItems[j] = this.tItems[j + 1]; } this.intPointerLast--;//表長-1 } public void Clear() { this.intPointerLast = -1; } public int LocateElement(T item) { if (this.intPointerLast == -1) { Console.WriteLine("There are no items in the list!"); return -1; } for (int i = 0; i <= this.intPointerLast; i++) { if (this.tItems[i].Equals(item))//若是自界說類型,則T類必需把Equals函數override { return i; } } Console.WriteLine("Not found"); return -1; } public void Reverse() { if (this.intPointerLast == -1) { Console.WriteLine("There are no items in the list!"); } else { int i = 0; int j = this.GetLength() / 2;//成果為下界整數,正好用於輪回 while (i < j) { T tmp = this.tItems[i]; this.tItems[i] = this.tItems[this.intPointerLast - i]; this.tItems[this.intPointerLast - i] = tmp; i++; } } } #endregion } class Program { static void Main(string[] args) { } } }
基於次序表的歸並排序:
//基於次序表的歸並排序 static private SequenceList<int> Merge(SequenceList<int> s1,SequenceList<int> s2) { SequenceList<int> sList = new SequenceList<int>(20); int i = 0; int j = 0; while(i<=s1.PointerLast&&j<=s2.PointerLast) { if (s1[i] < s2[j]) { sList.Add(s1[i]); i++; } else { sList.Add(s2[j]); j++; } } if (i > s1.PointerLast) { while (j <= s2.PointerLast) { sList.Add(s2[j]); j++; } return sList; } else//即j>s2.PointerLast { while (i <= s1.PointerLast) { sList.Add(s1[i]); i++; } return sList; } }
願望本文所述對年夜家C#法式設計有所贊助。