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

静态链表

时间:2019-04-30 21:34:02      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:语言   ==   sign   tag   col   .data   getc   com   pause   

顺序链表的缺陷

1.单链表的实现严重依赖指针
2.数据元素中必须包含一个额外的指针域
3.没有指针的程序设计语言无法实现

技术图片

 

技术图片

 

 

技术图片

 

  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 int 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) // O(n)
 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     
 30     if( ret != NULL )
 31     {
 32         ret->capacity = capacity;
 33         ret->header.data = 0;
 34         ret->header.next = 0;
 35         
 36         for(i=1; i<=capacity; i++)
 37         {
 38             ret->node[i].next = AVAILABLE;
 39         }
 40     }
 41     
 42     return ret;
 43 }
 44 
 45 void StaticList_Destroy(StaticList* list) // O(1)
 46 {
 47     free(list);
 48 }
 49 
 50 void StaticList_Clear(StaticList* list) // O(n)
 51 {
 52     TStaticList* sList = (TStaticList*)list;
 53     int i = 0;
 54     
 55     if( sList != NULL )
 56     {
 57         sList->header.data = 0;
 58         sList->header.next = 0;
 59         
 60         for(i=1; i<=sList->capacity; i++)
 61         {
 62             sList->node[i].next = AVAILABLE;
 63         }
 64     }
 65 }
 66 
 67 int StaticList_Length(StaticList* list) // O(1)
 68 {
 69     TStaticList* sList = (TStaticList*)list;
 70     int ret = -1;
 71     
 72     if( sList != NULL )
 73     {
 74         ret = sList->header.data;
 75     }
 76     
 77     return ret;
 78 }
 79 
 80 int StaticList_Capacity(StaticList* list) // O(1)
 81 {
 82     TStaticList* sList = (TStaticList*)list;
 83     int ret = -1;
 84     
 85     if( sList != NULL )
 86     {
 87         ret = sList->capacity;
 88     }
 89     
 90     return ret;
 91 }
 92 
 93 int StaticList_Insert(StaticList* list, StaticListNode* node, int pos)  // O(n)
 94 {
 95     TStaticList* sList = (TStaticList*)list;
 96     int ret = (sList != NULL);
 97     int current = 0;
 98     int index = 0;
 99     int i = 0;
100     
101     ret = ret && (sList->header.data + 1 <= sList->capacity);
102     ret = ret && (pos >=0) && (node != NULL);
103     
104     if( ret )
105     {
106         for(i=1; i<=sList->capacity; i++)
107         {
108             if( sList->node[i].next == AVAILABLE )
109             {
110                 index = i;
111                 break;
112             }
113         }
114         
115         sList->node[index].data = (unsigned int)node;
116         
117         sList->node[0] = sList->header;
118         
119         for(i=0; (i<pos) && (sList->node[current].next != 0); i++)
120         {
121             current = sList->node[current].next;
122         }
123         
124         sList->node[index].next = sList->node[current].next;
125         sList->node[current].next = index;
126         
127         sList->node[0].data++;
128         
129         sList->header = sList->node[0];
130     }
131     
132     return ret;
133 }
134 
135 StaticListNode* StaticList_Get(StaticList* list, int pos)  // O(n)
136 {
137     TStaticList* sList = (TStaticList*)list;
138     StaticListNode* ret = NULL;
139     int current = 0;
140     int object = 0;
141     int i = 0;
142     
143     if( (sList != NULL) && (0 <= pos) && (pos < sList->header.data) )
144     {
145         sList->node[0] = sList->header;
146         
147         for(i=0; i<pos; i++)
148         {
149             current = sList->node[current].next;
150         }
151         
152         object = sList->node[current].next;
153         
154         ret = (StaticListNode*)(sList->node[object].data);
155     }
156     
157     return ret;
158 }
159 
160 StaticListNode* StaticList_Delete(StaticList* list, int pos) // O(n)
161 {
162     TStaticList* sList = (TStaticList*)list;
163     StaticListNode* ret = NULL;
164     int current = 0;
165     int object = 0;
166     int i = 0;
167     
168     if( (sList != NULL) && (0 <= pos) && (pos < sList->header.data) )
169     {
170         sList->node[0] = sList->header;
171         
172         for(i=0; i<pos; i++)
173         {
174             current = sList->node[current].next;
175         }
176         
177         object = sList->node[current].next;
178         
179         sList->node[current].next = sList->node[object].next;
180         
181         sList->node[0].data--;
182         
183         sList->header = sList->node[0];
184         
185         sList->node[object].next = AVAILABLE;
186         
187         ret = (StaticListNode*)(sList->node[object].data);
188     }
189     
190     return ret;
191 }
#ifndef _STATICLIST_H_
#define _STATICLIST_H_

typedef void StaticList;
typedef void StaticListNode;

StaticList* StaticList_Create(int capacity);

void StaticList_Destroy(StaticList* list);

void StaticList_Clear(StaticList* list);

int StaticList_Length(StaticList* list);

int StaticList_Capacity(StaticList* list);

int StaticList_Insert(StaticList* list, StaticListNode* node, int pos);

StaticListNode* StaticList_Get(StaticList* list, int pos);

StaticListNode* StaticList_Delete(StaticList* list, int pos);

#endif
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "StaticList.h"
 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 5 
 6 int main(int argc, char *argv[])
 7 {
 8     StaticList* list = StaticList_Create(10);
 9     
10     int index = 0;
11     
12     int i = 0;
13     int j = 1;
14     int k = 2;
15     int x = 3;
16     int y = 4;
17     int z = 5;
18     
19     StaticList_Insert(list, &i, 0);
20     StaticList_Insert(list, &j, 0);
21     StaticList_Insert(list, &k, 0);
22     
23     for(index=0; index<StaticList_Length(list); index++)
24     {
25         int* p = (int*)StaticList_Get(list, index);
26         
27         printf("%d\n", *p);
28     }
29     
30     printf("\n");
31     
32     while( StaticList_Length(list) > 0 )
33     {
34         int* p = (int*)StaticList_Delete(list, 0);
35         
36         printf("%d\n", *p);
37     }
38     
39     printf("\n");
40     
41     StaticList_Insert(list, &x, 0);
42     StaticList_Insert(list, &y, 0);
43     StaticList_Insert(list, &z, 0);
44     
45     printf("Capacity: %d Length: %d\n", StaticList_Capacity(list), StaticList_Length(list));
46     
47     for(index=0; index<StaticList_Length(list); index++)
48     {
49         int* p = (int*)StaticList_Get(list, index);
50         
51         printf("%d\n", *p);
52     }
53     
54     StaticList_Destroy(list);
55     
56     return 0;
57 }

 

静态链表

标签:语言   ==   sign   tag   col   .data   getc   com   pause   

原文地址:https://www.cnblogs.com/xiaowulang/p/10797989.html

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