#include<string.h> #include<stdio.h> #include<malloc.h> #include<assert.h> #include<stdlib.h> typedef int DataType; typedef struct Seqlist { DataType* _array; size_t _size; size_t capacity; }Seqlist; void Initlist(Seqlist *s) { assert(s); s->capacity =100; s->_array =(DataType*)malloc(sizeof(DataType)*s->capacity ); s->_size =0; memset(s->_array,0,(sizeof(DataType)*s->capacity)); } void Pushback(Seqlist*s,DataType x) { assert(s); if(s->_size >=s->capacity ) { DataType *tmp=NULL; s->capacity *=2; tmp=(DataType *)malloc(sizeof(DataType)*s->capacity); memcpy(tmp,s->_array ,(sizeof(DataType)*s->_size )); free(s->_array ); s->_array =tmp; } s->_array [s->_size ++]=x; } void Popback(Seqlist*s) { assert(s); if(s->_size ==0) { printf("Seqlist is null\n"); return; } s->_size--; } void Pushfront(Seqlist*s,DataType x) { int i; assert(s); if(s->_size >=s->capacity ) { DataType *tmp=NULL; s->capacity *=2; tmp=(DataType *)malloc(sizeof(DataType)*s->capacity); memcpy(tmp,s->_array ,(sizeof(DataType)*s->_size )); free(s->_array ); s->_array =tmp; } for(i=(int)s->_size ;i>0;i--) { s->_array [i]=s->_array [i-1]; } s->_array [0]=x; s->_size ++; } void Popfront(Seqlist*s) { int i; assert(s); if(s->_size ==0) { printf("Seqlist is null\n"); return; } for(i=0;i<s->_size-1 ;i++) { s->_array [i]=s->_array [i+1]; } s->_size --; } void Insert(Seqlist*s,size_t pos,DataType x) { size_t i; assert(s); if(s->_size >=s->capacity ) { DataType *tmp=NULL; s->capacity *=2; tmp=(DataType *)malloc(sizeof(DataType)*s->capacity); memcpy(tmp,s->_array ,(sizeof(DataType)*s->_size )); free(s->_array ); s->_array =tmp; } for(i=s->_size ;i>pos;i--) { s->_array [i]=s->_array [i-1]; } s->_array [pos]=x; s->_size ++; } void Erase(Seqlist*s,size_t pos) { int i; assert(s); assert(pos<=s->_size); if(s->_size ==0) { printf("Seqlist is null\n"); return; } for(i=pos;i<s->_size-1 ;i++) { s->_array [i]=s->_array [i+1]; } s->_size --; } int find(Seqlist*s,DataType x) { int i; assert(s); for(i=0;i<s->_size ;i++) { if(s->_array [i]==x) { return i; } } printf("没有该值\n"); } void Remove(Seqlist*s,DataType x) { int i; assert(s); if(s->_size ==0) { printf("Seqlist is null\n"); return; } for(i=0;i<s->_size ;i++) { if(s->_array [i]==x) { int begin=i; for(;begin<s->_size-1;begin++) { s->_array[begin]=s->_array[begin+1]; } s->_size --; break; } } if(i==s->_size) { printf("无该值\n"); return; } } void Removeall(Seqlist*s,DataType x) { DataType * first=s->_array ; DataType * second=s->_array ; DataType * end=second+s->_size; int count=0; assert(s); if(s->_size ==0) { printf("Seqlist is null\n"); return; } for(;second<end;second++) { if(*second!=x) { *first=*second; first++; } else { count++; } } s->_size -=count; } void Modify(Seqlist*s,size_t pos,DataType x) { assert(s); assert(pos<=s->_size ); if(s->_size ==0) { printf("Seqlist is null\n"); return; } s->_array [pos]=x; } void Print(Seqlist*s) { int i; assert(s); if(s->_size ==0) { printf("Seqlist is null"); return; } for(i=0;i<s->_size ;i++) { printf("%d ",s->_array [i]); } } void test() { Seqlist s; Initlist(&s); Print(&s); Pushback (&s,1); Pushback (&s,2); Pushback (&s,3); Pushback (&s,2); Pushback (&s,2); Pushback (&s,7); /*Print(&s); Popback(&s); Print(&s);*/ Pushfront (&s,8); /*Print(&s);*/ Popfront (&s); Print(&s); printf("\n"); Remove(&s,2); Print(&s); printf("\n"); Insert(&s,1,2); Print(&s); Removeall(&s,2); Print(&s); Modify(&s,2,2); Print(&s); } int main() { test(); system("pause"); return 0; }
时间: 2024-10-14 17:03:53