Linux C 数据结构---线性表
数据结构指的是数据元素及数据元素之间的相互关系,包含下面三方面的内容:
其中,线性表是最基本、最简单、也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。线性表的逻辑结构简单,便于实现和操作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。
线性表是一个线性结构,它是一个含有n≥0个结点的有限序列,对于其中的结点,有且仅有一个开始结点没有前驱但有一个后继结点,有且仅有一个终端结点没有后继但有一个前驱结点,其它的结点都有且仅有一个前驱和一个后继结点。
特征:
1.集合中必存在唯一的一个“第一元素”;
2.集合中必存在唯一的一个 “最后元素” ;
3.除最后一个元素之外,均有 唯一的后继(后件);
4.除第一个元素之外,均有 唯一的前驱(前件);
线性表作为一种基本的数据结构类型,在计算机存储器的映像(或表示)一般有两种形式:
1、顺序映像---即我们常用的数组
2、链式映像---即我们常用的链表
今天,我们先来学习顺序存储结构:
一、顺序存储结构的表示
1、顺序存储结构的特点:
1)逻辑上相邻的元素A(i) A(i+1),其存储位置也是相邻的;
2)对数据元素A(i)的存取为随机存取或地址存取。
3)存储密度高。存储密度D=(数据结构中的元素所占存储空间)/(整个数据结构所占空间)
2、顺序存储结构的不足:
对表的插入和删除等运算的时间复杂度较差。
3、顺序存储结构的表示:
在C语言中,一维数组的元素也是存放于一片连续的存储空间中,故可借助于C语言中一维数组类型来描述线性表的存储结构,即
- #define N 100
- typedef int data_t;//这样定义的目的是为了代码便于维护,如果下次表中数据类型为char,修改比较方便;
- typedef struct
- {
- data_t data[N];//表的存储空间
- int last;//当前表尾指针,对我们对数据的定位起到很大的作用
- }sqlist_t,*sqlink_t;//顺序表类型
指针 L 指向一个顺序表,我们的数据{a0,a1,a2........ last};
- sqlink_t L;
- 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
- #ifndef _SEQ_LIST_H_
- #define _SEQ_LIST_H_
- #include "datatype.h"
- #define MAX 100
- typedef struct {
- data_t data[MAX];
- int last; /* pointer to the position
- * in the array 'data' where
- * stores the last element of
- * the list
- */
- } seqlist_t;
- /*
- * create a list and init it as empty
- * Input : void
- * Output: void
- * Return: new list, NULL when failed
- */
- seqlist_t *CreateEmptySqlist();
- /*
- * destroy a list
- * Input : the list to be destroied.
- * Output: void
- * Return: void
- */
- void DestroySqlist(seqlist_t *list);
- /*
- * clear the list and reset it as empty
- * Input : the list to be cleared.
- * Output: void
- * Return: void
- */
- void ClearSqlist(seqlist_t *list);
- /*
- * judge if the list is empty
- * Input: the list to be tested.
- * Output: void
- * Return:
- * 1: list is empty
- * 0: not
- * -1: error, e.g. the list is invalid
- */
- int EmptySqlist(seqlist_t *list);
- /*
- * judge if the list is full
- * Input : the list to be tested.
- * Output: void
- * Return:
- * 1 : list is full
- * 0 : not
- * -1: error
- */
- int FullSqlist(seqlist_t *list);
- /*
- * get length of the list
- * Input : the list to be tested.
- * Output: void
- * Return:
- * >= 0: length of the list;
- * -1 : means error
- */
- int LengthSqlist(seqlist_t *list);
- /*
- * get data of element at specified position
- * Input :
- * list : the list to be operated.
- * at : the position where to get the element at,
- * position index starts from zero.
- * Output:
- * x : the data value returned
- * Return:
- * 0 : success;
- * -1: error, e.g. list is invalid; 'at' extends
- * the range of the list
- */
- int GetSqlist(seqlist_t *list, int at, data_t *x);
- /*
- * set/update data of element at specified position
- * Input :
- * list : the list to be operated.
- * at : the position at where to set the element,
- * position index starts from zero
- * x : the new data value
- * Output: void
- * Return:
- * 0 : success;
- * -1: error, e.g. list is invalid; 'at' extends the
- * range of the list
- */
- int SetSqlist(seqlist_t *list, int at, data_t x);
- /*
- * Insert element at the specified position. If the "at" exceed the
- * upper limit of the list, append the data at the end of the list.
- * e.g. insert a new data into a {}.
- * Input :
- * list : the list to be operated.
- * at : the position at which to insert the new element,
- * position index starts from zero.
- * x : the data to be inserted
- * Output: void
- * Return:
- * 0 : success;
- * <0: error
- */
- int InsertSqlist(seqlist_t *list, int at, data_t x);
- /*
- * delete the element by the position
- * Input :
- * list : the list to be operated.
- * at : the position at which to delete the element,
- * position index starts from zero
- * Output : void
- * Return :
- * 0 : success;
- * !0 : error
- */
- int DeleteSqlist(seqlist_t *list, int at);
- #endif /* _SEQ_LIST_H_ */
seqlist.c
- #include <stdio.h>
- #include <stdlib.h>
- #include "seqlist.h"
- seqlist_t *CreateEmptySqlist()
- {
- seqlist_t *list;
- list = (seqlist_t *)malloc(sizeof(seqlist_t));
- if(list != NULL)
- {
- list->last = -1;//list->last初始化,空表中,list-last = -1;
- }
- return list;
- }
- void DestroySqlist(seqlist_t *list)
- {
- if (list == NULL)
- return ;
- free(list);
- return;
- }
- void ClearSqlist(seqlist_t *list)
- {
- if (list == NULL)
- return ;
- list->last = -1;//清空线性表
- }
- int EmptySqlist(seqlist_t *list)
- {
- if (list == NULL)
- return -1;
- if(list->last == -1)//空表的标志
- return 1;
- else
- return 0;
- }
- int FullSqlist(seqlist_t *list)
- {
- if (list == NULL)
- return -1;
- if (MAX - 1 == list->last)
- return 1;
- else
- return 0;
- }
- int LengthSqlist(seqlist_t *list)
- {
- if (list == NULL)
- return -1;
- else
- return (list->last + 1);
- }
- int GetSqlist(seqlist_t *list, int at, data_t *x) //data_t *x为出参
- {
- if(list == NULL || at < 0 || at > list->last)
- return -1;
- *x = list->data[at];
- return 0;
- }
- int SetSqlist(seqlist_t *list, int at, data_t x)//data_t x为入参
- {
- if(list == NULL || at < 0 || (at > list->last))
- return -1;
- list->data[at] = x;
- return 0;
- }
- int InsertSqlist(seqlist_t *list, int at, data_t x)
- {
- int j;
- if(list == NULL || at < 0 || FullSqlist(list))
- return -1;
- if(at > list->last)//此情况比较特殊,如表中长度是50,却要插在60前面,我们这里将其插在50后面,即at = list->last + 1
- {
- at = list->last + 1;//若是空表{},list->last为-1,此时将其放在第0位;
- }
- else
- {
- for(j = list->last; j >= at; j--)
- {
- list->data[j+1] = list->data[j];
- }
- }
- list->data[at] = x;
- list->last++;//list->last要+1
- return 0;
- }
- int DeleteSqlist(seqlist_t *list, int at)
- {
- int i;
- if(list == NULL || at < 0 || (at > list->last))
- return -1;
- for(i = at + 1; i <= list->last;i++)
- list->data[i-1] = list->data[i];
- list->last--;
- return 0;
- }
main.c
- #include <stdio.h>
- #include <stdlib.h>
- #include "seqlist.h"
- #include "datatype.h"
- void iterate_list(seqlist_t *list)
- {
- int i;
- printf("list.last = %d, list = {", list->last);
- for (i = -1; i < list->last;) {
- printf("%d,", list->data[++i]);
- }
- if (LengthSqlist(list) > 0)
- printf("\b}\n");
- else
- printf("}\n");
- }
- int main(int argc, const char *argv[])
- {
- int i;
- int length;
- data_t a[10] = {2,4,6,8,10,12,14,16,18,20};
- data_t x;
- seqlist_t *list;
- list = CreateEmptySqlist();
- if (list == NULL)
- return -1;
- for(i = 0; i < 10;i++)
- {
- if((InsertSqlist(list,i,a[i])) < 0)
- break;
- }
- iterate_list(list);
- length = LengthSqlist(list);
- printf("The length of the list is %d\n",length);
- GetSqlist(list,4,&x);
- printf("The NO.4 data of the list is %d\n",x);
- SetSqlist(list,4,5);
- GetSqlist(list,4,&x);
- printf("After updataing! The NO.4 data of the list is %d\n",x);
- printf("Delete data[4]!\n");
- DeleteSqlist(list,4);
- GetSqlist(list,4,&x);
- printf("Now data[4] = %d\n",x);
- printf("Now the legth of the list is %d\n",LengthSqlist(list));
- ClearSqlist(list);
- if(EmptySqlist(list))
- printf("The list is empty!\n");
- printf("Now the legth of the list is %d\n",LengthSqlist(list));
- DestroySqlist(list);
- printf("The list is destroyed!\n");
- return 0;
- }
makefile:
- OBJS = seqlist.o main.o
- CFLAGS = -c -g -Wall
- CC = gcc
- Test:$(OBJS)
- $(CC) -o Test -g $(OBJS)
- main.o:main.c seqlist.h datatype.h
- $(CC) $(CFLAGS) main.c
- seqlist.o:seqlist.c seqlist.h datatype.h
- $(CC) $(CFLAGS) seqlist.c
- clean:
- rm -rf Test *.o
执行结果如下:
- fs@ubuntu:~/qiang/list$ make
- gcc -c -g -Wall seqlist.c
- gcc -c -g -Wall main.c
- gcc -o Test -g seqlist.o main.o
- fs@ubuntu:~/qiang/list$ ./Test
- list.last = 9, list = {2,4,6,8,10,12,14,16,18,20}
- The length of the list is 10
- The NO.4 data of the list is 10
- After updataing! The NO.4 data of the list is 5
- Delete data[4]!
- Now data[4] = 12
- Now the legth of the list is 9
- The list is empty!
- Now the legth of the list is 0
- The list is destroyed!
Linux C 数据结构---线性表相关推荐
- 数据结构-线性表之用队列实现栈用栈实现队列
文章目录 **********用队列实现栈 一:思路 二:实现 (1)结构体定义 (2)初始化和销毁 (3)进"栈" (4)出"栈" 三:代码 ******** ...
- 数据结构-线性表之带头结点的双向循环链表
文章目录 前言 实现 (1)结构定义 (2)基本函数 (3)操作实现 测试 代码 前言 链表的类型有很多种(根据带头或不带头,循环或非循环等等),但是我们重点研究的只有两种,一种结构非常简单是无头单向 ...
- 数据结构摧毁线性表用c语言,[简述]数据结构-线性表(c语言实现)
[简述]数据结构-线性表(c语言实现)second60 20180422 1. 线性表的定义 线性表是具有相同特性的数据元素的一个有限序列. 2. 线性表抽象数据类型描述 ADT List { 数据 ...
- 王道数据结构线性表:有读者认为直接去掉p结点会造成断链?
王道数据结构线性表:有读者认为直接去掉p结点会造成断链? 我用图解的方式来说明一下,文字有点多,看起来比较眼疼,但是内容不多,希望能对你有帮助. 书上的代码 解释 (ps:对上面解释的一点补充↓)
- 数据结构——线性表(三)
数据结构--线性表(三) 作者:黑衣侠客 一.线性表的定义 线性表,从名字来看,可以发现,是具有像线一样性质的表 线性表:零个或多个数据元素的有限序列. 首先,它是一个序列,也就是说,元素之间是有顺序 ...
- 数据结构-线性表(严书代码实现)
数据结构-线性表的顺序表示代码 //头文件内容实现 #ifndef SEQLIST_H_INCLUDED #define SEQLIST_H_INCLUDED #include<string.h ...
- 数据结构-线性表-思维导图+小结
数据结构-线性表思维导图+小结 1 数据结构-第二章-线性表-思维导图 2 数据结构-第二章-线性表-习题小结 2.1 概念性习题小结 2.2 操作性习题小结 1 数据结构-第二章-线性表-思维导图 ...
- 数据结构线性表基本操作
数据结构线性表基本操作 基本内容 线性表的顺序表示和实现 线性表的顺序存储表示 顺序表中基本操作的实现 1.顺序表的初始化 2.取值 3.查找 4.插入 5.删除 线性表的链式表示和实现 单链表的定义 ...
- c语言构造一个空线性表l,数据结构线性表顺序结构的定义与实现C语言-Go语言中文社区...
大家好,今天给大家总结了一下数据结构里面的线性表的顺序结构,顺序表表示的是用一组地址连续的存储单元依次存储线性表的数据元素,所以顺序结构的实现一般采用数组的方式来实现,存储空间也采用动态分配的方式.在 ...
最新文章
- Python_基础知识储备
- java压测请求线程数_程序员撕开京东 618 大促压测的另一面 | 原力计划
- c oracle 中文列名,C#获取ORACLE列名的有关问题,这次够详细了吧。求帮助
- java final keyword
- 苹果android投屏,iPhone手机如何投屏到智能电视?
- 静态时序分析——单周期
- IntelliJ Idea注释模板--类注释、方法注释
- 蚂蚁分类信息系统 5.8 信息浏览量后台自定义设置
- charles-无法抓取https包的解决办法及效果
- tomcat服务器的虚拟目录,Windows系统下安装Tomcat服务器和配置虚拟目录的方法
- 随手记_英语_50大英文经典句/美句
- TCPIP协议详解----网络基础知识
- 用于交通数据预测的深度时空3维卷积神经网络
- c#日期时间的操作的函数总结
- 什么叫特立独行,其实不过是活在自己的节奏里
- 《linux 网络日志分析与流量监控》记录
- 完成对靓号号码的筛选
- sql入门(3)------SQLServer3
- 为仿真器添加eCos多线程调试支持,GDBServer Extender 0.0.1 使用说明
- 天道酬勤系列之Python 希尔排序
热门文章
- date数据存入mysql_Date对象存入mysql数据库
- 最小二乘法以及RANSAC(随机采样一致性)思想及实现
- leetcode 746. 使用最小花费爬楼梯(dp)
- python练习_如何使用Logzero在Python中练习记录
- drop sql语句_用于从表中删除数据SQL Drop View语句
- 自动化yaml文件_从YAML到TypeScript:开发人员对云自动化的看法
- 前端绘制绘制图表_绘制图表(第2页):JavaScript图表库的比较
- 如何选择适合自己的CMS建站系统
- JS JAVASCRIPT 判断两个日期相隔多少天
- 利用Mac创建一个 IPv6 WIFI 热点