前言
今天开始进入数据结构的学习,要学习的第一种数据结构就是顺序表。
在介绍顺序表之前先介绍一下数据结构,数据结构是数据在内存中的存储,组织的方式。数据结构是指相互之间存在一种或多种特定关系的数据的集合。接下来介绍顺序表。
一. 顺序表
1. 顺序表的概念
顺序表是一种底层是数组的线性表,它和数组一样通过下标访问元素,但是和数组不同的地方在于它可以不是定长的,动态顺序表的大小是可以通过动态内存申请得来。(线性表是在逻辑结构上一定连续,物理结构上不一定连续的具有相同特性数据的序列)。顺序表在物理结构和逻辑结构上都是连续的,因为其底层是数组。
2. 顺序表的分类
- 静态顺序表:内存大小是确定的
- 动态顺序表:内存大小是可以改变
对于静态顺序表,内存小了可能会导致数据的丢失,而内存大了又会导致内存空间的浪费;而动态顺序表可以扩容,不会导致数据的丢失,内存的浪费也没有像静态顺序表那么大。因此我们常用动态顺序表而不常用静态顺序表。
3.动态顺序表的功能
对于动态顺序表我们要实现以下功能:
- 顺序表的初始化和销毁
- 数据的插入:头插和尾插
- 数据的删除:头删和尾删
- 在指定位置删除/插入数据
- 数据的查找
二. 动态顺序表的代码实现
对于顺序表的实现我们分多个文件来写,首先创建一个头文件 (SeqList.h)来声明顺序表结构和实现顺序表的方法,在源文件(SeqList.c)文件中实现顺序表,另建一个.c文件用于测试。
以下是头文件
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;//将int类型重命名为SLDataType便于以后类型的更换
typedef struct SeqList
{
SLDataType* arr;
int size;//元素个数
int capacity;//内存大小
}SL;//将结构体类型冲定义为SL后续使用更方便
//顺序表的初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestory(SL* ps);
//尾插
void SLPushBack(SL* ps, SLDataType x);
//头插
void SLPushFront(SL* ps, SLDataType x);
//尾删
void SLPopBack(SL* ps);
//头删
void SLPopFront(SL* ps);
//在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x);
//删除指定位置的数据
void SLErase(SL* ps, int pos);
//查找数据
int SLFind(SL* ps, SLDataType x);
下面我们对这些函数一一进行实现。
1. 顺序表初始化和销毁
1.1 顺序表初始化
SeqList.c
#include"SeqList.h"
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->size = 0;
ps->capacity = 0;
}
SLInit函数接收一个结构体指针,然后对结构体解引用找到arr并将其置为NULL,找到size和capacity把它们初始化为0。
#include"SeqList.h"
void Test()
{
SL sl;
SLInit(&sl);
}
int main()
{
Test();
return 0;
}
测试一下
没有问题,接着往下实现。
1.2 顺序表的销毁
有句话说得好:“有借有还,再借不难”。动态顺序表开辟的空间是动态内存申请来的,在不使用之后这些内存要还给操作系统,下面我们来实现顺序表的销毁。
void SLDestory(SL* ps)
{
if (ps->arr)
{
ps->arr = NULL;
}
ps->capacity = 0;
ps->size = 0;
}
同样,SLDestory函数也是接收一个结构体指针,进入函数后,执行if语句,如果arr不为空,那就将其置空那个,再把size和capacity置为0。这样就完成了顺序表的销毁。
2. 顺序表的头插和尾插
2.1 顺序表的尾插
尾插 顾名思义就是在顺序表的末尾插入数据
但是在这里我们要考虑一下内存够不够用,上图中,size=5,capacity也等于5,当我们通过size下标访问时就会超出arr申请的空间也就是越界访问,因此我们在这种情况下要进行扩容。扩容一般按照原空间的2倍和进行扩容。
在插入数据后,对应的size大小也要进行+1!!!
下面是代码实现:
void SLPushBack(SL* ps, SLDataType x)
{
if (ps->size == ps->capacity)//判断内存够不够
{
int newcapacity = (ps->capacity == 0) ? 4 : 2 * ps->capacity;//先判断一下capacity是否为0
//若为0返回4;非零就返回原来的2倍
//扩容
SLDataType* tmp = (SLDataType*)realloc(ps->arr, sizeof(SLDataType) * newcapacity);
if (tmp == NULL)//如果开辟失败就打印错误信息,然后直接退出程序
{
perror("realloc");
exit(1);
}
ps->arr = tmp;//扩容成功后才可以将tmp赋给arr,如果不成功会返回NULL,
//那arr就成空了,之前的数据也会丢失。
ps->capacity = newcapacity;
}
ps->arr[ps->size] = x;
ps->size++;
}
这里realloc函数第2个参数是扩容后的空间大小,单位是字节,因此一定要乘上数据类型的大小。
测试一下:
传过空指针报错了,为了增加代码的健壮性我们可以在这个函数中加上assert断言,这样就不会发生这种现象了。
2.2 顺序表的头插
在插入数据之前还是要看一下内存够不够,然后在顺序表最前面插入数据,后面的数据要依次往后挪动1位,那应该怎们挪动呢?
像上面如果从前往后挪动的话就会把后面的数据覆盖,因此要从后往前挪动
最后是arr[1]=arr[0]。
代码实现:
void SLPushFront(SL * ps, SLDataType x)
{
assert(ps);
if (ps->size == ps->capacity)
{
int newcapacity = (ps->capacity == 0) ? 4 : 2 * ps->capacity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, sizeof(SLDataType) * newcapacity);
if (tmp == NULL)
{
perror("realloc");
exit(1);
}
ps->arr = tmp;
ps->capacity = newcapacity;
}
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = x;
ps->size++;
}
为了更方便的看出结果,我又定义了一个打印顺序表的函数,代码如下:
void SLPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
没有问题。但是大家发现了,在头插和尾插这两个函数中都有检查内存够不够的功能,使代码看着很冗余,于是我将它封装成了函数,整个头插和尾插的优化后代码如下:
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newcapacity = (ps->capacity == 0) ? 4 : 2 * ps->capacity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, sizeof(SLDataType) * newcapacity);
if (tmp == NULL)
{
perror("realloc");
exit(1);
}
ps->arr = tmp;
ps->capacity = newcapacity;
}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size] = x;
ps->size++;
}
//头插
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[0] = x;
ps->size++;
}
3. 顺序表的头删和尾删
3.1 顺序表的尾删
尾删就是将顺序表的最后一个元素删除(元素个数不能为0),实际上就是将顺序表的大小-1即可,最后一个元素并不会影响后续的增删改查。
代码实现:
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
ps->size--;
}
3.2 顺序表的头删
将顺序表的第一个元素删除,后面的元素依次往前移动,分析一下,应该是从前往后依次往前移。
代码如下:
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
for (int i = 0; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
4. 顺序表在指定位置删除添加数据
4.1 在指定位置前插入数据
要在pos=2的位置插入6
代码如下:
void SLInsert(SL* ps,int pos,SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);//保证顺序表的下标合法
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos] = x;
ps->size++;
}
4.2 在指定下标删除数据
把上面添加的6删除掉
pos后的元素依次往前挪。
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
for (int i = pos; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
这里要注意在第2个assert进行判断的时候,pos的右边界不能取到size,因为size下标对应没有元素,访问就会越界。
5. 顺序表元素的查找
这里我们让SLFind函数的返回值是整型,如果找到该元素就返回该元素所对应的下标,如果没有找到就返回-1。用一个简单的for循环实现,代码如下:
int SLFind(SL* ps, SLDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == x)
{
return i;
}
}
return -1;
}
三. 其他方法实现
以上我们就实现好了顺序表,但是对于头插,头删,在指定位置删除和插入数据我们还可以不用循环实现,借助库函数memmove也可以实现这几项功能。
1. memmove实现头插
我们把整体往后挪动了1位
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
memmove(ps->arr + 1, ps->arr, ps->size * sizeof(SLDataType));
ps->arr[0] = x;
ps->size++;
}
也就是把arr[0]以后的整体元素移动到arr[1]上
2.memmove实现头删
将arr[1]及其后面的size-1个元素移动到arr[0]的位置上
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
memmove(ps->arr, ps->arr + 1, sizeof(SLDataType) * ps->size - 1);
ps->size--;
}
3. memmove实现指定位置前插入数据
对于下面的顺序表我要在pos=2的元素之前插入6
代码如下:
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);
memmove(ps->arr + pos + 1, ps->arr + pos, sizeof(SLDataType) * (ps->size - pos));
ps->arr[pos] = x;
ps->size++;
}
4. memmove实现指定位置删除数据
对于这个顺序表要删除pos = 2位置的元素
代码如下:
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
memmove(ps->arr + pos, ps->arr + pos + 1, sizeof(SLDataType) * (ps->size - pos - 1));
ps->size--;
}
四. 整体代码呈现
1. SeqList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* arr;
int size;//元素个数
int capacity;//内存大小
}SL;
//顺序表的初始化
void SLInit(SL* ps);
//顺序表的销毁
void SLDestory(SL* ps);
//尾插
void SLPushBack(SL* ps, SLDataType x);
//头插
void SLPushFront(SL* ps, SLDataType x);
//尾删
void SLPopBack(SL* ps);
//头删
void SLPopFront(SL* ps);
//在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x);
//删除指定位置的数据
void SLErase(SL* ps, int pos);
//查找数据
int SLFind(SL* ps, SLDataType x);
//顺序表打印
void SLPrint(SL* ps);
2. SeqList.c
#include"SeqList.h"
//顺序表的初始化
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->size = 0;
ps->capacity = 0;
}
//顺序表的销毁
void SLDestory(SL* ps)
{
if (ps->arr)
{
ps->arr = NULL;
}
ps->capacity = 0;
ps->size = 0;
}
//检查内存是否够用
void SLCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newcapacity = (ps->capacity == 0) ? 4 : 2 * ps->capacity;
SLDataType* tmp = (SLDataType*)realloc(ps->arr, sizeof(SLDataType) * newcapacity);
if (tmp == NULL)
{
perror("realloc");
exit(1);
}
ps->arr = tmp;
ps->capacity = newcapacity;
}
}
//顺序表的尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size] = x;
ps->size++;
}
void SLPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
//顺序表的头插
//void SLPushFront(SL* ps, SLDataType x)
//{
// assert(ps);
// SLCheckCapacity(ps);
// for (int i = ps->size; i > 0; i--)
// {
// ps->arr[i] = ps->arr[i - 1];
// }
// ps->arr[0] = x;
// ps->size++;
//}
//尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
ps->size--;
}
//头删
//void SLPopFront(SL* ps)
//{
// assert(ps);
// assert(ps->size);
// for (int i = 0; i < ps->size - 1; i++)
// {
// ps->arr[i] = ps->arr[i + 1];
// }
// ps->size--;
//}
//在指定位置前插入数据
//void SLInsert(SL* ps,int pos,SLDataType x)
//{
// assert(ps);
// assert(pos >= 0 && pos <= ps->size);//保证顺序表的下标合法
// for (int i = ps->size; i > pos; i--)
// {
// ps->arr[i] = ps->arr[i - 1];
// }
// ps->arr[pos] = x;
// ps->size++;
//}
//在指定位置删除数据
//void SLErase(SL* ps, int pos)
//{
// assert(ps);
// assert(pos >= 0 && pos < ps->size);
// for (int i = pos; i < ps->size - 1; i++)
// {
// ps->arr[i] = ps->arr[i + 1];
// }
// ps->size--;
//}
//数据查找
int SLFind(SL* ps, SLDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == x)
{
return i;
}
}
return -1;
}
//memmove实现头插
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
memmove(ps->arr + 1, ps->arr, ps->size * sizeof(SLDataType));
ps->arr[0] = x;
ps->size++;
}
//memmove实现头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
memmove(ps->arr, ps->arr + 1, sizeof(SLDataType) * ps->size - 1);
ps->size--;
}
//memmove实现指定位置前插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos >= 0 && pos <= ps->size);//保证顺序表的下标合法
memmove(ps->arr + pos + 1, ps->arr + pos, sizeof(SLDataType) * (ps->size - pos));
ps->arr[pos] = x;
ps->size++;
}
//memmove实现指定位置数据的删除
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
memmove(ps->arr + pos, ps->arr + pos + 1, sizeof(SLDataType) * (ps->size - pos - 1));
ps->size--;
}
3.test.c(测试文件)
#include"SeqList.h"
//void Test()
//{
// SL sl;
// SLInit(&sl);
// SLPushBack(&sl, 1);
// SLPushBack(&sl, 2);
// SLPushBack(&sl, 3);
// SLPushBack(&sl, 4);
// SLPushBack(&sl, 5);
// //SLPushBack(NULL, 1);
// //SLPushFront(&sl, 6);
// //SLPopBack(&sl);
// //SLPopFront(&sl);
// /*SLInsert(&sl, 0, 6);
// SLInsert(&sl, 5, 6);
// SLInsert(&sl, 2, 8);*/
// //SLErase(&sl, 2);
// //SLPrint(&sl);
// /*int ret = SLFind(&sl, 6);
// if (ret < 0)
// {
// printf("没找到\n");
// }
// else
// {
// printf("找到了下标为%d\n",ret);
// }*/
//}
void Test2()
{
SL sl;
SLInit(&sl);
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPushBack(&sl, 4);
SLPushBack(&sl, 5);
//SLPushFront(&sl, 8);
//SLPopFront(&sl);
//SLInsert(&sl, 2, 6);
SLErase(&sl, 2);
SLPrint(&sl);
}
int main()
{
//Test1();
Test2();
return 0;
}
以上就讲完了顺序表的全部内容,希望对大家有帮助,也欢迎大家批评指正。