1#include <stdio.h>2 3 #defineMazsize 204typedefintElemtype;5typedefstruct {6 elemtype data[mazsize];7 intlength;8 }arrlist;9 Ten #defineOK 1 One #defineERROR 0 A #defineTrue 1; - #defineFalse 0; -typedefintStatus; the -Status initlist (Arrlist *L) - { -L->length =0; + returnOK; - } + A Status listempty (arrlist L) at { - if(L.length = =0) - return true; - - return false; - } in -Status clearlist (Arrlist *L) to { +L->length =0; - the returnOK; * } $ Panax NotoginsengStatus Getelem (Arrlist L,intI, Elemtype *e) - { the if(I > L.length | | i <1|| l.length==0) + returnERROR; A*e = l.data[i-1]; the + returnOK; - } $ $ Status Locateelem (arrlist L, elemtype e) - { - if(L.length = =0) the returnERROR; - inti;Wuyi for(i =0; i < l.length; i++) the { - if(L.data[i] = =e) Wu Break; - } About if(I >=l.length) $ returnERROR; - returni +1; - } - AStatus Listinsert (Arrlist *l,intI, elemtype e) + { the if(L->length = =mazsize) - returnERROR; $ if((i<1) || (I> l->length+1)) the returnERROR; the intJ; the if(I <= l->length) the { - for(j = l->length; J > I-1; j--) in { theL->DATA[J] = l->data[j-1]; the } About } thel->data[i-1] =e; thel->length++; the returnOK; + } - theStatus Listdelete (Arrlist *l,intI, Elemtype *e)Bayi { the if(L->length = =0) the returnERROR; - if((i<1) && (i>l->length)) - returnERROR; the intJ; the*e = L->data[i-1]; the if(I < l->length) the { - for(j = i-1; J < l->length; J + +) the { theL->DATA[J] = l->data[j +1]; the }94 } thel->length--; the returnOK; the }98 About intListlength (Arrlist *L) - {101 returnL->length;102 }103 104Status createlist (Arrlist *L) the {106 inti;107 for(i =0; i<mazsize; i++)108 {109L->data[i] = i +1; thel->length++;111 } the returnOK;113 } the voidTraverselist (ConstArrlist *L) the { the inti;117 for(i =0; I < l->length; i++)118printf"%d", l->data[i]);119printf"\ n");
}
Linear table implementation--array implementation