Sequential storage structure of linear tables

Source: Internet
Author: User

The sequential storage structure of 1 linear table is the data element of the linear table stored sequentially by a contiguous storage unit. Since each data element type of the linear table is the same, a one-dimensional array can be used to implement the sequential storage structure, that is, the first element is stored in the position of subscript 0, and then the elements adjacent to the linear table are stored in the adjacent position of the array.
2 structure code for linear table sequential storage
#define MAXSIZE 20//Storage space Initial allocation
typedef int ELEMTYPE;//ELEMTYPE Represents a type, here is assumed to be an int
typedef struct {
Elemtype data[maxsize];//Array stores data elements with a maximum value of MAXSIZE
int length;//Linear Table current length
} sqlist;
Description: A description of the sequential storage structure requires three properties
(1) The starting position of the storage space: array data, where storage space is stored.
(2) Maximum storage capacity of linear table: array length MaxSize
(3) Current length of linear table: length

3 insertion and deletion of sequential storage structures

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int STATUS;

(1) Return the value of the I data element in the array L with E
Status Getelem (sqlist L, int i, Elemtype *e) {
if (L.length < 0 | | I < 0 | | i > L.length) {
return ERROR;
}
*e = L.data[i-1];
return OK;
}
The time complexity is O (1).

(2) inserting element e
/* Initial conditions in linear table I: sequential linear table L already exists, I has a value range of 1 <= i <= l->length + 1 */
/* Operation result: Insert the length of the e,l before the I position in L plus 1 */< Br>status Listinsert (sqlist *l, int i, elemtype e) {
int k;
if (l->length = = MAXSIZE) {//Linear table full
return ERROR;
}
if (I < 1 | | i > l->length + 1) {//inserted position not in range
return ERROR;
}
if (i <= l-> length) {//is not inserted at the end of the queue
for (k = l-> length-1; k >= i; I-) {
L->data[k + 1] = L->d ATA[K];
}
}
L->data[i-1] = e;
L->length + +;
return OK;
}

(3) Delete the element of position I and return its value with E
/* Initial conditions: Sequential linear table L already exists, I has a value range of 1 <= i <= l->length */
/* Operation Result: Delete l data element, L's length minus 1 */
Status Listdelete (sqlist *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 (I < l->length) {//Not delete the tail element
for (k = i; k <= l->length-1; i++) {
L->DATA[K-1] = l->data[k];
}
}
L->length--;
return OK;
}

(4) Complexity of time
If you insert to the last position, or delete the last element, the time complexity is O (1). The worst case scenario is inserting to the first position, or deleting the first element, at which time the complexity is O (n). The average time is (n-1)/2. The time complexity is O (n).

Sequential storage structure of linear tables

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.