Linux C 数据结构---线性表

来源:互联网 发布:手机还原软件 编辑:程序博客网 时间:2024/05/19 15:25

 数据结构指的是数据元素及数据元素之间的相互关系,包含下面三方面的内容:

 

    其中,线性表是最基本、最简单、也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。线性表的逻辑结构简单,便于实现和操作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。

     线性表是一个线性结构,它是一个含有n≥0个结点的有限序列,对于其中的结点,有且仅有一个开始结点没有前驱但有一个后继结点,有且仅有一个终端结点没有后继但有一个前驱结点,其它的结点都有且仅有一个前驱和一个后继结点。

特征:

1.集合中必存在唯一的一个“第一元素”;
2.集合中必存在唯一的一个 “最后元素” ;
3.除最后一个元素之外,均有 唯一的后继(后件);
4.除第一个元素之外,均有 唯一的前驱(前件);

    线性表作为一种基本的数据结构类型,在计算机存储器的映像(或表示)一般有两种形式:

1、顺序映像---即我们常用的数组

2、链式映像---即我们常用的链表

    今天,我们先来学习顺序存储结构:

一、顺序存储结构的表示

    1、顺序存储结构的特点:

1)逻辑上相邻的元素A(i)  A(i+1),其存储位置也是相邻的;

2)对数据元素A(i)的存取为随机存取或地址存取。

3)存储密度高。存储密度D=(数据结构中的元素所占存储空间)/(整个数据结构所占空间)

     2、顺序存储结构的不足:

     对表的插入和删除等运算的时间复杂度较差。

     3、顺序存储结构的表示:

在C语言中,一维数组的元素也是存放于一片连续的存储空间中,故可借助于C语言中一维数组类型来描述线性表的存储结构,即

[cpp] view plain copy
  1. #define N 100  
  2.   
  3. typedef int data_t;//这样定义的目的是为了代码便于维护,如果下次表中数据类型为char,修改比较方便;  
  4. typedef struct  
  5. {  
  6.     data_t data[N];//表的存储空间  
  7.     int last;//当前表尾指针,对我们对数据的定位起到很大的作用  
  8. }sqlist_t,*sqlink_t;//顺序表类型  

指针 L 指向一个顺序表,我们的数据{a0,a1,a2........  last};

[cpp] view plain copy
  1. sqlink_t L;  
  2. L = (sqlink_t *)malloc(sizeof(sqlink_t));  

这里我们定义的 int last ,可以表示{  } 、{ a0 }、{a0,a1}、{a0,a1......a99};

ai可以表示为 L->data[i] (0<=i<=L->last),一般情况下,0 < L->last < N,如果是空表{  },此时L->last = -1;

 

下面我们通过一个实例来看线性表基本算法的相关算法如何使用:

seqlist.h

[cpp] view plain copy
  1. #ifndef _SEQ_LIST_H_  
  2. #define _SEQ_LIST_H_  
  3.   
  4. #include "datatype.h"  
  5.   
  6. #define MAX 100  
  7.   
  8. typedef struct {  
  9.     data_t  data[MAX];  
  10.     int last;   /* pointer to the position  
  11.              * in the array 'data' where  
  12.              * stores the last element of 
  13.              * the list 
  14.              */  
  15. } seqlist_t;  
  16.   
  17. /*  
  18.  * create a list and init it as empty  
  19.  * Input : void 
  20.  * Output: void 
  21.  * Return: new list, NULL when failed  
  22.  */  
  23. seqlist_t *CreateEmptySqlist();  
  24.   
  25. /*  
  26.  * destroy a list  
  27.  * Input : the list to be destroied.  
  28.  * Output: void 
  29.  * Return: void 
  30.  */  
  31. void DestroySqlist(seqlist_t *list);  
  32.   
  33. /* 
  34.  * clear the list and reset it as empty 
  35.  * Input : the list to be cleared.  
  36.  * Output: void 
  37.  * Return: void 
  38.  */  
  39. void ClearSqlist(seqlist_t *list);  
  40.   
  41. /*  
  42.  * judge if the list is empty 
  43.  * Input:   the list to be tested.  
  44.  * Output:  void 
  45.  * Return: 
  46.  *  1:  list is empty 
  47.  *  0:  not  
  48.  *  -1: error, e.g. the list is invalid 
  49.  */  
  50. int EmptySqlist(seqlist_t *list);  
  51.   
  52. /*  
  53.  * judge if the list is full  
  54.  * Input : the list to be tested.  
  55.  * Output: void 
  56.  * Return: 
  57.  *  1 : list is full 
  58.  *  0 : not  
  59.  *  -1: error 
  60.  */  
  61. int FullSqlist(seqlist_t *list);  
  62.   
  63. /*  
  64.  * get length of the list  
  65.  * Input : the list to be tested.  
  66.  * Output: void 
  67.  * Return:  
  68.  *  >= 0: length of the list; 
  69.  *   -1 : means error  
  70.  */  
  71. int LengthSqlist(seqlist_t *list);  
  72.   
  73. /* 
  74.  * get data of element at specified position 
  75.  * Input :  
  76.  *  list :  the list to be operated. 
  77.  *  at :    the position where to get the element at,  
  78.  *      position index starts from zero. 
  79.  * Output: 
  80.  *  x : the data value returned 
  81.  * Return: 
  82.  *  0 : success; 
  83.  *  -1: error, e.g. list is invalid; 'at' extends  
  84.  *      the range of the list     
  85.  */  
  86. int GetSqlist(seqlist_t *list, int at, data_t *x);  
  87.   
  88. /* 
  89.  * set/update data of element at specified position 
  90.  * Input :  
  91.  *  list :  the list to be operated. 
  92.  *  at :    the position at where to set the element,  
  93.  *      position index starts from zero 
  94.  *  x : the new data value 
  95.  * Output: void 
  96.  * Return: 
  97.  *  0 : success; 
  98.  *  -1: error, e.g. list is invalid; 'at' extends the  
  99.  *      range of the list    
  100.  */  
  101. int SetSqlist(seqlist_t *list, int at, data_t x);  
  102.   
  103. /*  
  104.  * Insert element at the specified position. If the "at" exceed the  
  105.  * upper limit of the list, append the data at the end of the list.  
  106.  * e.g. insert a new data into a {}. 
  107.  * Input :  
  108.  *  list :  the list to be operated. 
  109.  *  at :    the position at which to insert the new element,  
  110.  *      position index starts from zero. 
  111.  *  x : the data to be inserted  
  112.  * Output: void 
  113.  * Return: 
  114.  *  0 : success;  
  115.  *  <0: error  
  116.  */  
  117. int InsertSqlist(seqlist_t *list, int at, data_t x);  
  118.   
  119. /* 
  120.  * delete the element by the position 
  121.  * Input :  
  122.  *  list :  the list to be operated. 
  123.  *  at :    the position at which to delete the element,  
  124.  *      position index starts from zero 
  125.  * Output : void 
  126.  * Return : 
  127.  *  0 : success; 
  128.  *  !0 :    error   
  129.  */  
  130. int DeleteSqlist(seqlist_t *list, int at);  
  131.   
  132. #endif /* _SEQ_LIST_H_ */  

seqlist.c

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include "seqlist.h"  
  4.   
  5. seqlist_t *CreateEmptySqlist()  
  6. {  
  7.     seqlist_t *list;  
  8.     list = (seqlist_t *)malloc(sizeof(seqlist_t));  
  9.     if(list != NULL)  
  10.     {  
  11.         list->last = -1;//list->last初始化,空表中,list-last = -1;  
  12.     }  
  13.   
  14.     return list;  
  15. }  
  16.   
  17. void DestroySqlist(seqlist_t *list)  
  18. {  
  19.     if (list == NULL)  
  20.         return ;  
  21.       
  22.     free(list);  
  23.   
  24.     return;  
  25. }  
  26.   
  27. void ClearSqlist(seqlist_t *list)  
  28. {  
  29.     if (list == NULL)  
  30.         return ;  
  31.   
  32.     list->last = -1;//清空线性表  
  33.   
  34. }  
  35.   
  36. int EmptySqlist(seqlist_t *list)  
  37. {  
  38.     if (list == NULL)  
  39.         return -1;  
  40.       
  41.     if(list->last == -1)//空表的标志  
  42.         return 1;  
  43.     else  
  44.         return 0;  
  45. }  
  46.   
  47. int FullSqlist(seqlist_t *list)  
  48. {  
  49.     if (list == NULL)  
  50.         return -1;  
  51.   
  52.     if (MAX - 1 == list->last)  
  53.         return 1;  
  54.     else  
  55.         return 0;  
  56. }  
  57.   
  58. int LengthSqlist(seqlist_t *list)  
  59. {  
  60.     if (list == NULL)  
  61.         return -1;  
  62.     else  
  63.         return (list->last + 1);  
  64. }  
  65.   
  66. int GetSqlist(seqlist_t *list, int at, data_t *x) //data_t *x为出参  
  67. {  
  68.     if(list == NULL || at < 0 || at > list->last)  
  69.         return -1;  
  70.   
  71.     *x = list->data[at];  
  72.   
  73.     return 0;  
  74. }  
  75.   
  76. int SetSqlist(seqlist_t *list, int at, data_t x)//data_t x为入参  
  77. {  
  78.     if(list == NULL || at < 0 || (at > list->last))  
  79.         return -1;  
  80.   
  81.     list->data[at] = x;  
  82.   
  83.     return 0;  
  84. }  
  85.   
  86. int InsertSqlist(seqlist_t *list, int at, data_t x)  
  87. {  
  88.     int j;  
  89.     if(list == NULL || at < 0 || FullSqlist(list))  
  90.         return -1;  
  91.   
  92.     if(at > list->last)//此情况比较特殊,如表中长度是50,却要插在60前面,我们这里将其插在50后面,即at = list->last + 1  
  93.     {  
  94.         at = list->last + 1;//若是空表{},list->last为-1,此时将其放在第0位;  
  95.     }  
  96.     else  
  97.     {  
  98.         for(j = list->last; j >= at; j--)  
  99.         {  
  100.             list->data[j+1] = list->data[j];  
  101.         }  
  102.     }  
  103.   
  104.     list->data[at] = x;  
  105.     list->last++;//list->last要+1  
  106.   
  107.     return 0;  
  108. }  
  109.   
  110. int DeleteSqlist(seqlist_t *list, int at)  
  111. {  
  112.     int i;  
  113.     if(list == NULL || at < 0 || (at > list->last))  
  114.         return -1;  
  115.   
  116.     for(i = at + 1; i <= list->last;i++)  
  117.         list->data[i-1] = list->data[i];  
  118.   
  119.     list->last--;  
  120.   
  121.     return 0;  
  122. }  

main.c

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include "seqlist.h"  
  4. #include "datatype.h"  
  5.   
  6. void iterate_list(seqlist_t *list)  
  7. {  
  8.     int i;  
  9.       
  10.     printf("list.last = %d, list = {", list->last);  
  11.       
  12.     for (i = -1; i < list->last;) {  
  13.         printf("%d,", list->data[++i]);  
  14.     }  
  15.           
  16.     if (LengthSqlist(list) > 0)  
  17.         printf("\b}\n");  
  18.     else  
  19.         printf("}\n");  
  20.   
  21. }  
  22.   
  23. int main(int argc, const char *argv[])  
  24. {  
  25.     int i;  
  26.     int length;  
  27.     data_t a[10] = {2,4,6,8,10,12,14,16,18,20};  
  28.     data_t x;  
  29.     seqlist_t *list;  
  30.     list = CreateEmptySqlist();  
  31.   
  32.     if (list == NULL)  
  33.         return -1;  
  34.   
  35.     for(i = 0; i < 10;i++)  
  36.     {  
  37.         if((InsertSqlist(list,i,a[i])) < 0)  
  38.             break;  
  39.     }  
  40.     iterate_list(list);  
  41.     length = LengthSqlist(list);  
  42.     printf("The length of the list is %d\n",length);  
  43.   
  44.     GetSqlist(list,4,&x);  
  45.     printf("The NO.4 data of the list is %d\n",x);  
  46.     SetSqlist(list,4,5);  
  47.     GetSqlist(list,4,&x);  
  48.     printf("After updataing! The NO.4 data of the list is %d\n",x);  
  49.   
  50.     printf("Delete data[4]!\n");  
  51.     DeleteSqlist(list,4);  
  52.     GetSqlist(list,4,&x);  
  53.     printf("Now data[4] = %d\n",x);  
  54.     printf("Now the legth of the list is %d\n",LengthSqlist(list));  
  55.   
  56.     ClearSqlist(list);  
  57.     if(EmptySqlist(list))  
  58.         printf("The list is empty!\n");  
  59.   
  60.     printf("Now the legth of the list is %d\n",LengthSqlist(list));  
  61.   
  62.     DestroySqlist(list);  
  63.   
  64.     printf("The list is destroyed!\n");  
  65.   
  66.     return 0;  
  67. }  

makefile:

[cpp] view plain copy
  1. OBJS = seqlist.o main.o  
  2. CFLAGS = -c -g -Wall  
  3. CC = gcc  
  4.   
  5. Test:$(OBJS)  
  6.     $(CC) -o Test -g $(OBJS)  
  7. main.o:main.c seqlist.h datatype.h  
  8.     $(CC) $(CFLAGS) main.c  
  9. seqlist.o:seqlist.c seqlist.h datatype.h  
  10.     $(CC) $(CFLAGS) seqlist.c  
  11.   
  12. clean:  
  13.     rm -rf Test *.o  

执行结果如下:

[cpp] view plain copy
  1. fs@ubuntu:~/qiang/list$ make  
  2. gcc -c -g -Wall seqlist.c  
  3. gcc -c -g -Wall main.c  
  4. gcc -o Test -g seqlist.o main.o  
  5. fs@ubuntu:~/qiang/list$ ./Test   
  6. list.last = 9, list = {2,4,6,8,10,12,14,16,18,20}  
  7. The length of the list is 10  
  8. The NO.4 data of the list is 10  
  9. After updataing! The NO.4 data of the list is 5  
  10. Delete data[4]!  
  11. Now data[4] = 12  
  12. Now the legth of the list is 9  
  13. The list is empty!  
  14. Now the legth of the list is 0  
  15. The list is destroyed! 
0 0
原创粉丝点击