设为首页 加入收藏

TOP

数据结构——线性表的顺序存储结构(一)
2019-04-06 18:08:10 】 浏览:103
Tags:数据结构 线性 顺序 存储 结构

创建一个可复用的顺序表

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,
		    
 
			
style="color: #0000ff;">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,&a
			
		  
编程开发网
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言进阶指南(2)丨数组和指针.. 下一篇C语言进阶指南(1)丨整型溢出和..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(217) }