顺序表
定义接口
namespace AlgorithmTest06_List { interface IListDS<T> { int GetLength();
void Chear();
bool IsEmpty();
T Add(T item);
void Insert(int index, T item);
T Delete(int i);
T this[int i] { get; }
T GetElem(int i);
T Set(int i, T item);
int IndexOf(T value);
void Remove(T value); } }
|
实现接口
class SeqList<T> : IListDS<T> { private T[] data; private int count = 0; public SeqList(int size) { data = new T[size]; count = 0; } public SeqList() : this(10) { } public void Insert(int index, T item) { if (index < 0 || index > count) { throw new ArgumentException("数组索引越界"); } if (count == data.Length) { ResetCapacity(2 * data.Length); } for (int i = count - 1; i >= index; i--) { data[i + 1] = data[i]; } data[index] = item; count++; } public T Add(T item) { Insert(count, item); return item; } public T Delete(int index) { if (count == data.Length / 4) { ResetCapacity(data.Length / 2); } if (index < 0 || index >= count) { throw new ArgumentException("索引超出数组界限"); } T temp = data[index]; for (int i = index + 1; i < count; i++) { data[i - 1] = data[i]; } count--; data[count] = default(T); return temp; } public void Chear() { count = 0; } public T GetElem(int i) { if (i >= 0 && i < count) { return data[i]; } else { return default(T); throw new ArgumentException("索引不存在"); } } public T this[int i] { get { return GetElem(i); } } public T Set(int i, T item) { if (i >= 0 && i < count) return data[i] = item; else throw new ArgumentException("索引越界"); } public int GetCount() { return count; } public int GetLength() { return data.Length;
} public bool IsEmpty() { return count == 0; } public int IndexOf(T value) { for (int i = 0; i < count; i++) { if (data[i].Equals(value)) { return i; } } return -1; } public void Remove(T value) { int index = IndexOf(value); if (index != -1) { Delete(index); } } public override string ToString() { StringBuilder res = new StringBuilder();
for (int i = 0; i < count - 1; i++) { res.Append(data[i]); res.Append(","); } res.Append(data[count - 1]); return res.ToString(); } private void ResetCapacity(int newCapacity) { T[] newData = new T[newCapacity]; for (int i = 0; i < count; i++) { newData[i] = data[i]; } data = newData; } }
|
测试
class Program { static void Main(string[] argss) { #region 添加和删除 SeqList<int> seqlist = new SeqList<int>(20); Console.WriteLine("元素个数:" + seqlist.GetCount()); for (int i = 0; i < 10; i++) { seqlist.Add(i); } Console.WriteLine("已添加元素个数:" + seqlist.GetCount()); Console.WriteLine("----------------");
Console.WriteLine("索引3添加数字100"); seqlist.Insert(3, 100); Console.WriteLine(seqlist);
Console.WriteLine("个数:" + seqlist.GetCount());
seqlist.Chear();
Console.WriteLine("清空后个数:" + seqlist.GetCount());
Console.WriteLine("是否为空" + seqlist.IsEmpty());
Console.WriteLine("----------------");
for (int i = 4; i > 0; i--) { seqlist.Add(i); } Console.WriteLine("重新添加4~1"); Console.WriteLine(seqlist);
Console.WriteLine("\n----------------");
Console.WriteLine("删除:" + seqlist.Delete(2)); Console.WriteLine("打印" + seqlist);
Console.WriteLine("\n----------------"); #endregion
#region 查找 Console.WriteLine("输入值找索引(-1为找不到):"); int value = int.Parse(Console.ReadLine()); Console.WriteLine("索引:" + seqlist.IndexOf(value)); Console.WriteLine("当前值:" + seqlist); Console.WriteLine("找到后删除对象"); seqlist.Remove(value); Console.WriteLine("----------------"); Console.WriteLine("当前值:" + seqlist); Console.WriteLine("索引0替换为100"); seqlist.Set(0, 100); Console.WriteLine(seqlist); #endregion
#region 扩容数组(动态数组) Console.WriteLine("----------------"); Console.WriteLine("扩容数组"); SeqList<int> seq = new SeqList<int>(10); for (int i = 0; i < 10; i++) { seq.Add(i); } Console.WriteLine("数组元素展示:" + seq); Console.WriteLine("容量:" + seq.GetLength()); Console.WriteLine("数组元素数量:" + seq.GetCount()); Console.WriteLine("添加元素:" + seq.Add(1000)); Console.WriteLine("数组元素展示:" + seq); Console.WriteLine("容量" + seq.GetLength()); Console.WriteLine("数组元素数量:" + seq.GetCount()); #endregion
Console.ReadKey(); } }
|
重写ToString,添加打印功能
class SeqList<T> : IListDS<T>
public override string ToString() { StringBuilder res = new StringBuilder();
for (int i = 0; i < count; i++) { res.Append(data[i]); if (i != count - 1) { res.Append(", "); } } return res.ToString(); }
|
修改一下,去掉了if,将最后一个元素单独打印
public override string ToString() { StringBuilder res = new StringBuilder();
for (int i = 0; i < count - 1; i++) { res.Append(data[i]); res.Append(","); } res.Append(data[count - 1]); return res.ToString(); }
|
Main方法中直接打印seqlist
结果如下:
ZHY
除了生病以外,你感受到的痛苦,都是你的价值观带来的,而非真实存在。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Niaoyu!