Sequence table in data structures and algorithms

Source: Internet
Author: User
In a computer, the simplest and most natural way to save a linear table is to put the elements in the table one by one into a sequential storage unit, this is the SequenceStorage sequence table definition. In a computer, the simplest and most natural way to save a linear table is to put the elements in the table one by one into the sequential Storage unit, which is the sequential Storage of the linear table ).The sequential storage of a linear table is to store the data elements of a linear table in Sequence using an address-contiguous space in the memory. a linear table stored in this way is called a Sequence List, as shown in Figure 2.1. An ordered table is characterized by adjacent data elements in the table that are stored in the memory.



. 1. storage structure of the ordered table assume that each data element in the ordered table occupies w storage units. if the storage address of the I-th data element is Loc (ai), there are: loc (ai) = Loc (a1) + (i-1) * w 1 ≤ I ≤ n in the Loc (a1) represents the storage address of the first data element a1, it is also the starting storage Address of the sequence table, called the Base Address of the sequence table ). That is to say, as long as you know the base address of the sequence table and the number of storage units occupied by each data element, you can find the storage address of any data element in the sequence table.. In addition, because the storage address of each data element in the computing sequence table is the same, the sequence table has the characteristics of random access. C # The storage space occupied by arrays in the memory is a set of continuous storage areas. Therefore, arrays have the characteristics of random access. Therefore, arrays are inherently characteristic of data storage areas that represent sequential tables.
Think of the sequence table as a generic class. the class name is SeqList. ." Seq is the first three letters of the English word "Sequence. SeqList Class implements the interface IListDS . Use arrays to store elements in the sequence table. Class is represented by field data. Because you often need to insert or delete data elements in the sequence table, the table length of the sequence table must be variable. Therefore, the size of the Array needs to be designed to be very large and can be expressed using the Length attribute of System. Array. However, to describe the maximum length of the sequence table (the capacity of the sequence table ), The maxsize field is used in the class. The value of maxsize can be modified according to actual needs. Class constructor parameter size. The elements in the sequence table are stored sequentially starting from data [0]. because the actual number of elements in the sequence table generally does not reach maxsize Class requires a field "last" to indicate the position of the last data element in the sequence table in the array. If the sequence table has data elements, the last change range is 0 to the maxsize-1, if the sequence table is empty, last =-1. For more information, see. 1. Due to the restrictions of the sequential tablespace, when inserting data elements into the sequence table, you must determine whether the sequence table is full or not. when the sequence table is full, you cannot insert elements. Therefore, SeqList Besides the IListDS interface In addition, you also need to determine whether the sequence table is full or not. Sequence table SeqList Is described as follows. Public class SeqList : IListDS {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 ;}/// determine whether the sequence table is full public bool IsFull () {if (last = maxsize-1) {return true;} else {return false ;}// add the new element public void Append (T item) at the end of the sequence table) {if (IsFull () {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;
} // 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];} // find the data element with the value in the sequence table public int Locate (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 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.