数据结构二静态链表

来源:互联网 发布:xmind8破解补丁 mac 编辑:程序博客网 时间:2024/05/16 02:44

用数组描述的链表,即称为静态链表,它的表现形式即为结构体数组,结构体变量包括数据域data和指针域next。这种存储结构,仍需要预先分配一个较大的空间,但在作为线性表的插入和删除操作时不需移动元素,仅需修改指针,故仍具有链式存储结构的主要优点。而且在不具有指针类型的高级语言中,无法使用指针类型,也就无法用指针实现单链表结构,所以静态链表是个不错的选择。

       静态链表的定义为:

                l 顺序表数组中的元素由两个数据域组成:data和next

                l Data域用于存储数据

                l Next域用于存储下一个元素在数组中的下标

       这里介绍静态链表的常用操作:

                l 创建静态链表

                l 销毁静态链表

                l 清空静态链表

                l 表元素插入

                l 表元素删除

                l 获取表中某个位置的元素

                l 获取表长度

                l 获取表的最大长度

       

       代码总分为三个文件:

               StaticList.h : 放置功能函数的声明,以及表和表结点的声明 

               StaticList.h : 放置功能函数的定义,以及表和表结点的定义

               Main.c    : 主函数,使用功能函数完成各种需求,不过一般用作测试

       这里着重说下获取操作﹑插入操作和删除操作:

              获取操作:

                       获取第pos个元素方法:

                       首先判断线性表﹑pos位置是否合法

                       由表头开始通过next指针移动pos次后,当前元素的next域即要获取元素在数组中的下标

 

              插入操作:

                       如图

                                          

                       插入元素方法:

                               首先判断线性表﹑插入位置是否合法

                               在数组中从头开始查找空闲位置index(其实也就是判断每个数组元素的next是否为空闲状态)

                               由表头开始通过next域移动pos次后,当前元素的next域为要插入的位置

                               将新元素插入

                               静态链表长度加1

              删除操作:

                        如图

                       

                        删除元素方法:

 

                              判断表和删除位置是否合法

                              获取第pos个元素

                              将第pos个元素从链表中删除

                              表长度减1

   

OK! 上代码:

StaticList.h

[cpp] view plain copy
  1. #ifndef _STATICLIST_H_  
  2. #define _STATICLIST_H_  
  3.   
  4. typedef void StaticList;  
  5. typedef void StaticListNode;  
  6.   
  7. StaticList* StaticList_Create(int capacity);  
  8.   
  9. void StaticList_Destroy(StaticList* list);  
  10.   
  11. void StaticList_CLear(StaticList* list);  
  12.   
  13. int StaticList_Length(StaticList* list);  
  14.   
  15. int StaticList_Capacity(StaticList* list);  
  16.   
  17. int StaticList_Insert(StaticList* list, StaticListNode* node, int pos);  
  18.   
  19. StaticListNode* StaticList_Get(StaticList* list, int pos);  
  20.   
  21. StaticListNode* StaticList_Delete(StaticList* list, int pos);  
  22.   
  23. #endif  


StaticList.c

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

Main.c

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include "StaticList.h"  
  4.   
  5. int main(void)  
  6. {  
  7.     StaticList* list = StaticList_Create(10);  
  8.   
  9.     int index = 0;  
  10.   
  11.     int i=0, j=1, k=2, x=3, y=4, z=5;  
  12.   
  13.     StaticList_Insert(list, &i, 0);  
  14.     StaticList_Insert(list, &j, 0);  
  15.     StaticList_Insert(list, &k, 0);  
  16.   
  17.     for(index=0; index<StaticList_Length(list); index++)  
  18.     {  
  19.         int* p = (int*)StaticList_Get(list, index);  
  20.   
  21.         printf("%d\n", *p);  
  22.     }  
  23.     printf("\n");  
  24.   
  25.     while(StaticList_Length(list) > 0)  
  26.     {  
  27.         int* p = (int*)StaticList_Delete(list, StaticList_Length(list)-1);  
  28.   
  29.         printf("%d\n", *p);  
  30.     }  
  31.   
  32.     StaticList_Insert(list, &x, 0);  
  33.     StaticList_Insert(list, &y, 0);  
  34.     StaticList_Insert(list, &z, 0);  
  35.   
  36.     printf("Capacity:%d Length:%d\n", StaticList_Capacity(list), StaticList_Length(list));  
  37.     printf("\n");  
  38.   
  39.     for(index=0; index<StaticList_Length(list); index++)  
  40.     {  
  41.         int* p = (int*)StaticList_Get(list, index);  
  42.   
  43.         printf("%d\n", *p);  
  44.     }  
  45.   
  46.     StaticList_Destroy(list);  
  47.   
  48.     return 0;  
  49. }  

原创粉丝点击