哦,看了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();
}
}
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 ();
}
}
}