Linear Structure(Linear stucture) is the most basic structure in data structure. Its Special requisition graphic representation is as follows:
That is, each element has only one element (called the "precursor") before it, and there is only one element (called "Successor") after it. -- Note: the precursor of the starting element is null, and the successor of the ending element is null, so that the concept will not conflict.
Linear table (list)Is a typical Implementation of linear structures, and it can alsoDivided:Sequence Table (seqlist)AndLinked List)Two categories.
The basic features of a sequence table (seqlist) are that elements are stored sequentially in the storage unit during internal storage, therefore, you only need to know the "storage address of the starting element"-the base address of the sequence table and the location of any element in the sequence table (that is, it is the first element ), you can directly locate the address of the element to directly access the value of the element. That is to say, the time used to store/read each element is the same.Random Access"
In C # language, arrays occupy a group of continuous storage areas in the memory. Therefore, it is not suitable to use arrays to implement sequential tables.
First, define the general interface ilistds. CS of the linear table (Note: DS is the abbreviation of Datastructure)
Namespace linear table {public interface ilistds <t> {// get the actual number of elements in the linear table int count (); // clear the linear table void clear (); // determine whether the linear table is empty. bool isempty (); // append the element void append (T item) at the end ); // Insert the element item void insertbefore (T item, int I) in front of position I; // Insert the element item void insertafter (T item, int I); // Delete the T removeat (int I) element at index I; // obtain the T getitemat (int I) element at index position I ); // return the index int indexof (T value) of the element value; // reverse all elements of the linear table void reverse ();}}
Implementation of sequence table (seqlist:
Using system; using system. text; namespace linear table {// <summary> /// sequential table // </Summary> /// <typeparam name = "T"> </typeparam> public class seqlist <t>: ilistds <t> {private int maxsize; private T [] data; private int last; // The Public t this [int Index] {get {return this. getitemat (INDEX);} set {If (index <0 | index> last + 1) {console. writeline ("position is error"); return;} data [Index] = value; }}// Subscript of the last element public int last {get {return last ;}/// maximum capacity public int maxsize {get {return this. maxsize;} set {This. maxsize = value ;}/// constructor public seqlist (INT size) {DATA = new T [size]; maxsize = size; last =-1 ;} // return the actual length of the linked list public int count () {return last + 1;} // clear public void clear () {last =-1 ;} // whether public bool isempty () {return last =-1;} // whether it is full public bool isfull () {Return last = maxsize-1;} // append the public void append (T item) {If (isfull () {console. writeline ("list is full"); return;} data [++ last] = item ;} /// <summary> /// prefix /// </Summary> /// <Param name = "item"> elements to be inserted </param> /// <param name = "I"> index of the location to be inserted </param> Public void insertbefore (T item, int I) {If (isfull () {console. writeline ("list is full"); return;} if (I <0 | I> last + 1 ){ Console. writeline ("position is error"); return;} if (I = last + 1) {data [last + 1] = item ;} else {// position I and all elements after I are moved for (Int J = last; j> = I; j --) {data [J + 1] = data [J];} data [I] = item ;}++ last ;} /// <summary> /// insert // </Summary> /// <Param name = "item"> </param> /// <Param name =" I "> </param> Public void insertafter (T item, int I) {If (isfull () {console. writeline ("list is full"); Re Turn;} if (I <0 | I> last) {console. writeline ("position is error"); return;} if (I = last) {data [last + 1] = item ;} else {// elements after position I (excluding position I), all are moved back for (Int J = last; j> I; j --) {data [J + 1] = data [J];} data [I + 1] = item;} + + last ;} /// <summary> /// Delete element /// </Summary> /// <Param name = "I"> index of the element to be deleted </param> // /<returns> </returns> Public t removeat (int I) {t tmp = default (t); If (isempty () {Console. writeline ("list is empty"); Return TMP;} if (I <0 | I> last) {console. writeline ("position is error! "); Return TMP;} if (I = last) {TMP = data [last];} else {TMP = data [I]; // advance the position I and elements after I (Int J = I; j <= last; j ++) {data [J] = data [J + 1] ;}}-- last; return TMP ;} /// <summary> /// obtain the elements in the nth position /// </Summary> /// <Param name = "I"> position </param> /// <returns> </returns> Public t getitemat (int I) {If (isempty () | (I <0) | (I> last) {console. writeline ("list is empty or position is error! "); Return default (t);} return data [I];} /// <summary> /// subscript index of the positioning element /// </Summary> /// <Param name = "value"> </param> // <returns> </returns> Public int indexof (T value) {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 ;} /// <summary> // element inversion // </Summary> Public void reverse () {t tmp = default (t); For (INT I = 0; I <= last/2; I ++) {TMP = data [I]; data [I] = data [last-I]; data [last-I] = TMP;} public override string tostring () {stringbuilder sb = new stringbuilder (); For (INT I = 0; I <= last; I ++) {sb. append (data [I]. tostring () + ",");} return sb. tostring (). trimend (',');}}}
test Code snippet:
Console. writeline ("sequence table test starts... "); seqlist <string> seq = new seqlist <string> (10); seq. append ("X"); seq. insertbefore ("W", 0); seq. insertbefore ("V", 0); seq. append ("Y"); seq. insertbefore ("Z", seq. count (); console. writeline (seq. count (); // 5 console. writeline (seq. tostring (); // V, W, X, Y, Z console. writeline (SEQ [1]); // W console. writeline (SEQ [0]); // v console. writeline (SEQ [4]); // Z console. writeline (seq. indexof ("Z"); // 4 console. writeline (seq. removeat (2); // X console. writeline (seq. tostring (); // V, W, Y, Z seq. insertbefore ("X", 2); console. writeline (seq. tostring (); // V, W, X, Y, Z console. writeline (seq. getitemat (2); // X seq. reverse (); console. writeline (seq. tostring (); // Z, Y, X, W, V seq. insertafter ("Z_1", 0); seq. insertafter ("Y_1", 2); seq. insertafter ("v_1", seq. count ()-1); console. writeline (seq. tostring (); // Z, Z_1, Y, y_1, X, W, V, v_1
Advantages of an ordered table: it can be directly located when reading elements. Therefore, in some operations (such as reversing elements in an ordered table), full traversal is not required, and the number of cycles (that is, the time complexity) it can be halved in terms of full traversal.
Advantages of sequence tables: insert/delete elements. To maintain their order, the subsequent elements need to be moved, increasing the time overhead.
Finally, it is pointed out that the. NET namespaceSystem. Collections. GenericInList <t>Is a built-in sequence table.