赞
踩
数据结构是由“数据”和“结构”两词组合而来。
什么是数据?
常见的数值1、2、3、4…、教务系统里保存的用户信息(姓名、性别、年龄、学历等等)、网页肉眼可以看到的信息(文字、图片、视频等等),这些都是数据.
什么是结构?
当我们想要使用大量使用同一类型的数据时,通过手动定义大量的独立的变量对于程序来说,可读性非常差,我们可以借助数组这样的数据结构将大量的数据组织在一起,结构也可以理解为组织数据的方式。
想要找到草原上名叫“咩咩”的羊很难,但是从羊圈里找到1号羊就很简单,羊圈这样的结构有效将羊群组织起来。
概念:数据结构是计算机存储、组织数据的⽅式。数据结构是指相互之间存在⼀种或多种特定关系的数据元素的集合。数据结构反映数据的内部构成,即数据由那部分构成,以什么方式构成,以及数据元素之间呈现的结构。
总结:
1)能够存储数据(如顺序表、链表等结构)
2)存储的数据能够⽅便查找
最基础的数据结构:数组
【思考】有了数组,为什么还要学习其他的数据结构?
假定数组有10个空间,已经使用了5个,向数组中插⼊数据步骤:
结论:最基础的数据结构能够提供的操作已经不能完全满足复杂算法实现
线性表的概念:
顺序表和数组的区别
顺序表分类
概念:使用定长数组存储元素
静态顺序表
静态顺序表缺陷:空间给少了不够用,给多了造成空间浪费
动态顺序表
在我们实现顺序表的时候,首先定义出结构体,还有数据类型的定义
头文件:SeqList.h
其中size
是记录多少个有效数据,为什么要定义这个capacity
呢?因为是动态的顺序表,今天100的空间,明天200的空间,所以就是capacity
空间有多大,空间容量~~
我们可以看到我们当前的顺序表只能存储的是整形的数据,假如我把顺序表实现好了,我给其他人使用的时候,那我这里是整形,那能不能是char类型,double型…,我这里为什么一定要定义int类型?
那么我们这里就要使用typedef
来定义类型~~
那我们也给这个结构体也重命名一个名字~~
#pragma once
//头文件
#include<stdio.h>
//数据类型的定义
typedef int SLDataType;
//结构体的定义
typedef struct SeqList
{
SLDataType* a;
int size;
int capacity;
}SL;
函数的声明:SeqList.h
//初始化顺序表
void SLInit(SL s);
函数的实现:SeqList.c
//初始化顺序表
void SLInit(SL* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
有初始化顺序表就有销毁顺序表,接下来我们就来看看销毁顺序表是怎么完成的~~
函数的声明:SeqList.h
//销毁顺序表
void SLDestroy(SL* ps);
函数的实现:SeqList.c
free
NULL
所以我们就需要断言~~void SeqListDestroy(SeqList* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}
这个时候就要实现顺序表的主体功能了~~
void SLCheckCapacity(SL* ps)
{
assert(ps);
if (ps->size == ps->capacity) {
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
//空间不够,需要扩容
SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType));
if (tmp == NULL) {
perror("realloc fail!");
return;
}
ps->a = tmp;
ps->capacity = newCapacity;
}
}
函数的声明:SeqList.h
//尾插
void SLPushBack(SL* ps, SLDataType x);
函数的实现:SeqList.c
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
//直接插入数据
ps->a[ps->size++] = x;
}
函数的声明:SeqList.h
void SLPushFront(SL* ps, SLDataType x);
函数的实现:SeqList.c
//头插 void SLPushFront(SL* ps, SLDataType x) { assert(ps); //判断空间是否足够,不够则扩容 SLCheckCapacity(ps); //空间足够,历史数据后移一位 // 挪动数据 int end = ps->size - 1; while (end >= 0) { ps->a[end + 1] = ps->a[end]; --end; } ps->a[0] = x; ps->size++; }
函数的声明:SeqList.h
bool SLIsEmpty(SL* ps);
函数的声明:SeqList.h
bool SLIsEmpty(SL* ps)
{
assert(ps);
return ps->size == 0;
}
函数的声明:SeqList.h
void SLPopBack(SL* ps);
函数的实现:SeqList.c
//尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(!SLIsEmpty(ps));
ps->size--;
}
函数的声明:SeqList.h
void SeqListPrint(SeqList* ps);
函数的实现:SeqList.c
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
函数的声明:SeqList.h
void SLPopFront(SL* ps);
函数的实现:SeqList.c
//顺序表的头删
//头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(!SLIsEmpty(ps));
//让后面的数据往前挪动一位
for (int i = 0; i < ps->size - 1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
函数的声明:SeqList.h
void SLInsert(SL* ps, int pos, SLDataType x);
函数的实现:SeqList.c
//在顺序表pos位置插入x void SLInsert(SL* ps, int pos, SLDataType x) { assert(ps); //需要限制范围 assert(pos >= 0 && pos <= ps->size); //检查容量 SLCheckCapacity(ps); for (int i = ps->size; i > pos; i--) { ps->a[i] = ps->a[i - 1]; } ps->a[pos - 1] = x; ps->size++; }
函数的声明:SeqList.h
void SLErase(SL* ps, int pos);
函数的实现:SeqList.c
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(!SLIsEmpty(ps));
assert(pos >= 0 && pos <= ps->size);
for (int i = pos - 1; i < ps->size - 1; i++)
{
ps->a[i] = ps->a[i + 1];
}
ps->size--;
}
函数的声明:SeqList.h
bool SLFind(SL* ps, SLDataType x);
函数的实现:SeqList.c
bool SLFind(SL* ps, SLDataType x)
{
assert(ps);
assert(!SLIsEmpty(ps));
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
return true;
}
return false;
}
SepList.h
#pragma once #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<stdbool.h> //类型重命名 typedef int SLDataType; //结构体的定义 typedef struct SeqList { SLDataType* a; int size;//记录多少个有效数据 int capacity;//空间容量 }SL; //初始化顺序表 void SLInit(SL* ps); //销毁顺序表 void SLDestroy(SL* ps); //尾插 void SLPushBack(SL* ps, SLDataType x); //头插 void SLPushFront(SL* ps, SLDataType x); //尾删 void SLPopBack(SL* ps); //头删 void SLPopFront(SL* ps); //打印顺序表 void SLPrint(SL* ps); //判断是否为空 bool SLIsEmpty(SL* ps); //在任意位置之前插入 void SLInsert(SL* ps, int pos, SLDataType x); //在任意位置之前删除 void SLErase(SL* ps, int pos); //查找顺序表 bool SLFind(SL* ps, SLDataType x);
SepList.c
#include"SepList.h" //初始化顺序表 void SLInit(SL* ps) { assert(ps); ps->a = NULL; ps->capacity = ps->size = 0; } //销毁顺序表 void SLDestroy(SL* ps) { if(ps->a) free(ps->a); ps->capacity = ps->size = 0; } //检查容量 void SLCheckCapacity(SL* ps) { assert(ps); if (ps->size == ps->capacity) { int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity; //空间不够,需要扩容 SLDataType* tmp = (SLDataType*)realloc(ps->a, newCapacity * sizeof(SLDataType)); if (tmp == NULL) { perror("realloc fail!"); return; } ps->a = tmp; ps->capacity = newCapacity; } } //尾插 void SLPushBack(SL* ps, SLDataType x) { assert(ps); //判断空间够不够? SLCheckCapacity(ps); //直接插入数据 ps->a[ps->size++] = x; } //头插 void SLPushFront(SL* ps, SLDataType x) { assert(ps); //判断空间是否足够,不够则扩容 SLCheckCapacity(ps); //空间足够,历史数据后移一位 // 挪动数据 int end = ps->size - 1; while (end >= 0) { ps->a[end + 1] = ps->a[end]; --end; } ps->a[0] = x; ps->size++; } //尾删 void SLPopBack(SL* ps) { assert(ps); assert(!SLIsEmpty(ps)); ps->size--; } //头删 void SLPopFront(SL* ps) { assert(ps); assert(!SLIsEmpty(ps)); //让后面的数据往前挪动一位 for (int i = 0; i < ps->size - 1; i++) { ps->a[i] = ps->a[i + 1]; } ps->size--; } //打印顺序表 void SLPrint(SL* ps) { assert(ps); assert(!SLIsEmpty(ps)); for (int i = 0; i < ps->size; i++) { printf("%d ", ps->a[i]); } printf("\n"); } //判断空间是否为空 bool SLIsEmpty(SL* ps) { assert(ps); return ps->size == 0; } //在任意位置之前插入 void SLInsert(SL* ps, int pos, SLDataType x) { assert(ps); //需要限制范围 assert(pos >= 0 && pos <= ps->size); //检查容量 SLCheckCapacity(ps); for (int i = ps->size; i > pos; i--) { ps->a[i] = ps->a[i - 1]; } ps->a[pos - 1] = x; ps->size++; } //在任意位置之前删除 void SLErase(SL* ps, int pos) { assert(ps); assert(!SLIsEmpty(ps)); assert(pos >= 0 && pos <= ps->size); for (int i = pos - 1; i < ps->size - 1; i++) { ps->a[i] = ps->a[i + 1]; } ps->size--; } //查找顺序表 bool SLFind(SL* ps, SLDataType x) { assert(ps); for (int i = 0; i < ps->size; i++) { if (ps->a[i] == x) return true; } return false; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。