顺序表定义:
/// <summary> /// Description of Sequence. /// </summary> public struct Sequence<T> where T:IComparable { private T[] _data; private int _current; public Sequence(int size) { _data=new T[size]; _current=0; } public T[] Data { get{return _data;} } public int Current { get{return _current;} set{_current=value;} } public int Size { get{return _data.Length;} } }
带有指针指向可用值得数组:
/// <summary> /// A array has point to show the end position of the value /// </summary> public struct SizedArray<T> where T:IComparable { private T[] _data; private int _current; public SizedArray(int size) { _data=new T[size]; _current=0; } public int Current { get{return _current;} set{_current=value;} } public int Size { get{return _data.Length;} } public T[] GetArray() { T[] array=new T[_current]; for(int i=0;i<_current;i++) { array[i]=_data[i]; } return array; } public void Insert(T x) { if(_current>=Size) { return; } _data[_current]=x; _current++; } }
顺序表的实现类:
public class SequenceList<T> where T:IComparable { private Sequence<T> _sequence; private int _size; /// <summary> /// /// </summary> /// <param name="size">The size of sequence list</param> public SequenceList(int size) { _size=size; _sequence=new Sequence<T>(_size); } /// <summary> /// Init the sequence list /// </summary> public void Init() { _sequence.Current=0; } /// <summary> /// At the end of sequence list,Insert a value /// </summary> /// <param name="x">The value want to be inserted</param> public void insert(T x) { if(IsFull()) return; _sequence.Data[_sequence.Current]=x; _sequence.Current++; } /// <summary> /// Print all the values of the sequence list /// </summary> public void Print() { if(IsEmpty()) return; for(int i=0;i<_sequence.Current;i++) { Console.WriteLine(" {0} ",_sequence.Data[i]); } } /// <summary> /// Check whether the sequence list is empty /// </summary> /// <returns>True or false</returns> public bool IsEmpty() { if(_sequence.Current==0) { Console.WriteLine("Sequence is empty!"); return true; } return false; } /// <summary> /// Check whether the sequence list is full /// </summary> /// <returns>True or false</returns> public bool IsFull() { if(_sequence.Current>=_sequence.Size) { Console.WriteLine("Sequence is full!"); return true; } return false; } /// <summary> /// Check whether the specified position is exist or not /// </summary> /// <param name="i">The specified position</param> /// <returns>True or false</returns> public bool PositionIsExist(int i) { if(i<0||i>=_sequence.Current) { Console.WriteLine("Specified position is not exist!"); return false; } return true; } /// <summary> /// Find all the positions whose values are equal to the specified value /// </summary> /// <param name="x">The specified value</param> /// <returns>All fulfiled condition's positions</returns> public int[] FindNum(T x) { SizedArray<int> array=new SizedArray<int>(_size); for(int i=0;i<_sequence.Current;i++) { if(_sequence.Data[i].CompareTo(x)==0) { array.Insert(i); } } return array.GetArray(); } /// <summary> /// Get the value of the specified position /// </summary> /// <param name="i">The specified position</param> /// <returns>The suitable value</returns> public T GetDataPosition(int i) { if(!PositionIsExist(i)) { return default(T); } return _sequence.Data[i]; } // public void InsertPosition(T x,T y) // { // int position; // if(IsFull()) return; // // position=FindNum(y)[0]; // InsertPosition(x,position); // } /// <summary> /// Insert a value in the specified position /// </summary> /// <param name="x">The value</param> /// <param name="position">The specified position</param> public void InsertPosition(T x,int position) { if(IsFull()) return; if(!PositionIsExist(position)) return; for(int i=_sequence.Current-1;i>=position;i--) { _sequence.Data[i+1]=_sequence.Data[i]; } _sequence.Data[position]=x; _sequence.Current++; } /// <summary> /// Delete a value in the specified position /// </summary> /// <param name="position">The specified position</param> public void DeletePosition(int position) { if(IsFull()) return; if(!PositionIsExist(position)) return; for(int i=position;i<_sequence.Current;i++) { _sequence.Data[i]=_sequence.Data[i+1]; } _sequence.Current--; } }