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

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

线性表是一个线性结构,它是一个含有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 plaincopy
  1. #define N 100
  2. typedef int data_t;//这样定义的目的是为了代码便于维护,如果下次表中数据类型为char,修改比较方便;
  3. typedef struct
  4. {
  5. data_t data[N];//表的存储空间
  6. int last;//当前表尾指针,对我们对数据的定位起到很大的作用
  7. }sqlist_t,*sqlink_t;//顺序表类型

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

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

seqlist.c

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

main.c

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

makefile:

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

执行结果如下:

[cpp] view plaincopy
  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!

Linux C 数据结构---线性表相关推荐

  1. 数据结构-线性表之用队列实现栈用栈实现队列

    文章目录 **********用队列实现栈 一:思路 二:实现 (1)结构体定义 (2)初始化和销毁 (3)进"栈" (4)出"栈" 三:代码 ******** ...

  2. 数据结构-线性表之带头结点的双向循环链表

    文章目录 前言 实现 (1)结构定义 (2)基本函数 (3)操作实现 测试 代码 前言 链表的类型有很多种(根据带头或不带头,循环或非循环等等),但是我们重点研究的只有两种,一种结构非常简单是无头单向 ...

  3. 数据结构摧毁线性表用c语言,[简述]数据结构-线性表(c语言实现)

    [简述]数据结构-线性表(c语言实现)second60 20180422 1. 线性表的定义 线性表是具有相同特性的数据元素的一个有限序列. 2. 线性表抽象数据类型描述 ADT  List { 数据 ...

  4. 王道数据结构线性表:有读者认为直接去掉p结点会造成断链?

    王道数据结构线性表:有读者认为直接去掉p结点会造成断链? 我用图解的方式来说明一下,文字有点多,看起来比较眼疼,但是内容不多,希望能对你有帮助. 书上的代码 解释 (ps:对上面解释的一点补充↓)

  5. 数据结构——线性表(三)

    数据结构--线性表(三) 作者:黑衣侠客 一.线性表的定义 线性表,从名字来看,可以发现,是具有像线一样性质的表 线性表:零个或多个数据元素的有限序列. 首先,它是一个序列,也就是说,元素之间是有顺序 ...

  6. 数据结构-线性表(严书代码实现)

    数据结构-线性表的顺序表示代码 //头文件内容实现 #ifndef SEQLIST_H_INCLUDED #define SEQLIST_H_INCLUDED #include<string.h ...

  7. 数据结构-线性表-思维导图+小结

    数据结构-线性表思维导图+小结 1 数据结构-第二章-线性表-思维导图 2 数据结构-第二章-线性表-习题小结 2.1 概念性习题小结 2.2 操作性习题小结 1 数据结构-第二章-线性表-思维导图 ...

  8. 数据结构线性表基本操作

    数据结构线性表基本操作 基本内容 线性表的顺序表示和实现 线性表的顺序存储表示 顺序表中基本操作的实现 1.顺序表的初始化 2.取值 3.查找 4.插入 5.删除 线性表的链式表示和实现 单链表的定义 ...

  9. c语言构造一个空线性表l,数据结构线性表顺序结构的定义与实现C语言-Go语言中文社区...

    大家好,今天给大家总结了一下数据结构里面的线性表的顺序结构,顺序表表示的是用一组地址连续的存储单元依次存储线性表的数据元素,所以顺序结构的实现一般采用数组的方式来实现,存储空间也采用动态分配的方式.在 ...

最新文章

  1. Python_基础知识储备
  2. java压测请求线程数_程序员撕开京东 618 大促压测的另一面 | 原力计划
  3. c oracle 中文列名,C#获取ORACLE列名的有关问题,这次够详细了吧。求帮助
  4. java final keyword
  5. 苹果android投屏,iPhone手机如何投屏到智能电视?
  6. 静态时序分析——单周期
  7. IntelliJ Idea注释模板--类注释、方法注释
  8. 蚂蚁分类信息系统 5.8 信息浏览量后台自定义设置
  9. charles-无法抓取https包的解决办法及效果
  10. tomcat服务器的虚拟目录,Windows系统下安装Tomcat服务器和配置虚拟目录的方法
  11. 随手记_英语_50大英文经典句/美句
  12. TCPIP协议详解----网络基础知识
  13. 用于交通数据预测的深度时空3维卷积神经网络
  14. c#日期时间的操作的函数总结
  15. 什么叫特立独行,其实不过是活在自己的节奏里
  16. 《linux 网络日志分析与流量监控》记录
  17. 完成对靓号号码的筛选
  18. sql入门(3)------SQLServer3
  19. 为仿真器添加eCos多线程调试支持,GDBServer Extender 0.0.1 使用说明
  20. 天道酬勤系列之Python 希尔排序

热门文章

  1. date数据存入mysql_Date对象存入mysql数据库
  2. 最小二乘法以及RANSAC(随机采样一致性)思想及实现
  3. leetcode 746. 使用最小花费爬楼梯(dp)
  4. python练习_如何使用Logzero在Python中练习记录
  5. drop sql语句_用于从表中删除数据SQL Drop View语句
  6. 自动化yaml文件_从YAML到TypeScript:开发人员对云自动化的看法
  7. 前端绘制绘制图表_绘制图表(第2页):JavaScript图表库的比较
  8. 如何选择适合自己的CMS建站系统
  9. JS JAVASCRIPT 判断两个日期相隔多少天
  10. 利用Mac创建一个 IPv6 WIFI 热点