C#简易线性表实现。

哦,看了SIKI的数据结构教程,自己尝试实现了一下。


接口类如下,主要就实现了这几个,

using System;

namespace DSL {
	interface IListDataStruct<T> {
		
		/// <summary>
		/// Add the specified data.
		/// </summary>
		/// <param name="data">Data.</param>
		void Add(T data);

		/// <summary>
		/// Delete the specified data.
		/// </summary>
		/// <param name="data">Data.</param>
		void Delete(T data);

		/// <summary>
		/// Clear this instance.
		/// </summary>
		void Clear();

		/// <summary>
		/// Insert the specified data and index.
		/// </summary>
		/// <param name="data">Data.</param>
		/// <param name="index">Index.</param>
		void Insert(T data,int index);

		/// <summary>
		/// Ises the empty.
		/// </summary>
		/// <returns><c>true</c>, if empty was ised, <c>false</c> otherwise.</returns>
		bool isEmpty();

		/// <summary>
		/// Gets the <see cref="DSL.IListDataStruct`1"/> at the specified index.
		/// </summary>
		/// <param name="index">Index.</param>
		T this [int index]{ get;}

		/// <summary>
		/// Gets the index of the element by.
		/// </summary>
		/// <returns>The element by index.</returns>
		/// <param name="index">Index.</param>
		T GetElementByIndex(int index);

		/// <summary>
		/// Gets the index by element.
		/// </summary>
		/// <returns>The index by element.</returns>
		/// <param name="data">Data.</param>
		int GetIndexByElement(T data);

		/// <summary>
		/// Gets the length.
		/// </summary>
		/// <returns>The length.</returns>
		int GetLength();

	}
}



实现类,自动扩充,直接用暴力的加5设置的。哈哈。

using System;

namespace DSL {
	public class SeqList<T> : IListDataStruct<T> {


		private int count = 0;
		private T[] datas;
		private int ssize = 0;

		public SeqList(int size){
			count = 0;
			this.ssize = size;
			datas = new T[size];
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DSL.SeqList`1"/> class.
		/// defalut size is ten.
		/// </summary>
		public SeqList():this(10){}

		void ReNewArray (T data) {
			T[] newArray = new T[datas.Length + 5];
			datas.CopyTo (newArray, 0);
			datas = newArray;
			datas [count] = data;
			count++;
			Console.WriteLine ("Renew OK");
		}

		public void Add(T data){
			if (count == datas.Length) {
				ReNewArray (data);
			}else{
				datas [count] = data;
				count++;
			}
		}

		public void Delete(T data){
			int index = GetIndexByElement (data);
			Delete (index);
		}

		public void Delete(int index){
			if (index >= 0 && index < count) {
				if (index == count - 1) {
					datas [index] = default(T);
					count--;
				} else {
					for (int i = index; i < count - 1; i++) {
						datas [i] = datas [i + 1];
					}
					datas [count - 1] = default(T);
					count--;
				}
			} else {
				Console.WriteLine ("Value is not in correct index");
				throw new ArgumentOutOfRangeException ();
			}
		}

		public void Clear(){
			if (count == 0) {
				return;
			}else {
				int size = datas.Length;
				datas = null;
				datas = new T[size];
				count = 0;
			}
			Console.WriteLine ("Clear ok");
		}

		public void Insert(T data,int index){
			if (index >= 0 && index < count - 1) {
				for (int i = count; i > index; i--) {
					datas [i] = datas [i - 1];
				}
				datas [index] = data;
				count++;
			} else {
				Console.WriteLine ("index is wrong");
				throw new ArgumentOutOfRangeException ();
			}
		}

		public bool isEmpty(){
			return count == 0;
		}

		public T this [int index] {
			get {
				return GetElementByIndex (index);
			}
		}

		public T GetElementByIndex(int index){
			if (index >= 0 && index < count) {
				return datas [index];
			} else {
				Console.WriteLine ("Index is out of array");
				throw new ArgumentOutOfRangeException ();
			}
		}

		public int GetIndexByElement(T data){
			int index = 0;
			for (int i = 0; i < datas.Length; i++) {
				if (datas[i].Equals(data)) {
					index = i;
					break;
				}
			}
			return index;
		}

		public int GetLength(){
			return count;
		}
	}
}

测试类:


using System;

namespace DSL {
	class MainClass {
		public static void Main (string[] args) {
			SeqList<string> testA = new SeqList<string> (10);
			testA.Add ("1");
			testA.Add ("2");

			for (int i = 0; i < testA.GetLength(); i++) {
				Console.WriteLine (testA [i]);
			}

			Console.ReadKey ();

			testA.Insert ("3", 0);
			for (int i = 0; i < testA.GetLength(); i++) {
				Console.WriteLine (testA [i]);
			}

			Console.ReadKey ();
			testA.Add ("4");
			testA.Add ("5");
			testA.Add ("6");
			testA.Add ("7");
			testA.Add ("8");
			testA.Add ("9");
			testA.Add ("10");
			for (int i = 0; i < testA.GetLength(); i++) {
				Console.WriteLine (testA [i]);
			}

			Console.ReadKey ();

			testA.Add ("11");
			for (int i = 0; i < testA.GetLength(); i++) {
				Console.WriteLine (testA [i]);
			}

			Console.ReadKey ();

			testA.Delete (0);
			for (int i = 0; i < testA.GetLength(); i++) {
				Console.WriteLine (testA [i]);
			}

			Console.ReadKey ();

			testA.Clear ();
			for (int i = 0; i < testA.GetLength(); i++) {
				Console.WriteLine (testA [i]);
			}

			Console.ReadKey ();

			testA.Add ("wwww");
			for (int i = 0; i < testA.GetLength(); i++) {
				Console.WriteLine (testA [i]);
			}

			Console.ReadKey ();
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值