Analysis of data structure (iii): Linear table __ Data structure

Source: Internet
Author: User
Tags array length
> Software programming > Other Related > Analysis of data structure (III.): Linear table2016-01-11 11:49 Source: Qing Screen Network popularity: 155 Comments (0) Preface

What is a linear table.

What are the two main storage structures of a linear table?

How the various storage structures implement access, insert deletes, and so on.

This article mainly answers these questions, interested in the words together to see what is a linear table.

Linear tables can literally imagine the style of a line, so what is a linear table? Here's a definition: a finite sequence of 0 or more data elements is called a linear table (list). Elements are ordered, and if there are multiple elements, the first element has no precursors, the last element has no successor, and each of the other elements has only one predecessor and successor. sequential storage structure of linear tables

Diagram of sequential storage of linear table

Diagram of sequential storage of linear table

The sequential storage structure of linear table is one of the physical structure of linear table, which refers to storing the data elements of the linear table sequentially with a continuous memory cell. 1, sequential storage needs.

Describes the three attributes required for a sequential storage structure: The starting position of the storage space: the storage location of the array data is the storage location of the storage space; The maximum storage capacity of the linear table: array length MaxSize; The current length of the linear table:

#define MAXSIZE 20/* Storage space Initial allocation
/typedef int ELEMTYPE/* Elemtype type depending on the actual, here for the INT/
typedef struct
{
    Elemtype Data[maxsize]; /* Array to store the data element, the maximum value is MAXSIZE *
    /int length;
}  SqList;
here the array data length differs from the linear table length.

The length of the array is known at the beginning of the creation, and this amount is generally unchanged;

The linear table length is the number of data elements in the linear table, which will change with the insertion and deletion of the linear table.

At any moment, the linear table length is less than or equal to the length of the array. insertion and deletion of sequential storage structure 1. Get the element

For the sequential storage structure of the linear table, if we want to implement the Getelem operation, we simply return the element value in the first position of the linear table L.

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;  /* Status value is the result state of the function, such as OK

///////////////////////////////////////////////////////
* *
Status Getelem (sqlist L; int i, Elemtype *e)
{
    if (l.length = 0 | | I < 1 | | i > l.length)
        retur n ERROR;
    * e = l.data[i-1];

    return OK;
}
2. Insert new element

If we want to implement Listin-sert (*l, I, E), that is how to insert the new element E in the first position in the linear table L. The insertion algorithm is used here. Insert element process Insert algorithm idea: If the insertion position is unreasonable, throw an exception; if the linear table length >= array length, throws an exception or dynamically increases capacity; Moves forward from the last element to the first position, moving it back one position, respectively; You will insert the element into position I, linear table length +1.

T (sqlist *l, int i, elemtype e)
{
    int k;

    /* Sequential linear table is full *
    /if (l->length = = MAXSIZE) return 
        ERROR;

    /* When I is not in range
    /if (I < 1 | | | i > l-> length + 1) return 
        ERROR;

    /* If the insertion data position is not at the end of the table
    /if (i <= l->length) 
    {/* To move the
        data element backward after the insertion position a
        /for (k = l->length-1; k > = I-1; k--) 
            l->data[k + 1] = l->data[k];
    }

    L->data[i-1] = e; /* Inserts the new element into the */
    l->length++;
    return OK;
}
3, delete elementsTo delete an element procedure Delete algorithm ideas:If the deletion position is unreasonable, throws an exception, takes out the deletion element, moves from the location of the deletion element to the last element position, moving it forward one position respectively; linear table length-1.
/* Initial condition: sequential linear table L already exist, 1≤i≤
   listlength (L)///////////////////////////////////////////////////
Qlist *l, int i, elemtype *e)
{
    int k;
    if (L->length = = 0) return
        ERROR;
    if (I < 1 | | | i > l->length) return
        ERROR;
    *e = l->data[i-1];
    /* If deletion is not the last position *
    /if (I < l->length)
    {/
        * will delete position successor element forward * * for
        (k = i; k < l->length; k++) 
  l->data[k-1] = l->data[k];
    }
    l->length--;
    return OK;
}
sequential storage structure of linear tables time complexityWhen data elements are stored and fetched, the time complexity is O (1) at any location, and the (average) time complexity is O (n) when inserted and deleted. sequential storage structure pros and consExcellent: fast access to any position in the table elements; Missing: Insert, delete operations need to move a large number of elements; When the length of the linear table changes greatly, the storage space can not be determined and the storage space is wasted. chain-type storage structure of linear table

In a sequential structure, each data element requires only the information of the data element to be stored. and chain structure needs to store two kinds of data-data field, pointer field, two parts of data elements, called nodes.

Data fields are used to store data element information;

A pointer field is used to store a direct successor address.

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.