【C#】【資料結構】001-線性表:順序表
阿新 • • 發佈:2018-11-09
C#資料結構:順序表結構
1、自定義順序表結構
using System.Collections; using System.Collections.Generic; /// <summary> ///線性表介面 /// </summary> /// <typeparam name="T"></typeparam> public interface IListDS<T> { int MaxSize(); bool IsEmpty(); bool IsFull(); void Add(T item); void Insert(T item, int index); T Delete(int index); int GetLength(); T this[int index] { get; } T GetElem(int index); int Locate(T value); void Clear(); } /// <summary> /// 順序表 /// </summary> /// <typeparam name="T"></typeparam> public class SqeList<T> : IListDS<T> { private T[] data; private int last = -1; //表的最後索引,表空是last=-1 //初始化順序表 public SqeList(int size)//size最大容量 { data = new T[size]; last = -1; } public SqeList() : this(10)//預設建構函式,容量10 { } //表最大容量 public int MaxSize() { return data.Length; } //判空 public bool IsEmpty() { return last == -1; } //判滿 public bool IsFull() { return last == data.Length - 1; } //新增操作 public void Add(T item) { if (IsFull()) { Debug.LogError("當前順序表已存滿!"); return; } data[last + 1] = item; last++; } //插入操作 public void Insert(T item, int index) { if (index < 0 || index > last+1) //合法的插入 0<=index<=last+1 { Debug.LogError("插入index不合法!"); return; } if (IsFull()) { Debug.LogError("當前順序表已存滿!"); return; } for (int i = last; i >= index; i--) { data[i + 1] = data[i];//將後面的元素向後移動 } data[index] = item; last++; } //根據index刪除表元素 public T Delete(int index) { if (index < 0 || index > last) { Debug.LogError("刪除位置index不合法!"); return default(T); } T temp = data[index]; for (int i = index + 1; i <= last; i++) { data[i - 1] = data[i];//將後面的元素向後移 } last--; return temp; } //表長 public int GetLength() { return last + 1; } //根據索引獲得表元素 public T GetElem(int index) { if (index >= 0 && index <= last) { return data[index]; } else { Debug.LogError("索引不合法!"); return default(T); } } public T this[int index]//索引器訪問表元素值 { get { return GetElem(index); } } //獲取元素索引值 public int Locate(T value) { for (int i = 0; i <= last; i++) { if (data[i].Equals(value)) { return i; } } return -1; } //清空順序表 public void Clear() { last = -1; } //顯示所有的表元素值 public void Display() { if (IsEmpty()) { Debug.Log("表中沒有元素"); return; } Debug.Log("表中的值:"); for (int i = 0; i <= last; i++) { Debug.Log("index:"+i.ToString()+" value:"+data[i]); } } }
順序表:測試用例
using System.Collections; using System.Collections.Generic; using UnityEngine; public class _001LineTable : MonoBehaviour { //初始化順序表 SqeList<string> sqeList = new SqeList<string>(20); void Start() { Debug.Log("順序表的最大容量:" + sqeList.MaxSize()); //判空操作 Debug.Log("順序表是否為空:" + sqeList.IsEmpty()); //判滿操作 Debug.Log("順序表是否已滿:" + sqeList.IsFull()); //新增操作 Debug.Log("新增操作--------------新增'123','456','789'"); sqeList.Add("123"); sqeList.Add("456"); sqeList.Add("789"); sqeList.Display(); Debug.Log("順序表是否為空:" + sqeList.IsEmpty()); //插入操作 Debug.Log("插入操作---------------在index=2處插入字串:'111'"); sqeList.Insert("111", 2); sqeList.Display(); //刪除操作 sqeList.Delete(2); Debug.Log("刪除操作---------------刪除index=2的元素"); sqeList.Display(); //表長 Debug.Log("表長-------------------順序表表長:" + sqeList.GetLength()); //查詢 Debug.Log("查詢--------------index查value"); Debug.Log("index=0的值:" + sqeList[0]); Debug.Log("index=2的值:" + sqeList.GetElem(2)); Debug.Log("查詢--------------value查index"); Debug.Log("'789’的index值:" + sqeList.Locate("789")); //清空 Debug.Log("清空表"); sqeList.Clear(); sqeList.Display(); }
輸出結果:
注意
1.順序表的基本操作:初始化表,判空,判滿,新增,插入,刪除,查詢,表長,清空。
2.此處的清空表,只是將last置為-1,來表示為表空,而data中依然有原來的資料,表也沒有被銷燬。再次的賦值該表,只是將之前的資料覆蓋掉。
3.last表示該表的最後元素的索引,last = -1:表示表為空表。
4.插入操作時,插在尾端時,不用移動元素。