Sequence Table Implementation (C language)
# Include
# Include
# Define MAXSIZE 100 struct List {int element [MAXSIZE]; // int len; // length of the current linked List} * t; void print (List * t ); int Isemtty (List * t) // determines whether the linked List is empty {if (t-> len = 0) return 0; elsereturn-1;} void getElement (List * t, int I) // obtain the I-th element of the table (subscript starts from 0) {if (I <0 | I> t-> len) printf ("fail! \ N "); elseprintf (" % d \ n ", t-> element [I]);} void add (List * t) // Add the node {int I, j; printf ("input the count of new numner:"); scanf ("% d", & j ); printf ("input new numbers: \ n"); for (I = 0; I
Element [I]); t-> len ++;} printf ("added successfully! \ N ") ;}void insert (List * t) {int I, j, x; printf (" input the value of I and x :"); // insert a node scanf ("% d", & I, & x) before node I in the table ); if (I <= 0 | I> t-> len | t-> len = MAXSIZE) // The pluggable condition printf ("can not insret! \ N "); else {for (j = t-> len; j> = I; j --) {t-> element [j] = t-> element [J-1]; // All nodes are moved back from the insertion position} t-> element [I] = x; t-> len ++ ;}} void Delete (List * t, int I) // Delete the I node in the table {int j; if (I <0 | I >= t-> len) printf ("can not delete! \ N "); else {for (j = I; j
Len; j ++) t-> element [j] = t-> element [j + 1]; // starting from the deleted location, all nodes are moved forward from t-> len -- ;}} void clear (List * t) // clear the linked List {t-> len = 0 ;} void modify (List * t, int I) // modify node {int B; printf ("intut a number:"); scanf ("% d ", & B); if (I <0 | I> t-> len) printf ("can not modify! \ N "); elset-> element [I] = B;} void show (List * t) // display data of all nodes in the linked List {int I = 0; for (; I
Len; I ++) printf ("% 2d", t-> element [I]); printf ("\ n");} int main () {struct List * p; p = new List; p-> len = 0; int ch, I, x; add (p); while (1) {printf ("************************************ * ***************** \ n "); printf ("1. judge 2. getelement 3.add 4. delete \ n 5. insert 6. show 7. modify 8. clear 0. quit \ n "); printf ("************************************* * **************** \ n "); printf ("please input your choice:"); scanf ("% d", & c H); switch (ch) {case 1: printf ("% d \ n", Isemtty (p); break; case 2: {printf ("input a number: "); scanf (" % d ", & I); getElement (p, I); break;} case 3: add (p); break; case 4: {printf ("input value of I:"); scanf ("% d", & I); Delete (p, I); break;} case 5: insert (p); break; case 6: show (p); break; case 7: {printf ("input value of I:"); scanf ("% d ", & I); modify (p, I); break;} case 8: clear (p); break; case 0: exit (0); default: pri Ntf ("your input is incorrect! Enter "); break;} return 0;} again ;}