Use C # To learn a linear table of data structures (I ),
What is a linear table?
A linear table is the simplest, most basic, and most commonly used data structure. A linear table is an Abstract of a linear structure. A linear structure is characterized by a one-to-one linear relationship between data elements in the structure. This one-to-one relationship refers to the positional relationship between data elements, that is, (1) except for the data element at the first position, there is only one data element before the positions of other data elements; (2) Except for the data element at the last position, there is only one element behind the position of other data elements. That is to say, data elements are arranged one by one. Therefore, we can think of a linear table as a data structure of a sequence of data elements.
Interface Definition of linear table
1 public interface IListDS <T> {2 int GetLength (); // calculates the length of 3 void Clear (); // clears 4 bool IsEmpty (); // determine whether the linear table is empty. 5 void Append (T item); // Add operation 6 void Insert (T item, int I ); // insert operation 7 T Delete (int I); // Delete operation 8 T GetElem (int I); // obtain table metadata 9 int Locate (T value ); // Search 10 by value}
Implementation of SeqList for sequence tables <T>
Public class SeqList <T>: IListDS <T> {private int maxsize; // capacity of the sequence table private T [] data; // array, used to store the data element private int last in the sequence table; // indicates the position of the last element in the sequence table. // The index is public T this [int index] {get {return data [index];} set {data [index] = value ;}// the Last data element location attribute public int last {get {return Last ;}} // capacity attribute public int Maxsize {get {return maxsize;} set {maxsize = value ;}/// constructor public SeqList (int size ){ Data = new T [size]; maxsize = size; last =-1;} // calculate the length of the sequence table public int GetLength () {return last + 1 ;} // Clear the sequence table public void Clear () {last =-1;} // determine whether the sequence table is empty public bool IsEmpty () {if (last =-1) {return true;} else {return false;} // determines whether the sequence table is full public bool IsFull () {if (last = maxsize-1) {return true ;} else {return false ;}}// Add the new public void Append (T item) {if (IsFull () element to the end of the sequence table ()) {Console. writeLine ("List is full"); return;} data [++ last] = item ;} // Insert a Data Element public void Insert (T item, int I) {if (IsFull () {Console. writeLine ("List is full"); return;} if (I <1 | I> last + 2) {Console. writeLine ("Position is error! "); Return;} if (I = last + 2) {data [last + 1] = item;} else {for (int j = last; j> = I-1; -- j) {data [j + 1] = data [j];} data [I-1] = item ;}++ last ;} // Delete the data element public T Delete (int I) {T tmp = default (T); if (IsEmpty () {Console. writeLine ("List is empty"); return tmp;} if (I <1 | I> last + 1) {Console. writeLine ("Position is error! "); Return tmp;} if (I = last + 1) {tmp = data [last --];} else {tmp = data [I-1]; for (int j = I; j <= last; ++ j) {data [j] = data [j + 1] ;}} -- last; return tmp ;} // obtain the I-th data element of the sequence table public T GetElem (int I) {if (IsEmpty () | (I <1) | (I> last + 1) {Console. writeLine ("List is empty or Position is error! "); Return default (T);} return data [I-1];} // search for the public int Locate (T value) data element with the value in the sequence table) {if (IsEmpty () {Console. writeLine ("List is Empty! "); Return-1;} int I = 0; for (I = 0; I <= last; ++ I) {if (value. equals (data [I]) {break ;}} if (I> last) {return-1;} return I ;}}