在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
概念 主要参考C#数据结构一书 代码 线性表接口 using System; using System.Collections.Generic; using System.Text; namespace CSharpDemo { public interface ILinearList<T> { int GetLength(); void Clear(); bool IsEmpty(); void Append(T item); void Insert(T item, int i); void Delete(int i); T GetElement(int i); int Locate(T item); } } 顺序表 using System; using System.Collections.Generic; using System.Text; namespace CSharpDemo { /// <summary> /// 顺序表实现 /// </summary> /// <typeparam name="T"></typeparam> public class SequenceList<T>:ILinearList<T> { #region Field and Property private int maxsize; private T[] data; private int last; public int MaxSize { get { return this.maxsize; } set { this.maxsize = value; } } public int Last { get { return this.last; } } #endregion public T this[int i] { get { return data[i]; } set { data[i] = value; } } public SequenceList(int size) { this.MaxSize = size; data = new T[size]; last = -1; } #region ILinearList<T> Member public int GetLength() { return last + 1; } public void Clear() { last = -1; } public bool IsEmpty() { return last == -1 ? true : false; } public void Append(T item) { data[++last] = item; } public void Insert(T item, int index) { for (int i = last; i >= index; i--) { data[i + 1] = data[i]; } data[index] = item; ++last; } public void Delete(int index) { for (int i = index; i <= last; i++) { data[i] = data[i + 1]; } --last; } public T GetElement(int index) { if (IsEmpty()) { return default(T); } return data[index]; } public int Locate(T item) { int flag = -1; for (int i = 0; i <= last; i++) { if (item.Equals(data[i])) { flag = i; break; } } return flag; } #endregion public override string ToString() { string dataString = string.Empty; for (int i = 0; i <= last; i++) { dataString += data[i] + " "; } return dataString; } } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论