Sequential storage structure of linear table (C language Source code implementation),
#include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h" #define OK 1 #defi NE ERROR 0 #define TRUE 1 #define FALSE 0 #define MAXSIZE 20/* Storage space Initial allocation * * typedef int STATUS; /* Status is the type of function whose value is the function result status code, such as OK/typedef int ELEMTYPE;
/* Elemtype type according to the actual situation, here is assumed to be int/Status visit (elemtype c) {printf ("%d", c);
return OK; typedef struct {elemtype data[maxsize]; /* Array, storing the data element */int length;
/* Linear Table Current length * *}sqlist;
/* Initialization order Linear table/Status initlist (sqlist *l) {l->length = 0;
return OK; }/* Initial condition: sequential linear table L already exists.
Operation Result: If L is an empty table, returns True, otherwise returns false */Status Listempty (sqlist L) {if (l.length = = 0) return true;
else return FALSE; }/* Initial condition: sequential linear table L already exists.
Action Result: Reset L to Empty table/Status clearlist (sqlist *l) {l->length = 0;
return OK; }/* Initial condition: sequential linear table L already exists.
Operation Result: Returns the number of data elements in L/int listlength (SqList L) {return l.length;} /* Initial conditions: Sequential linear table L already exist, 1≤i≤listlength (L)////////////////////////////////////////////The array to be placed is starting from 0/Status Getelem (sqlist L, int i, Elemtype *e) {if (l.length = 0 | | i<1 | | i>l.length) return ERROR;
*e = L.data[i-1];
return OK;
* * * If such data element does not exist, then the return value is 0/int Locateelem (sqlist L, elemtype e) {int i;
if (L.length = = 0) return 0;
for (i = 0;i<l.length;i++) {if (l.data[i] = e) break;
} if (I >= l.length) return 0;
return i + 1; }/* Initial conditions: Sequential linear table L already exist, 1≤i≤listlength (L),////////////////////////////////////////////////////
Type e) {int k;
if (l->length = = MAXSIZE)/* Sequential linear table is full/return ERROR;
if (i<1 | | i>l->length + 1)/* When I is smaller than the first position or more than the last position in the latter position is larger when the return error; if (i <= l->length)/* Insert data position not at footer/{for (k = l->length-1;k >= i-1;k--)/* Move backward data element after insertion position
One */l->data[k + 1] = l->data[k]; } l->data[i-1] = e;
/* Inserts the new element into the */l->length++;
return OK; }/* Initial conditions: Sequential linear table L already exists, 1≤i≤lisTlength (L) *///////////////////////////////////////////////////////////////
if (L->length = = 0)/* Linear table IS null/return ERROR;
if (i<1 | | i>l->length)/* Delete location incorrect/return ERROR;
*e = l->data[i-1]; if (i<l->length)/* If deletion is not the last position */{for (k = i;k<l->length;k++)/* will delete position successor element forward * * L->da
TA[K-1] = l->data[k];
} l->length--;
return OK;
for (i = 0;i<l.length;i++) visit (L.data[i]);
printf ("\ n");
return OK;
} void Unionl (SqList *la, sqlist Lb) {int La_len, lb_len, I;
Elemtype e;
La_len = Listlength (*la);
Lb_len = Listlength (Lb);
for (i = 1;i <= lb_len;i++) {Getelem (Lb, I, &e); if (!
Locateelem (*la, E)) Listinsert (La, ++la_len, E);
int main () {sqlist L;
Elemtype e;
Status i;
Int J, K;
i = Initlist (&l); printf (after "Initialize L: l.length=%d\n", L.length);
for (j = 1;j <= 5;j++) i = Listinsert (&l, 1, j);
printf ("After the header of L is inserted 1~5: L.data=");
Listtraverse (L);
printf ("l.length=%d \", l.length);
i = Listempty (L);
printf ("L" is null: i=%d (1: is 0: no) \ n ", i);
i = Clearlist (&l);
printf ("Empty L: l.length=%d\n", l.length);
i = Listempty (L);
printf ("L" is null: i=%d (1: is 0: no) \ n ", i);
for (j = 1;j <= 10;j++) Listinsert (&l, J, J);
printf ("After the tail of the L is inserted 1~10: L.data=");
Listtraverse (L);
printf ("l.length=%d \", l.length);
Listinsert (&l, 1, 0);
printf ("Insert 0 after the header of L: L.data=");
Listtraverse (L);
printf ("l.length=%d \", l.length);
Getelem (L, 5, &e);
printf ("The value of the 5th element is:%d\n", e);
for (j = 3;j <= 4;j++) {k = Locateelem (L, J);
if (k) printf ("The value of the%d element is%d\n", K, J);
else printf ("no element with%d value \ n", j); } k = Listlength (L); /* k is the table length/for (j = k + 1;j >= k;j--) {i = Listdelete (&l, J, &e);/* Delete the J data/if (i = = ERROR) print
F ("Delete%d data failed \ n", j);
else printf ("Delete the%d element value:%d\n", J, E); printf("Output The elements of L:");
Listtraverse (L);
j = 5; Listdelete (&l, J, &e);
/* Delete the 5th data */printf ("Delete the%d element value:%d\n", J, E);
printf ("Output the elements of L in turn:");
Listtraverse (L);
Construct a 10-digit lb sqlist lb;
i = Initlist (&lb);
for (j = 6;j <= 15;j++) i = Listinsert (&lb, 1, j);
Unionl (&l, Lb);
printf ("sequentially outputs the elements that combine the L of lb:");
Listtraverse (L);
System ("pause");
return 0;
}