Header file
/** Sequential representation and implementation of linear tables*/#ifndef Sqlist_h#defineSqlist_h#include"Sqlist.h"#defineTRUE 1#defineFALSE 0#defineERROR-1#defineList_size 10#defineList_increase 2typedefintStatus;typedefintElemtype;typedefstruct{elemtype*Elem; intlength; intsize;} SqList; Status isequal (elemtype lhs, elemtype RHS); Status initlist (SqList&L); Status Insertelem (SqList&l,intPos, elemtype elem); Status Deleteelem (SqList&l,intPOS, Elemtype *elem); Status Getelem (SqList&l,intPOS, Elemtype *elem); Status unionlist (sqlist srcL1, SqList srcL2, SqList&desl); Status destroylist (SqList&m); Status clearlist (SqList&m); Status listempty (SqList L);intgetlistlength (SqList L);voidprintlist (SqList L);intLocateelem (SqList L, Elemtype E, Status (*Compare) (Elemtype, Elemtype));#endif
source file
#include <stdio.h>#include<stdlib.h>#include"SqList.h"Status isequal (elemtype lhs, elemtype rhs) {if(LHS = =RHS) { returnTRUE; } returnFALSE;} Status initlist (SqList&m) {L.elem= (Elemtype *) malloc (sizeof(elemtype) *list_size); if(!L.elem) {returnFALSE; } l.length=0; L.size=list_size; returnTRUE;} Status Insertelem (SqList&l,intPos, Elemtype elem) { if(Pos <1|| POS > l.length+1) { returnFALSE; } //Judging capacity if(L.length >=l.size) {elemtype*pelemnew = (Elemtype *) realloc (L.elem,sizeof(elemtype) * (L.size +list_increase)); if(!pelemnew) { returnERROR; } L.elem=pelemnew; L.size= L.size +list_increase; } elemtype*pelem = L.elem + pos-1; *pelem =Elem; for(Elemtype *p = L.elem + L.length-1; P >= L.elem + pos; --p) {*p++ = *p; } l.length++; returnTRUE;} Status Deleteelem (SqList&l,intPOS, Elemtype *elem) { if(Pos <1|| POS >l.length) {returnFALSE; } elemtype*pelem = L.elem + pos-1; Elem=Pelem; for(Elemtype *p =l.elem+ pos; p< l.elem+l.length; p++) { * (P-1)= *p; } l.length--; returnTRUE;} Status Getelem (SqList&l,intPOS, Elemtype *elem) { if(Pos <1|| POS >l.length) {returnFALSE; } *elem = * (L.elem + pos-1); returnTRUE;} Status unionlist (sqlist srcL1, SqList srcL2, SqList&Desl) { for(inti =0; i < srcl1.length; i++) {Insertelem (Desl, Desl.length+1, * (srcl1.elem+i)); } for(inti =0; i < srcl2.length; i++) {Insertelem (Desl, Desl.length+1, * (Srcl2.elem +i)); } returnTRUE;} Status destroylist (SqList&L) {free (L.elem); L.elem=NULL; L.length= L.size =0; returnTRUE;} Status clearlist (SqList&m) {L.elem=NULL; L.length=0; L.size=list_size; returnTRUE;} Status listempty (sqlist L) {if(L.length >0) { returnFALSE; } returnTRUE;}intgetlistlength (sqlist L) {returnl.length;}voidprintlist (sqlist L) { for(inti =0; i < l.length; i++) {printf ("%d", * (l.elem+i)); }}intLocateelem (SqList L, Elemtype E, Status (*Compare) (Elemtype, Elemtype)) {Elemtype*pelem =L.elem; inti =1; while(I <= l.length &&!compare (*pelem++, E)) { ++i; } if(I <l.length)returni; Else return 0;}
Sequential implementation of linear tables