#include<stdio.h> #include<stdlib.h> typedef int elemType; #define lists 10 #define addlists 5 struct List{ elemType *list; int size; int maxsize; }; /*initialize table*/ void initList(struct List *l){ l->list = (elemType *)malloc(lists * sizeof(elemType)); if(!l->list) exit(1); l->size = 0; l->maxsize = lists; } /*insert elem*/ void insert(struct List *l, int i, elemType e){ if(i<1||i>l->size+1) exit(1); if(l->size == l->maxsize){ l->list = (elemType *)realloc(l->list,(lists+addlists)*sizeof(elemType)); if(!l->list) exit(1); l->maxsize += addlists; } int *p; int *q; q =&(l->list[i-1]); for(p =&(l->list[l->size-1]);p>=q;--p) *(p+1) = *p; *q = e; ++l->size; } int main(){ struct List l; initList(&l); insert(&l,1,7); printf("操作成功%d\n",l.list[0]); }