鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > c语言 > >

[c/c++] C数据结构: 顺序表 Sequence List

来源:互联网 作者:佚名 时间:2012-11-22 15:02
#include MAXSIZE 10 5 6 typedef int ElemType; 7 typedef struct { 8 int * elem; 9 int length; 10 int listsize; 11 } Sqlist; initSqlist(Sqlist * L) { 16 L-elem = ( int *) malloc(MAXSIZE * sizeof (ElemType)); 17 if (!L-elem) exit( 0 ); 18 L-l

#include MAXSIZE 10 5 6 typedef int ElemType; 7 typedef struct { 8 int *elem; 9 int length; 10 int listsize; 11 } Sqlist; initSqlist(Sqlist *L) { 16 L->elem = (int *) malloc(MAXSIZE * sizeof(ElemType)); 17 if(!L->elem) exit(0); 18 L->length = 0; 19 L->listsize = MAXSIZE; 20 } insertSqlistElem(Sqlist *L,int i,ElemType item) { 25 ElemType *base,*insertPtr,*p; (i < (L->length >= L->listsize) { = (ElemType *) realloc(L->elem,(L->listsize + 10) * sizeof(ElemType)); 31 L->elem = base; 32 L->listsize = L->listsize + 10; 33 } 34 insertPtr = &(L->elem[i-1]); // 要插入的元素的启始位置 (p = &L->elem[L->length - 1]; p >= insertPtr; p--) { 37 *(p + 1) = *p; 38 } 39 *insertPtr = item; 40 L->length++; 41 } deleteSqlistElem(Sqlist *L,int i) { 46 ElemType *deleteElem,*q; (i < 1 || i > L->length) exit(0); 49 50 deleteElem = &(L->elem[i - 1]); 51 q = L->elem + L->length - 1; (++deleteElem; deleteElem <= q; ++deleteElem) *(deleteElem - 1) = *deleteElem; 54 L->length--; 55 } printSqlist(Sqlist *L) { 60 int i; ,L->elem[i]); ); 63 } main() { 67 Sqlist sqlist; 68 int i; 69 70 initSqlist(&sqlist); 71 for(i = 0; i < 15; i++) insertSqlistElem(&sqlist, i+1,i+1); ); 73 printSqlist(&sqlist); 74 75 deleteSqlistElem(&sqlist,5); ); 77 printSqlist(&sqlist); ; 80 }

美国服务器香港服务器服务器空间
网友评论
<