欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 数据结构——动态顺序表(DEV C++版本)

数据结构——动态顺序表(DEV C++版本)

2024/10/27 4:33:02 来源:https://blog.csdn.net/2301_82086893/article/details/143193799  浏览:    关键词:数据结构——动态顺序表(DEV C++版本)

之前写的动态顺序表是在vs2022中完成的,为了照顾没有vs2022的家人,发布一篇DEV版本

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct {int *arr;int size;int capacity;
}SL;
SL s;
void init(SL* ps)
{ps->arr=NULL;ps->capacity=0;ps->size=0;} void print(SL* ps){int i=0;for(i=0;i<ps->size;i++){printf("%d  ",ps->arr[i]);}printf("\n");}void check(SL* ps){int newcapacity=ps->capacity==0?4:2*ps->capacity;if(ps->size==ps->capacity){int* temp=(int*)realloc(ps->arr,sizeof(int)*2*ps->capacity);if(temp==NULL){printf("error\n");exit(-1);}else{ps->arr=temp;ps->capacity=newcapacity;}}}void pushback(SL* ps,int x){check(ps);ps->arr[ps->size]=x;ps->size++;}void pushfront(SL* ps,int x){check(ps);int end=ps->size-1;while(end>=0){ps->arr[end+1]=ps->arr[end];end--;}ps->size++;ps->arr[0]=x;}void deleteback(SL* ps){ps->size--;}void deletefront(SL* ps){int tail=1;while(tail<ps->size){ps->arr[tail-1]=ps->arr[tail];tail++;}ps->size--;}void pushrandom(SL* ps,int index,int x){assert(index<=ps->size);check(ps);int end=ps->size;while(end>index){ps->arr[end]=ps->arr[end-1];end--;}ps->arr[index]=x;ps->size++;}void deleterandom(SL* ps,int index){assert(index<ps->size);int begin =index;while(begin<=ps->size-1){ps->arr[begin]=ps->arr[begin+1];begin++;}ps->size--;}void updata(SL* ps,int index,int x){ps->arr[index]=x;}void test(){init(&s);pushback(&s,2);pushback(&s,1);pushback(&s,1);pushfront(&s,5);pushfront(&s,8);pushfront(&s,9);print(&s); deleteback(&s);deleteback(&s);deletefront(&s);deletefront(&s);print(&s);pushrandom(&s,1,5);print(&s);pushrandom(&s,1,9);print(&s);pushrandom(&s,1,8);print(&s);pushrandom(&s,1,2);print(&s);pushrandom(&s,1,3);print(&s);deleterandom(&s,1);print(&s);deleterandom(&s,1);print(&s);deleterandom(&s,1);print(&s);deleterandom(&s,1);print(&s);deleterandom(&s,1);print(&s);updata(&s,0,2);print(&s);}int main(){test();free(s.arr);return 0;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com