1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include "SeqList.h"
SeqList SeqListInit() { struct SeqList newSqList; newSqList.p=NULL; newSqList.size=newSqList.capacity=0; return newSqList; }
void checkCapacity(SeqList* ps) { if(ps->size==ps->capacity) { int newCapacity = !ps->capacity?4:ps->capacity*2; SeqDataType* newP=(SeqDataType*)realloc(ps->p, newCapacity*sizeof(SeqDataType)); if(newP!=NULL) { ps->p=newP; ps->capacity=newCapacity; } else { printf("realloc failed"); assert(-1); }
} }
void SeqListDestroy(SeqList* ps) { free(ps->p); ps->p=NULL; ps->size=ps->capacity=0; }
void SeqListtraverse (SeqList* ps) { for(int i=0; i<ps->size; i++) { printf("%d->",ps->p[i]); } printf("end size:%d capacity%d\n", ps->size, ps->capacity); }
void SeqListPushBack(SeqList* ps, SeqDataType x) { checkCapacity(ps); ps->p[ps->size++]=x; }
void SeqListPopBack(SeqList* ps) { if(ps->size==0) { printf("SeqList is empty!"); assert(-1); } ps->size--; }
void SeqListPushFront(SeqList* ps, SeqDataType x) { checkCapacity(ps); int end = ps->size++; while(end>0) { ps->p[end]=ps->p[end-1]; end--; } ps->p[0]=x; }
void SeqListPopFront(SeqList* ps) { if(ps->size==0) { printf("SeqList is empty!"); assert(0); } for(int i=0; i<ps->size-1; i++) { ps->p[i]=ps->p[i+1]; } ps->size--; }
int SeqListFind(SeqList* ps, SeqDataType x) { for(int i=0; i<ps->size; i++) { if(ps->p[i]==x) { return i; } } return -1; }
void SeqListInsert(SeqList* ps, int pos, SeqDataType x) { checkCapacity(ps); if(pos<0 || pos>ps->size) { printf("insert pos is illegal!"); assert(0); } int end = ps->size++; while(end>pos) { ps->p[end]=ps->p[end-1]; end--; } ps->p[pos]=x;
}
void SeqListErase(SeqList* ps, int pos) { if(pos<0 || pos>=ps->size ) { printf("delete pos is illegal!"); assert(0); } if(ps->size==0) { printf("SeqList is empty!"); assert(0); }
for(int i=pos; i<ps->size-1; i++) { ps->p[i]=ps->p[i+1]; } ps->size--; }
|