1 1. Array definition: 2 3typedefstructSqList4 {5 elemtype list[maxsize];6 intsize;7 } sqlist;8 9 2. Pointer definition: Ten OnetypedefstructSqList A { -Elemtype *Elem; - intlength; the intlistsize; - } sqlist; - - 3. Complete code + -#include <stdio.h> +#include <malloc.h> A#include <process.h> at - #defineList_init_size 100 - #defineListincrement 10 - - #defineOK 1 - #defineERROR 0 in #defineOVERFLOW-2 - totypedefintStatus; +typedefintElemtype; - thetypedefstruct * { $Elemtype *Elem;Panax Notoginseng intlength; - intlistsize; the } sqlist; + A //Create an empty order table theStatus initlist_sq (SqList &L) + { -L.elem = (elemtype*)malloc(list_init_size*sizeof(Elemtype)); $ if(!L.elem) $ exit (OVERFLOW); -L.length =0; -L.listsize =list_init_size; the returnOK; - }Wuyi the //sequential tables Insert E before the first element -Status listinsert_sq (SqList &l,intI, elemtype e) Wu { -Elemtype *newbase,*q,*p; About if(i<1|| i>l.length+1)//illegal insertion position $ returnERROR; - - if(l.length>=l.listsize)//overflow, dynamically append space - { ANewbase= (Elemtype *)realloc(L.elem, (l.listsize+ listincrement) *sizeof(Elemtype)); + if(!newbase) exit (OVERFLOW); thel.elem=newbase; -l.listsize+=listincrement; $ } theq=& (l.elem[i-1]); the for(p=& (l.elem[l.length-1]); p>=q; p--)//element Move back the* (p+1)=*p; the*q=e;//Complete Element Insertion -++l.length; in return(OK); the } the About the //Sequential table traversal display the Status listtraverse_sq (sqlist L) the { + intI=0; - if(!L.elem) the returnERROR;Bayi while(i<l.length) theprintf"%d", l.elem[i++]); theprintf"\ n"); - returnOK; - } the //Sequential table Inversion the voidREVERSE_SQ (SqList &L) the { the inti,j; - Elemtype temp; the for(i=0, j=l.length-1; i<j; i++,j--) the { thetemp=L.elem[i];94l.elem[i]=L.elem[j]; thel.elem[j]=temp; the } the }98 intMain () About { - sqlist L;101 CharFlag;102 inti;103 elemtype e;104 if(INITLIST_SQ (L) = =OK) the {106printf"Build Empty Order table success! \ n");107 Do108 {109printf"the current linear table length is:%d\n", l.length); theprintf"Please enter the location where you want to insert the element:");111scanf"%d",&i); theprintf"Please enter the value of the element to insert:");113scanf"%d",&e); the if(LISTINSERT_SQ (l,i,e) = =OK) the { theprintf"Insert succeeded, insert post-order table length:%d\n", l.length);117printf"the order table after insertion is:");118 listtraverse_sq (L);119 } - Else121printf"Insert Failed");122printf"\ nyou continue inserting elements? (y/n)");123 fflush (stdin);124scanf"%c",&flag); the }126 while(flag=='y');127 - reverse_sq (L);129printf"The order table is reversed after: \ n"); the listtraverse_sq (L);131 } the Else133printf"Sequential table initialization failed! \ n");134}
Sequential storage of linear tables