数据结构——线性表的顺序存储结构
程序员文章站
2022-10-18 08:22:40
创建一个可复用的顺序表 seqlist.h(头文件) seqlist.c(功能函数) seqlistmain.c(实现程序) ......
创建一个可复用的顺序表
seqlist.h(头文件)
1 #ifndef _seqlist_h_ 2 #define _seqlist_h_ 3 //数据封装 4 typedef void seqlist; 5 typedef void seqlistnode; 6 //创建 7 seqlist* seqlist_create(int capacity); 8 //销毁 9 void seqlist_destroy(seqlist* list); 10 //清除 11 void seqlist_clear(seqlist* list); 12 //长度 13 int seqlist_length(seqlist* list); 14 //最大空间 15 int seqlist_capacity(seqlist* list); 16 //插入 17 int seqlist_insert(seqlist* list,seqlistnode* node,int pos); 18 //取值 19 seqlistnode* seqlist_get(seqlist* list,int pos); 20 //删除 21 seqlistnode* seqlist_delete(seqlist* list,int pos); 22 23 #endif
seqlist.c(功能函数)
1 #include<stdio.h> 2 #include<malloc.h> 3 #include"seqlist.h" 4 5 typedef unsigned long tseqlistnode;//64位地址 6 typedef struct _tag_seqlist 7 { 8 int capacity; 9 int length; 10 tseqlistnode *node; 11 }tseqlist; 12 13 seqlist* seqlist_create(int capacity) 14 { 15 tseqlist* ret=null; 16 if(capacity>=0) 17 { 18 ret=(tseqlist*)malloc(sizeof(tseqlist)+capacity*sizeof(tseqlistnode)); 19 } 20 if(ret!=null) 21 { 22 ret->capacity=capacity; 23 ret->length=0; 24 ret->node=(tseqlistnode*)(ret+1); 25 } 26 return ret; 27 } 28 29 void seqlist_destroy(seqlist* list) 30 { 31 free(list); 32 } 33 34 void seqlist_clear(seqlist* list) 35 { 36 tseqlist* slist=(tseqlist*)list; 37 if(slist!=null) 38 { 39 slist->length=0; 40 } 41 } 42 43 int seqlist_length(seqlist* list) 44 { 45 tseqlist* slist=(tseqlist*)list; 46 int ret=-1; 47 if(slist!=null) 48 { 49 ret=slist->length; 50 } 51 return ret; 52 } 53 54 int seqlist_capacity(seqlist* list) 55 { 56 tseqlist* slist=(tseqlist*)list; 57 int ret=-1; 58 if(slist!=null) 59 { 60 ret=slist->capacity; 61 } 62 return ret; 63 } 64 65 int seqlist_insert(seqlist* list,seqlistnode* node,int pos) 66 { 67 tseqlist* slist=(tseqlist*)list; 68 int i=0; 69 int ret=(slist!=null); 70 ret=ret&&(slist->length+1<=slist->capacity); 71 ret=ret&&(pos>=0); 72 if(ret) 73 { 74 if(pos>=slist->length)//插入位置大,自动放入最后 75 { 76 pos=slist->length; 77 } 78 for(i=slist->length;i>pos;i--) 79 { 80 slist->node[i]=slist->node[i-1]; 81 } 82 slist->node[pos]=(tseqlistnode)node; 83 slist->length++; 84 } 85 return ret; 86 } 87 88 seqlistnode* seqlist_get(seqlist* list,int pos) 89 { 90 tseqlist* slist=(tseqlist*)list; 91 seqlistnode* ret=null; 92 int r=(slist!=null); 93 r=r&&(pos>=0)&&(pos<slist->length); 94 if(r) 95 { 96 ret=(seqlistnode*)slist->node[pos]; 97 } 98 return ret; 99 } 100 101 seqlistnode* seqlist_delete(seqlist* list,int pos) 102 { 103 tseqlist* slist=(tseqlist*)list; 104 int i=0; 105 seqlist* ret=null; 106 int r=(slist!=null); 107 r=r&&(pos>=0)&&(pos<slist->length); 108 if(r) 109 { 110 ret=(seqlist*)slist->node[pos]; 111 for(i=pos;i<slist->length-1;i++) 112 { 113 slist->node[i]=slist->node[i+1]; 114 } 115 slist->length--; 116 return ret; 117 } 118 }
seqlistmain.c(实现程序)
1 #include<stdio.h> 2 #include<malloc.h> 3 #include"seqlist.h" 4 int main() 5 { 6 seqlist* list=seqlist_create(5); 7 int a=1; 8 int b=2; 9 int c=3; 10 int d=4; 11 int e=5; 12 int i=0; 13 seqlist_insert(list,&a,0); 14 seqlist_insert(list,&b,0); 15 seqlist_insert(list,&c,0); 16 seqlist_insert(list,&d,0); 17 seqlist_insert(list,&e,0); 18 19 for(i=0;i<seqlist_length(list);i++) 20 { 21 int *p=(int*)seqlist_get(list,i); 22 printf("%d\n",*p); 23 } 24 printf("\n"); 25 seqlist_delete(list,0); 26 for(i=0;i<seqlist_length(list);i++) 27 { 28 int *p=(int*)seqlist_get(list,i); 29 printf("%d\n",*p); 30 } 31 printf("\n"); 32 int length=seqlist_length(list); 33 printf("%d\n",length); 34 printf("\n"); 35 int capacity=seqlist_capacity(list); 36 printf("%d\n",capacity); 37 38 seqlist_destroy(list); 39 return 0; 40 }