Data structure and algorithm--linear table sequential storage structure

Source: Internet
Author: User

Today, we summarize the sequential storage structure in the linear table in the data structure, which is a relatively simple structure.

First, look at what a linear table is.

what is a linear table?

A linear table is a simple data structure that is a one-to-one relationship between data elements, that is, the rest of the elements except the first and last elements. the number of elements is called the length of the linear table, and the length of zero indicates that the linear table is empty.

What is the sequential storage structure of a linear table?

The sequential storage structure in a linear table is that the elements in the linear table are stored in logical order in a contiguous set of storage spaces, and the linear table is called the sequential table. Depending on the characteristics of the sequential table, the elements are usually stored in arrays.

The following is a concrete implementation (C + +).

defines a template class for a sequential table that declares the appropriate method.

<span style= "Font-family:courier new;font-size:14px;" >const int MAXSIZE = +;  Define the maximum length of the Order table template <class t>        //define template class Seqlist {public:    seqlist () {        length = 0;    Parameterless constructor    }    //Initializes a sequential table    seqlist (const T a[],int n) containing n elements;          int GetLength ()       //Get the length of the order table        return lengths;    }    void Printlist (); Print out the element values in the sequential table    void Insert (int i,t x);//insert element x into the specified position    t Delete (int i);//delete the element with the position I,    t Get (int i);    Returns the value of the element in position i    , int Locate (T x);//finds the element with the value x in the order table and returns its location private:    int length;    Sequential table length    T data [MAXSIZE];  Stores an array of data elements};</span>
The implementation of the constructor method with parameters: assigns the elements in the given array to the data array at a time, and throws an exception if the number of elements exceeds the sequential table length

<span style= "Font-family:courier new;font-size:14px;" >template <class t>seqlist<t>::seqlist (const T a[],int N) {   if (n>maxsize)    throw " The length of the array cannot exceed the maximum length of the sequential table ";   for (int i=0;i<n;i++) {    data[i] = A[i];   }   length = n;} </span>
iterating through the elements of the sequential table, it is simpler, for loops, to remove the array elements at once.

<span style= "Font-family:courier new;font-size:14px;" >template <class t>void seqlist<t>::P rintlist () {    cout<< "iterates through the elements in the order table sequentially" <<endl;    for (int i=0;i<length;i++) {        cout<<data[i]<< "";    }    Cout<<endl;} </span>

insert operation ideas for sequential tables:

    1. Determines whether the order table is full, or if full, throws an exception
    2. Determine if the insertion position is reasonable, such as not being inserted in a position larger than the sequential table length
    3. Start from the last element to the first element, move backward one position, and empty the position you want to insert
    4. Inserts an element into the specified position
    5. Increase the sequential table length by 1

<span style= "Font-family:courier new;font-size:14px;" >template <class t>void seqlist<t>::insert (int i,t x) {    if (length>maxsize) throw "too long";    if (i<1| | i>=length+1) Throw "insertion position not present";    From backward to forward, move the array element backward until equal to I when for    (int j=length;j>=i;j--) {        data[j] = data[j-1];    }    Data[i-1] =x;    length++; Array length increment 1}</span>

How to delete a sequential table:

    1. Throws an exception if the table is empty
    2. Throws a position exception if the Delete element position does not exist
    3. Remove the element you want to delete and keep it (you can know which element is being deleted)
    4. Move the element i+1 to N in turn, moving one position forward
    5. Sequential table length minus 1, returning deleted elements

<span style= "Font-family:courier new;font-size:14px;" >template <class t>t seqlist<t>::D elete (int i) {     T n = data[i-1];//element to be deleted for    (int j=i;j< length;j++) {        data[j-1] = data[j];    }    length--;    return n;} </span>

get the elements and locations in the order table:

<span style= "Font-family:courier new;font-size:14px;" >template <class t>t seqlist<t>::get (int i) {    return data[i-1];} Template <class t>int seqlist<t>::locate (T x) {for    (int i=0;i<length;i++) {        if (data[i]==x) {            return i+1;        }    }    return 0;} </span>

Test:

<span style= "Font-family:courier new;font-size:14px;" >int Main () {    int a[6]={11,21,22,43,23,14};    Seqlist<int> list (a,6);    List. Printlist ();    List. Insert (2,5);    List. Printlist ();    int x = list. Delete (3);    cout<< "Delete element:" <<x<<endl;    List. Printlist ();    int i = list. Locate (a);    cout<< "22 is located in:" <<i<<endl;    return 0;} </span>

Results:



Data structure and algorithm--linear table sequential storage structure

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.