This article summarizes the C language implementation and operation of the sequence table method, for learning data structure of friends is a good reference program. The complete code is as follows:
#include <stdio.h> #include <stdlib.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define O
VERFLOW-2 #define LIST_INIT_SIZE #define LISTINCREMENT a typedef int status;
typedef int ELEMTYPE;
typedef struct{ELEMTYPE *elem;
int length,listsize;
}sqlist;
Status Initlist (SqList &l)/Initialize {l.elem= (Elemtype *) malloc (list_init_size*sizeof (elemtype)); if (!
L.elem) exit (OVERFLOW);
L.listsize=list_init_size;
l.length=0;
return OK;
The status build (SqList &l)//establishes the table {int i,n;
printf ("Please enter the number of elements N and n");
scanf ("%d", &n);
if (n>list_init_size)//if n is greater than the current space {l.elem= (Elemtype *) realloc (L.elem, (n+listincrement) *sizeof (elemtype)); if (!
L.elem) exit (OVERFLOW);
L.listsize=n+listincrement;
for (i=0;i<n;i++) scanf ("%d", l.elem+i);
L.length=n;
return OK;
} void Print (SqList &l)//output Table element and length {int i;
for (i=0;i<l.length;i++) printf ("%d", * (L.elem+i));
printf ("\ n Length:%d\n\n", l.length);
void tips ()//Prompt function {printf ("Please select the action you want: \ n"); Printf ("<1> output order table and the length of the sequential table \ n");
printf ("<2> delete node \ n");
printf ("<3> Delete node of the given location i \ n");
printf ("<4> reverse the order table \ n");
printf ("<5> sort the order table by ascending rank \ n");
printf ("<6> inserts x to the appropriate location of the order table \ n");
printf ("<7> merge two ordered tables \ n");
printf ("<0> exit \ n \ nthe");
Status ListDelete1 (sqlist &l,int x)//delete element {int i;
for (i=0;i<l.length;i++) if (* (l.elem+i) ==x) break;
if (i==l.length) return ERROR;
for (i++;i<l.length;i++) * (l.elem+i-1) =* (l.elem+i);
l.length--;
return OK;
Status ListDelete2 (sqlist &l,int x)//delete x element {int i; if (x<0| |
X>=l.length) return ERROR;
for (i=x+1;i<l.length;i++) * (l.elem+i-1) =* (l.elem+i);
l.length--;
return OK;
} void Inverse (SqList &l)//inverse function {int i,t;
for (i=0;i<l.length/2;i++) {t=* (l.elem+i);
* (l.elem+i) =* (l.elem+l.length-i-1);
* (l.elem+l.length-i-1) =t;
} void Sort (sqlist &l)//bubble sort (ascending) {int i,j,t;
for (i=1;i<l.length;i++) for (j=0;j<l.length-i;j++) {if (* (L.ELEM+J) >* (l.elem+j+1)) {t=* (L.ELEM+J);
* (L.ELEM+J) =* (l.elem+j+1);
* (l.elem+j+1) =t;
printf ("sorted in ascending order \ n");
The Status Listinsert (sqlist &l,int x)//Inserts the X into the still ordered {int i,k;
if (l.length>=l.listsize) {l.elem= (Elemtype *) realloc (L.elem, (l.listsize+listincrement) *sizeof (ElemType)); if (!
L.elem) exit (OVERFLOW);
L.listsize+=listincrement;
for (i=0;i<l.length;i++) if (x<* (l.elem+i)) a break;
K=i;
for (i=l.length;i>k;i--) * (l.elem+i) =* (l.elem+i-1);
* (l.elem+k) =x;
l.length++;
return OK;
Status merger (SqList &l,sqlist &lb)//merge two linear table {int i,j,k;
SqList Lc;
Initlist (LC); if (lc.listsize<l.length+lb.length) {lc.elem= (Elemtype *) realloc (Lc.elem, l.length+lb.length+listincrement) *
sizeof (Elemtype)); if (!
L.elem) exit (OVERFLOW);
Lc.listsize=l.length+lb.length+listincrement;
} i=j=k=0;
while (I<l.length && j<lb.length) {if (* (L.elem+i) < * (LB.ELEM+J)) {* (lc.elem+k) =* (l.elem+i);
k++;i++;
else {* (lc.elem+k) =* (LB.ELEM+J);
k++;j++;} while (I<l.length) {* (lc.elem+k) =* (l.elem+i);
k++;i++;
while (j<lb.length) {* (lc.elem+k) =* (LB.ELEM+J);
k++;j++;
} lc.length=l.length+lb.length;
L=LC;
return OK;
int main () {int op,x,flag;
SqList l,lb;
Initlist (L);
Build (L);
Tips ();
scanf ("%d", &op);
while (OP) {switch (OP) {case 1:print (L);
Break
Case 2:printf ("Please enter the data to be deleted x:\n");
scanf ("%d", &x);
Flag=listdelete1 (L,X); if (flag) printf (delete succeeded!!)
\ n '); else printf ("element does not exist, delete failed!!")
\ n ');
Break
Case 3:printf ("Please enter the location to delete i:\n");
scanf ("%d", &x); Flag=listdelete2 (l,x-1);//The Subscript for the I element corresponds to i-1 if (flag) printf (delete succeeded!!
\ n '); else printf ("element does not exist, delete failed!!")
\ n ');
Break
Case 4:inverse (L);
Break
Case 5:sort (L);
Break
Case 6:printf ("Please enter the data to be inserted x:\n");
scanf ("%d", &x);
Flag=listinsert (L,X); if (flag) printf ("Insert succeeded!!")
\ n '); else printf ("Insert failed!!")
\ n ');
Break
Case 7:printf ("Please enter the contents of lb: \ n");
Initlist (LB);
Build (LB);
Flag=merger (L,LB);if (flag) printf (merge succeeded!!)
\ n ');
Break
Tips ();
scanf ("%d", &op);
return 0; }