#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct seqlistNode { int date; }seqlistNode; typedef struct SEQLIST { unsigned int lengh; //长度 unsigned int count; //已存元素长度 seqlistNode * date; //数据域 }seqlist; seqlist * Creatlist(int count) { seqlist * s=NULL; if(count==0) return NULL; s=(seqlist *)malloc(sizeof(seqlist)+count*sizeof(seqlistNode)); //动态开辟内存 memset(s,0,sizeof(seqlist)+count*sizeof(seqlistNode)); //自己管理 s->date=(seqlistNode *)(s+1); s->lengh=0; s->count=count; return s; } int insertList(seqlist *s,seqlistNode n,unsigned pos) { unsigned i; if(s==NULL) return -1; if(pos > (s->count-1) )return -2; if(s->lengh==s->count) return -3; if(pos > s->lengh) pos=s->lengh; for(i=s->lengh;i!=pos;i--) s->date[i].date=s->date[i-1].date; s->date[pos].date=n.date; s->lengh+=1; return 0; } int dellist(seqlist *s,unsigned pos) { unsigned int i; if(s==NULL) return -1; if(pos>s->lengh) return -2; for(i=pos;i<s->lengh-1;i++) s->date[i]=s->date[i+1]; s->lengh--; return 0; } void destroylist(seqlist* list) { if(list!=NULL) free(list); } void display(seqlist *s) { unsigned i; for(i=0;i<s->lengh;i++) { printf("%d ",s->date[i].date); } printf("\n"); } int main() { seqlist *list=NULL; unsigned i=0; list=Creatlist(10); for(i;i<list->count;i++) { seqlistNode n; n.date=i; insertList(list,n,0); } display(list); for(i=0;i<list->count;i++) { dellist(list,0); display(list); } destroylist(list); return 0; }
运行结果:
时间: 2024-10-11 02:52:46