Insertion and deletion of sequential storage structures

Source: Internet
Author: User

1 Getting the operation of the element
To obtain the value of element I in a linear table, the value of I is returned to the i-1 under the subscript range of the array.

#define OK 1
#define ERROR 0
#define FALSE 0
#define TRUE 1

typedef int STATUS;//STATUS is the type of the function, whose value is the function result status code

Status Getelem (sqlist L, int  i, elemtype *e) {
    if (l.length==0 | | i<1 | | i>l.length) The rational interpretation
    of//i return ERROR;
    *E=L.DATA[I-1];
    return OK;
}

2 inserting elements
The idea of inserting an algorithm
If the insertion position is unreasonable, throw an exception;
If the length of the linear table is greater than or equal to the length of the array, throw an exception or increase capacity;
Move forward from the last element to the first position, moving them backward one position;
The element to be inserted is filled in at position I;
Table length plus 1;
Note: The insertion position I refers to the position of the linear table, the range is 1 to length, moving the element is considered as an array of storage, so it starts from 0 to Length-1

Status Lstinsert (sqlist *l, int  i, elemtype e) {
    if (l->length >=maxsize)
     return ERROR;
    if (i<1 | | i>l->length+1)
    return ERROR;
    if (i<=l->length) {for
        (k=l->length-1;k>=i-1;k--)
        l->data[k+1]=l->data[k];
    }
    l->data[i-1]=e;
    l->length++;//table length plus 1
    return OK;

}

3 Delete operations
If the location of the deletion is unreasonable, throw an exception;
Remove the deleted elements, move them forward one position from the point of deletion to the last element, respectively;
Table length minus 1;

Status Lstdelete (sqlist *l, int  i, elemtype elete*e) {
    if (l->length ==0)
    return ERROR;
    if (i<1 | | i> l->length)
    return ERROR;
    *e=l->data[i-1];
    if (i<l->length) {for
        (int k=i;k<l->length;k++)
        l->data[k-1]=l->data[k];
    }
    l->length--;
    return OK;
}

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.