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;
}