线性表之顺序存储结构--C实现

定义

线性表可以说是最简单的数据结构,它的描述为:n个数据元素的有限序列

记为:L=(a1,a2,...,an)

按照存储结构它又可以分为顺序存储结构和链式存储结构。

而其中线性表的顺序存储结构是最简单最常用的数据结构:用一段连续地址依次存储表中的数据元素

看到这里,我们会自然的联想到C语言中的数组。

下面我要实现的是线性表中的元素为整型的顺序存储结构,及它的主要运算:增删查。

先来简单的定义一下这个线性表的顺序存储结构:

[cpp]  view plain copy
  1. #define MAXLENGTH 20  
  2.   
  3. struct sequencelist  
  4. {  
  5.     int data[MAXLENGTH];  
  6.     int length;  
  7. };  
其中data数组为这个线性表的主要部分,数据元素就存在于此数组中,而对这个线性表的操作都是基于这个数组。

length是这个线性表的一个属性,表示这个线性表包含元素的个数。

增:线性表的插入操作

对线性表的插入就是对data数组的插入,然后将其length增加。

[cpp]  view plain copy
  1. //insert opration  
  2. int insert(struct sequencelist *list,int index,int element)  
  3. {  
  4.     int length = list->length;  
  5.     if(length ==0 || index < 0 || index > length || length >= MAXLENGTH)  
  6.         return ERROR;  
  7.     list->data[index] = element;  
  8.     for(int i = length - 1;i>index;i--)  
  9.     {  
  10.         list->data[i+1] = list->data[i];  
  11.     }     
  12.     list->length++;  
  13.     return OK;  
  14. }  

删:线性表的删除操作

类似增的相反操作。

[cpp]  view plain copy
  1. // Delete opration  
  2. int delete(struct sequencelist *list,int index)  
  3. {  
  4.     int length = list->length;  
  5.     if(length ==0 || index < 0 || index > length-1 )  
  6.         return ERROR;  
  7.     for(int i = index;i<length-1;i++)  
  8.     {  
  9.         list->data[i] = list->data[i+1];  
  10.     }  
  11.     list->data[length-1] = '\0';//delete the last element.  
  12.     list->length--;  
  13.     return OK;  
  14. }  

查:线性表的取元素操作

用索引值查找元素的值。

[cpp]  view plain copy
  1. //get list elements  
  2. //make sure elemet is NOT NULL when calling.  
  3. int getElement(struct sequencelist list,int index,int *element)  
  4. {  
  5.     printf("\ngetElement\n");  
  6.     int length = list.length;  
  7.     printf("length is %d\n",length);  
  8.     if(length ==0 || index < 0 || index >= length)  
  9.         return ERROR;  
  10.     *element = list.data[index];  
  11.     return OK;  
  12. }  
从程序中可以看出增删操作的时间复杂度都是0(n),所以这两项操作都是不是它的强项。而查找操作的时间复杂度是O(1),那么线性表的顺序存储结构的优势就是可以快速的取出任意位置的元素。

上面的3种操作作为较为基本的操作,是本人的学习笔记。如果大虾们发现哪里有不妥,请不吝赐教。

而线性表的其他操作如求前驱元素、求后驱元素、判断表中是否存在某元素值、清空操作等等有意思的操作还待空闲时去完成。


较为完整的调试例子:

[cpp]  view plain copy
  1. //2013.2  
  2. //lincoln  
  3. //linear list  
  4. //Sequence Storage Structure   
  5. //  
  6. #include <stdio.h>  
  7.   
  8. #define OK 1  
  9. #define ERROR -1  
  10. #define TURE 1  
  11. #define FALSE 0  
  12. #define MAXLENGTH 20  
  13.   
  14. struct sequencelist  
  15. {  
  16.     int data[MAXLENGTH];  
  17.     int length;  
  18. };  
  19.   
  20. //get list elements  
  21. //make sure elemet is NOT NULL when calling.  
  22. int getElement(struct sequencelist list,int index,int *element)  
  23. {  
  24.     printf("\ngetElement\n");  
  25.     int length = list.length;  
  26.     printf("length is %d\n",length);  
  27.     if(length ==0 || index < 0 || index >= length)  
  28.         return ERROR;  
  29.     *element = list.data[index];  
  30.     return OK;  
  31. }  
  32.   
  33. //insert opration  
  34. //  
  35. int insert(struct sequencelist *list,int index,int element)  
  36. {  
  37.     printf("\ninsert\n");  
  38.     int length = list->length;  
  39.     printf("length is %d\n",length);  
  40.     if(length ==0 || index < 0 || index > length || length >= MAXLENGTH)  
  41.         return ERROR;  
  42.     list->data[index] = element;  
  43.     for(int i = length - 1;i>index;i--)  
  44.     {  
  45.         list->data[i+1] = list->data[i];  
  46.     }     
  47.     list->length++;  
  48.     return OK;  
  49. }  
  50.   
  51. // Delete opration  
  52. //  
  53. int delete(struct sequencelist *list,int index)  
  54. {  
  55.     printf("\ndelete\n");  
  56.     int length = list->length;  
  57.     printf("length is %d\n",length);  
  58.     if(length ==0 || index < 0 || index > length-1 )  
  59.         return ERROR;  
  60.     for(int i = index;i<length-1;i++)  
  61.     {  
  62.         printf("delete data[%d]\n",i);  
  63.         list->data[i] = list->data[i+1];  
  64.     }  
  65.     list->data[length-1] = '\0';//delete the last element.  
  66.     list->length--;  
  67.     return OK;  
  68. }  
  69.   
  70. int main()  
  71. {  
  72.     struct sequencelist list =   
  73.     {  
  74.         {3,1,5,7,12,78,34},  
  75.         7  
  76.     };  
  77.   
  78.     printf("list length  : %d\n",list.length);  
  79.     //Test get  
  80.     int *element = 0, test = 8;  
  81.     element = &test;  
  82.     if(OK == getElement(list,2,element))  
  83.     {  
  84.         printf("list get 2 :%d\n", *element);  
  85.     }  
  86.     //Test insert  
  87.     if(OK == insert(&list,7,520))     
  88.     {  
  89.         printf("list insert 7 ok!\n");  
  90.     }  
  91.     if(OK == getElement(list,7,element))  
  92.     {  
  93.         printf("list get 7 :%d\n", *element);  
  94.     }     
  95.     if(OK == insert(&list,3,520))     
  96.     {  
  97.         printf("list insert 3 ok!\n");  
  98.     }  
  99.     if(OK == getElement(list,3,element))  
  100.     {  
  101.         printf("list get 3 :%d\n", *element);  
  102.     }  
  103.       
  104.     //Test delete  
  105.     if(OK == delete(&list,3))  
  106.     {  
  107.         printf("list delete 3 ok!\n");  
  108.     }  
  109.     if(OK == getElement(list,3,element))  
  110.     {  
  111.         printf("list get 3 :%d\n", *element);  
  112.     }  
  113.     if(OK == delete(&list,6))  
  114.     {  
  115.         printf("list delete 6 ok!\n");  
  116.     }  
  117.     if(OK == getElement(list,6,element))  
  118.     {  
  119.         printf("list get 6 :%d\n", *element);  
  120.     }  
  121.     else  
  122.     {  
  123.         printf("list get ERROR!\n");  
  124.     }  
  125. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值