码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构——线性表的链式存储结构

时间:2019-04-12 13:31:02      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:class   turn   div   data-   object   font   tno   main   一个   

创建一个可复用的静态链表

1 //结点结构体定义
2 typedef struct _tag_staticlistnode
3 {
4     unsigned long data;
5     int next;
6 }tstaticlistnode;
1 //静态链表结构体定义
2 typedef struct _tag_staticlist
3 {
4     int capacity;
5     tstaticlistnode header;
6     tstaticlistnode node[];
7 }tstaticlist;

staticlist.h(头文件)

 1 #ifndef _STATICLIST_H_
 2 #define _STATICLIST_H_
 3 
 4 typedef void staticlist;
 5 
 6 typedef void staticlistnode;
 7 
 8 staticlist* staticlist_create(int capacity);
 9 
10 void staticlist_destroy(staticlist* list);
11 
12 void staticlist_clear(staticlist* list);
13 
14 int staticlist_length(staticlist* list);
15 
16 int staticlist_capacity(staticlist* list);
17 
18 staticlistnode* staticlist_get(staticlist* list,int pos);
19 
20 int staticlist_insert(staticlist* list,staticlistnode* node,int pos);
21 
22 staticlistnode* staticlist_delete(staticlist* list,int pos);
23 
24 #endif

staticlist.c(功能函数)

  1 #include<stdio.h>
  2 #include<malloc.h>
  3 #include"staticlist.h"
  4 
  5 #define AVAILABLE -1
  6 
  7 typedef struct _tag_staticlistnode
  8 {
  9     unsigned long data;
 10     int next;
 11 }tstaticlistnode;
 12 
 13 typedef struct _tag_staticlist
 14 {
 15     int capacity;
 16     tstaticlistnode header;
 17     tstaticlistnode node[];
 18 }tstaticlist;
 19 
 20 staticlist* staticlist_create(int capacity)
 21 {
 22     tstaticlist* ret=NULL;
 23     int i=0;
 24 
 25     if(capacity>=0)
 26     {
 27         ret=(tstaticlist*)malloc(sizeof(tstaticlist)+sizeof(tstaticlistnode)*(capacity + 1));
 28     }
 29     if(ret!=NULL)
 30     {
 31         ret->capacity=capacity;
 32         ret->header.next=0;
 33         ret->header.data=0;
 34 
 35         for(i=1;i<=capacity;i++)
 36         {
 37             ret->node[i].next=AVAILABLE;
 38         }
 39     }
 40     return ret;
 41 }
 42 
 43 void staticlist_destroy(staticlist* list)
 44 {
 45     free(list);
 46 }
 47 
 48 void staticlist_clear(staticlist* list)
 49 {
 50     tstaticlist* slist=(tstaticlist*)list;
 51     int i=0;
 52     if(slist!=NULL)
 53     {
 54         slist->header.data=0;
 55         slist->header.next=0;
 56 
 57         for(i=1;i<=slist->capacity;i++)
 58         {
 59             slist->node[i].next=AVAILABLE;
 60         }
 61     }
 62 }
 63 
 64 
 65 int staticlist_length(staticlist* list)
 66 {
 67     tstaticlist* slist=(tstaticlist*)list;
 68     int ret=-1;
 69     if(slist!=NULL)
 70     {
 71         ret=slist->header.data;
 72     }
 73     return ret;
 74 }
 75 
 76 int staticlist_capacity(staticlist* list)
 77 {
 78     tstaticlist* slist=(tstaticlist*)list;
 79     int ret=-1;
 80     if(slist!=NULL)
 81     {
 82         ret=slist->capacity;
 83     }
 84     return ret;
 85 }
 86 
 87 staticlistnode* staticlist_get(staticlist* list,int pos)
 88 {
 89     tstaticlist* slist=(tstaticlist*)list;
 90     staticlistnode* ret=NULL;
 91     int current=0;
 92     int object=0;
 93     int i=0;
 94     int f=0;
 95     f=(slist!=NULL);
 96     f=f&&(pos>=0)&&(pos<slist->header.data);
 97     if(f)
 98     {
 99         slist->node[0]=slist->header;
100         for(i=0;i<pos;i++)
101         {
102             current=slist->node[current].next;
103         }
104         object=slist->node[current].next;
105         ret=(staticlistnode*)(slist->node[object].data);
106     }
107     return ret;
108 }
109 
110 int staticlist_insert(staticlist* list,staticlistnode* node,int pos)
111 {
112     tstaticlist* slist = (tstaticlist*)list;
113     int ret=(slist!=NULL);
114     int current=0;
115     int index=0;
116     int i=0;
117     
118     ret=ret&&(slist->header.data+1<=slist->capacity);
119     ret=ret&&(pos>=0)&&(node!=NULL);
120     
121     if(ret)
122     {
123         for(i=1;i<=slist->capacity;i++)
124         {
125             if(slist->node[i].next==AVAILABLE)
126             {
127                 index=i;
128                 break;
129             }
130         }
131         
132         slist->node[index].data=(unsigned long)node;
133         
134         slist->node[0]=slist->header;
135 
136         for(i=0;(i<pos)&&(slist->node[current].next!=0);i++)
137         {
138             current=slist->node[current].next;
139         }
140         slist->node[index].next=slist->node[current].next;
141         slist->node[current].next=index;
142         slist->node[0].data++;
143         slist->header=slist->node[0];
144     }
145     
146     return ret;
147 }
148 staticlistnode* staticlist_delete(staticlist* list,int pos)
149 {
150     tstaticlist* slist=(tstaticlist*)list;
151     staticlistnode* ret=NULL;
152     int i=0;
153     int current=0;
154     int object=0;
155     int f=0;
156     f=(slist!=NULL);
157     f=f&&(pos>=0)&&(pos<slist->header.data);
158     if(f)
159     {
160         slist->node[0]=slist->header;
161         for(i=0;i<pos;i++)
162         {
163             current=slist->node[current].next;
164         }
165         object=slist->node[current].next;
166         slist->node[current].next=slist->node[object].next;
167         slist->node[0].data--;
168         slist->header=slist->node[0];
169         slist->node[object].next=AVAILABLE;
170         ret=(staticlistnode*)(slist->node[object].data);
171     }
172     return ret;
173 }

staticlistmain.c(实现程序)

 1 #include<stdio.h>
 2 #include<malloc.h>
 3 #include"staticlist.h"
 4 
 5 int main()
 6 {
 7     staticlist* list=staticlist_create(10);
 8     int index=0;
 9     int a=1;
10     int b=2;
11     int c=3;
12     int d=4;
13     int e=5;
14 
15     staticlist_insert(list,&a,0);
16     staticlist_insert(list,&b,0);
17     staticlist_insert(list,&c,0);
18     staticlist_insert(list,&d,0);
19     staticlist_insert(list,&e,0);
20 
21     for(index=0;index<staticlist_length(list);index++)
22     {
23         int *p=(int*)staticlist_get(list,index);
24         printf("%d\n",*p);
25     }
26     printf("\n");
27 
28     staticlist_delete(list,2);
29     for(index=0;index<staticlist_length(list);index++)
30     {
31         int *p=(int*)staticlist_get(list,index);
32         printf("%d\n",*p);
33     }
34 
35     printf("\n");
36 
37     int l=0;
38     int m=0;
39     l=staticlist_length(list);
40     m=staticlist_capacity(list);
41     printf("%d\n",l);
42     printf("\n");
43     printf("%d\n",m);
44 
45     staticlist_destroy(list);
46 
47     return 0;
48 }

 

数据结构——线性表的链式存储结构

标签:class   turn   div   data-   object   font   tno   main   一个   

原文地址:https://www.cnblogs.com/hhwa/p/10695273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!