马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
目次
一、数据结构的概念
什么是数据结构?
为什么还必要数据结构?
二、次序表
1.线性表
2.次序表和数组的区别
3.次序表分类
3.1静态次序表
3.2动态次序表
三、动态次序表的实现
一、数据结构的概念
什么是数据结构?
数据结构可以划分为数据和结构两部门。
数据是我们一样平常生活中相称广泛的东西,好比数值1、2、3...,教务系统的教师、学生、姓名等肉眼可以看到的一切事物都可以是数据;
而结构则是基于该数据的某一特点使用数组进行划分的方式,好比1、2、3等都是数字,而教师、学生等都是学校这个数组的内容。
数据结构是盘算机存储、构造数据的方式,数组是最基础的数据结构。
为什么还必要数据结构?
这时候就有了疑问,既然有了数组还为什么必要数据结构?
在我们一样平常现实工作当中我们会遇到大量的数据,如果只依靠数组这个基础的数据结构来进行数据处置惩罚的话会用到大量的人力物力,还会浪费很多的时间成本,也就是说数组无法满足复杂操作的运算。
二、次序表
1.线性表
线性表是n个具有相同特性的数据元素组成的有限序列,常见的线性表有:栈、队列、次序表、链表等等。
线性表在逻辑上是线性结构,也就说是一连的⼀条直线。但是在物理结构上并不⼀定是一连的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
比方:蔬菜可以分为绿叶类、瓜类、菌菇类等,线性表是指具有部门特性相同的一类数据结构的集合。
2.次序表和数组的区别
次序表和数组的唯一区别就是次序表对数组进行了封装,它增加了“增“、“删”、”查“、”改”四个功能接口。
3.次序表分类
3.1静态次序表
3.2动态次序表
三、动态次序表的实现
- //SeqList.c 示例代码
- #define _CRT_SECURE_NO_WARNINGS 1
- #include"SeqList.h"
- //初始化顺序表
- void SLInit(SL* sl)
- {
- sl->arr = NULL;
- sl->capacity = sl->length = 0;
- }
- //检查顺序表空间是否足够
- void CheckCapacity(SL* sl)
- {
- int Newcapacity = 0;
- if (sl->capacity == sl->length)
- {
- if (sl->capacity == 0)
- {
- Newcapacity = 2 * sl->capacity;
- }
- else
- {
- Newcapacity = 4;
- }
- SLDatatype* tmp = (SLDatatype*)realloc(sl->arr, Newcapacity * sizeof(SLDatatype));
- sl->arr = tmp;
- sl->capacity = Newcapacity;
- }
- }
- //头插
- void SLPushFront(SL* ps, int x)
- {
- assert(ps);
- CheckCapacity(ps);
- memmove(ps->arr+1, ps->arr, ps->length * sizeof(SLDatatype));
- ps->arr[0] = x;
- ps->length++;
- }
- //头删
- void SLPopFront(SL* ps)
- {
- assert(ps);
- assert(ps->length);
- memmove(ps->arr, ps->arr + 1, ps->length * sizeof(SLDatatype));
- ps->length--;
- }
- //尾插
- void SLPushBack(SL* ps, int x)
- {
- assert(ps);
- CheckCapacity(ps);
- ps->arr[ps->length++] = x;
- }
- //尾删
- void SLPopBack(SL* ps)
- {
- assert(ps);
- assert(ps->length);
- ps->length--;
- }
- //打印数据
- void SLPrint(SL* p)
- {
- for (int i = 0; i < p->length; i++)
- {
- printf("%d ", p->arr[i]);
- }
- }
- //顺序表销毁
- void SLDestory(SL* ps)
- {
- if (ps->arr)
- {
- free(ps->arr);
- }
- ps->arr = NULL;
- ps->capacity = 0;
- ps->length = 0;
- }
复制代码- //SeqList.h示例代码
- #pragma once
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<assert.h>
- //创建顺序表
- typedef int SLDatatype;
- typedef struct SeqList
- {
- SLDatatype* arr;
- SLDatatype length;//真正被填充的大小
- SLDatatype capacity;//被开辟的有效空间
- }SL;
- //初始化顺序表
- void SLInit(SL* sl);
- //检查顺序表空间是否满足插入条件
- void CheckCapacity(SL* sl);
- //头插
- void SLPushFront(SL* ps, int x);
- //头删
- void SLPopFront(SL* ps);
- //打印
- void SLPrint(SL* p);
- //尾插
- void SLPushBack(SL* ps, int x);
- //尾删
- void SLPopBack(SL* ps);
- //顺序表销毁
- void SLDestory(SL* ps);
复制代码- //test.c示例代码
- #define _CRT_SECURE_NO_WARNINGS 1
- #include"SeqList.h"
- void SLTest01()
- {
- SL sl;
- SLInit(&sl);
- //测试
- SLPushBack(&sl, 1);
- SLPushBack(&sl, 2);
- SLPushBack(&sl, 3);
- SLPushBack(&sl, 4);
- SLPushFront(&sl, 5);
- SLPushFront(&sl, 6);
- SLPushFront(&sl, 7);
- //SLPopFront(&sl);
- //SLPopFront(&sl);
- SLPopBack(&sl);
- //打印顺序表
- SLPrint(&sl);
- //销毁顺序表
- SLDestory(&sl);
- }
- int main()
- {
- SLTest01();
- return 0;
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |