The storage address calculation formula of the sequence table node is as follows:
Storage location of data element I: LOC (AI) = LOC (AI) + (I-1) * l; 1 ≤ I ≤ n (L indicates that each element occupies 1 storage unit)
The relationship between the storage location LOC (AI + 1) of the (I + 1) data elements and the storage location LOC (AI) of the I data elements: LOC (AI + 1) = LOC (AI) + L;
Array:
#define OK 1#define ERROR -1#define MAX_SIZE 100typedef int Status ;typedef int ElemType ; typedef struct sqlist{ ElemType elem_array[MAX_SIZE] ; int length ;} SqList ;
1. Sequence linear table Initialization
Status Init_SqList( SqList *L ) { L->elem_array=( ElemType * )malloc(MAX_SIZE*sizeof( ElemType ) ) ; if ( !L -> elem_array ) return ERROR ; L->length= 0 ; return OK ; }
2. Insert a sequence linear table --- implementation step (einsert = n/2, the average time complexity is O (n ))
/*
(1) Move the number I in the linear table L to the position after the nth node.
(2) Insert node e to the node ai-1.
(3) linear table length plus 1
*/
Status insert_sqlist (sqlist * l, int I, elemtype e) {Int J; if (I <1 | I> L-> Length + 1) return error; // valid I value: 1 to Length + 1 If (L-> length> = max_size) {printf ("linear table overflow! \ N "); Return Error;} For (j = L-> length-1; j> = I-1; -- J) l-> elem_array [J + 1] = L-> elem_array [J];/* Move all nodes after I-1 position */L-> elem_array [I-1] = E; /* Insert the node in the I-1 position */L-> length ++; Return OK ;}
3. delete an ordered linear table (einsert = (n-1)/2, and the average time complexity is O (n ))
/*
(1) Move the I + 1 to the N node in the linear table L to a position forward accordingly.
(2) The length of a linear table is reduced by 1.
*/
Elemtype delete_sqlist (sqlist * l, int I) {int K; elemtype X; If (L-> length = 0) {printf ("linear table L is empty! \ N "); Return Error;} if (I <1 | I> L-> length) {printf (" the data element to be deleted does not exist! \ N "); Return Error;} X = L-> elem_array [I-1];/* Save the node value */For (k = I; k <L-> length; k ++) L-> elem_array [k-1] = L-> elem_array [k];/* forward all nodes after I position */L-> length --; return (x );}
4. Locate and delete ordered linear tables --- implementation steps (ecompare = (n + 1)/2, edelete = (n-1)/2, average time complexity: ecompare + edelete = n, O (n ))
/*
(1) Search for the first data element whose value is X in the linear table L.
(2) move one position forward from the found position to the last node.
(3) The length of a linear table is reduced by 1.
*/
Status locate_delete_sqlist (sqlist * l, elemtype X)/* Delete the first node where the value of L in a linear table is x */{int I = 0, K; while (I <L-> length)/* Find the first node of x */{If (L-> elem_array [I]! = X) I ++; else {for (k = I + 1; k <L-> length; k ++) l-> elem_array [k-1] = L-> elem_array [k]; L-> length --; break;} if (I> L-> length) {printf ("the data element to be deleted does not exist! \ N "); Return Error;} Return OK ;}
Dynamic Array:
Typedef struct {elemtype * ELEM; // The base address of the bucket int length; // The current length int listsize; // The currently allocated storage capacity (in sizeof (elemtype )} sqlist;
Create an ordered table (array ):
Status Init (sqlist & L) {L. ELEM = (elemtype *) malloc (initial length * sizeof (elemtype); If (! L. ELEM) Exit (0); L. Length = 0; L. listsize = initial length; Return OK ;}
Insert:
Status insertlist (sqlist * l, int I, elemtype e) // Insert the element {if (I <1 | I> L. length + 1) // if the following table is 0, it is used as a sentinel without data. Return Error; If (L. length> = L. listsize) // the bucket is full and the allocated space {newbase = (elemtype *) realloc (L. ELEM, (L. listsize + increment) * sizeof (elemtype); // L. location of the original ELEM pointer if (! Newbase) // storage allocation failed exit (0); L. ELEM = newbase; // new base address L. listsize + = increment; // increase storage capacity} q = & (L. ELEM [I-1]); // P Insert Location for (P = & (L. ELEM [length-1]); P> = Q; -- p) * (p + 1) = * P; // move the pointer * q = E; // insert element ++ L. length; // Add Return OK to the table length ;}
Delete:
Status deletlist (sqlist * l, int I, elemtype e) {if (I <1 | I> L. return Error; P = & (L. ELEM [I-1]); // Delete the element's location E = * P; // Delete the element's value q = L. ELEM + L. ELEM + Llength-1; // the position at the end of the table for (+ + P; P <= Q; + + p) * (PM) = * P; // shifts left of the element -- l. length; // reduce the table length by one Return OK ;}
Sequence Table-array