目录
前言:
经过了上一节对数据结构和算法复杂度的认识,我们现在进入到了数据结构正式的学习环节,前方迎接我们的是更深层次的学习。这一篇文章给大家分享一下顺序表的内容,有基础知识还有代码实现哦,最后还有题目等着我们。
一、概念和结构
1. 线性表
线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是⼀种在实际中⼴泛使 ⽤的 数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串...
线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的, 线性表在物理上存储时,通常以数组和链式结构的形式存储。
2.顺序表的概念
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。
顺序表就是 数组+增加数据+删除数据+修改数据+查找数据+......
顺序表的底层是数组,顺序表是用数组实现的。
3.顺序表的结构
逻辑结构:线性
物理结构:线性
二、分类
顺序表分为静态顺序表和动态顺序表,下面用代码来分别说明一下静态顺序表和动态顺序表的不同之处:
1.静态顺序表:
//概念:使⽤定⻓数组存储元素
typedef int SLDateType
#define N 7
typedef struct SeqList
{
SLDateType a[n]; //定长数组
int size; //有效数据个数
}
//静态顺序表缺陷:空间给少了不够⽤,给多了造成空间浪费
2.动态顺序表:
//动态顺序表--按需申请
typedef struct SeqList
{
SLDateType* a;
int size; //有效数据个数
int capacity; //空间容量
}
三、顺序表的实现
顺序表的实现可以分成三个函数来实现:
SeqList.h :定义结构,声明函数(头文件)
SeqList.c :定义函数
test.c :测试功能
下面我给大家分功能来实现线性表
1.定义顺序表:
//定义动态顺序表的结构
typedef int SLTDateType;//用别名方便后续更改顺序表的类型
typedef struct Seqlist {
SLTDateType* arr; //存储数据的数组
int size; //有效数据个数
int capacity; //空间大小
}SL;
//函数声明
void Slinit(SL* ps);
定义顺序表部分写在SeqList.h部分
2.初始化:
初始化和后面的函数功能都写在SeqList.c上,但需要在SeqList.h上面声明。
//初始化
void Slinit(SL* ps)
{
ps->arr = NULL;
ps->size = 0;
ps->capacity = 0;
}
写完功能后再test.c部分测试是否成功,如右图所示
#include"Seqlist.h"
void test01()
{
SL s1;
Slinit(&s1);
}
int main()
{
test01();
return 0;
}
3.打印数据:
//打印函数
void Slprint(SL* ps)
{
for (int i = 0;i < ps->size;i++)
{
printf("%d ", ps->arr[i]);
}
printf("\n");
}
4.插入数据:
1)尾插(初始版本和简洁版本)
//尾插法
void SlPushBack(SL* ps, SLTDateType x)
{
assert(ps);
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity = 0 ? 4 : 2 * ps->capacity;
SLTDateType* temp = (SLTDateType*)realloc(ps->arr, newCapacity * sizeof(SLTDateType));
if (temp == NULL)
{
perror("realloc");
exit(1);
}
ps->arr = temp;
ps->capacity = newCapacity;
}
ps->arr[ps->size++] = x;
}
在写尾插操作过程中发现了其中的增容操作后续仍需要使用,所以,我们可以把增容这一部分给分离出来,单独设置一个函数,方便后续操作中的扩容操作
增容函数:
//扩容
void SlCheckCapacity(SL* ps)
{
if (ps->size == ps->capacity)
{
int newCapacity = ps->capacity = 0 ? 4 : 2 * ps->capacity;
SLTDateType* temp = (SLTDateType * )realloc(ps->arr, newCapacity * sizeof(SLTDateType));
if (temp == NULL)
{
perror("realloc");
exit(1);
}
ps->arr = temp;
ps->capacity = newCapacity;
}
}
这样,尾插函数就会简洁许多:
//尾插法
void SlPushBack(SL* ps, SLTDateType x)
{
assert(ps);
SlCheckCapacity(ps);
ps->arr[ps->size++] = x;
}
2)头插
//头插法
void SlPushFront(SL* ps, SLTDateType x)
{
assert(ps);
SlCheckCapacity(ps);
for (int i = ps->size;i > 0;i--)
{
ps->arr[i - 1] = ps->arr[i];
}
ps->arr[0] = x;
ps->size++;
}
5.删除数据
1)尾删
尾删就比较简单了,只需要将size前移一位,size后面的数据就不是有效数据了,相当于被删除。
assert(ps && ps->size);除了需要断言ps不为空指针情况外,还需要断言一下顺序表不能为空,就是ps->size!=0,也就是assert(ps->size)。
//尾删法
void SlDelBack(SL* ps)
{
assert(ps && ps->size);
ps->size--;
}
2)头删
头删比尾删多了一步需要将后面的数据全部向前挪动一位
//头删法
void SlDelFront(SL* ps)
{
assert(ps && ps->size);
for (int i = 0;i < ps->size-1;i++)
{
ps->arr[i + 1] = ps->arr[i];
}
ps->size--;
}
6.指定位置插入/删除
1)在指定位置之前插入数据
//在指定位置之前插入数据
void Slinsert(SL* ps, int pos, SLTDateType x)
{
assert(ps && 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++;
}
2)删除指定位置的数据
//删除指定位置的数据
void Slerase(SL* ps, int pos)
{
assert(ps && ps->size);
assert(pos >= 0 && pos < ps->size);
for (int i = pos;i < ps->size - 1;i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
7.查找数据
//查找
int SLFind(SL* ps, SLTDateType x)
{
assert(ps);
for (int i = 0;i < ps->size;i++)
{
if (ps->arr[i] == x)
{
return i;//找到了
}
}
return -1;//未找到
}
8.销毁函数
//销毁函数
void Sldestroy(SL* ps)
{
if (ps->arr != NULL)
free(ps->arr);
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
四、博主手记
字迹不是很好,大家多见谅QAQ




五、完整版代码分享
Seqlist.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
//定义动态顺序表的结构
typedef int SLTDateType;//用别名方便后续更改顺序表的类型
typedef struct Seqlist {
SLTDateType* arr; //存储数据的数组
int size; //有效数据个数
int capacity; //空间大小
}SL;
//函数声明
//初始化
void Slinit(SL* ps);
//打印函数
void Slprint(SL* ps);
//销毁函数
void Sldestroy(SL* ps);
//扩容
void SlCheckCapacity(SL* ps);
//尾插法
void SlPushBack(SL* ps, SLTDateType x);
//头插法
void SlPushFront(SL* ps, SLTDateType x);
//尾删法
void SlDelBack(SL* ps);
//头删法
void SlDelFront(SL* ps);
//在指定位置之前插入数据
void Slinsert(SL* ps, int pos, SLTDateType x);
//删除指定位置的数据
void Slerase(SL* ps, int pos);
//查找
int SLFind(SL* ps, SLTDateType x);
Seqlist.c
#define _CRT_SECURE_NO_WARNINGS
#include"Seqlist.h"
//初始化
void Slinit(SL* ps)
{
ps->arr = NULL;
ps->size = 0;
ps->capacity = 0;
}
//打印函数
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;
SLTDateType* temp = (SLTDateType * )realloc(ps->arr, newCapacity * sizeof(SLTDateType));
if (temp == NULL)
{
perror("realloc");
exit(1);
}
ps->arr = temp;
ps->capacity = newCapacity;
}
}
//尾插法
void SlPushBack(SL* ps, SLTDateType x)
{
assert(ps);
SlCheckCapacity(ps);
ps->arr[ps->size++] = x;
}
//头插法
void SlPushFront(SL* ps, SLTDateType x)
{
assert(ps);
SlCheckCapacity(ps);
for (int i = ps->size;i > 0;i--)
{
ps->arr[i - 1] = ps->arr[i];
}
ps->arr[0] = x;
ps->size++;
}
//尾删法
void SlDelBack(SL* ps)
{
assert(ps && ps->size);
ps->size--;
}
//头删法
void SlDelFront(SL* ps)
{
assert(ps && ps->size);
for (int i = 0;i < ps->size-1;i++)
{
ps->arr[i + 1] = ps->arr[i];
}
ps->size--;
}
//在指定位置之前插入数据
void Slinsert(SL* ps, int pos, SLTDateType x)
{
assert(ps && 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 && ps->size);
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, SLTDateType x)
{
assert(ps);
for (int i = 0;i < ps->size;i++)
{
if (ps->arr[i] == x)
{
return i;//找到了
}
}
return -1;//未找到
}
//销毁函数
void Sldestroy(SL* ps)
{
if (ps->arr != NULL)
free(ps->arr);
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"Seqlist.h"
void test01()
{
SL sl;
SLInit(&sl);
//具备了一个空的顺序表
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPushBack(&sl, 4);
SLPrint(&sl);
//SLPushFront(&sl, 1);
//SLPushFront(&sl, 2);//2 1
//SLPushFront(&sl, 3);//3 2 1
//SLPushFront(&sl, 4);//4 3 2 1
//SLPopBack(&sl);
//SLPrint(&sl);//1 2 3
//SLPopBack(&sl);
//SLPrint(&sl);//1 2
//SLPopBack(&sl);
//SLPrint(&sl);//1
//SLPopBack(&sl);
//SLPrint(&sl);//
//SLPopBack(&sl);
////头删
//SLPopFront(&sl);
//SLPrint(&sl);
//SLPopFront(&sl);
//SLPrint(&sl);
//SLPopFront(&sl);
//SLPrint(&sl);
//SLPopFront(&sl);
//SLPrint(&sl);
//SLPopFront(&sl);
//测试查找
int pos = SLFind(&sl, 4);
//if (pos < 0)
//{
// printf("未找到!\n");
//}
//else {
// printf("找到了!\n");
//}
//SLInsert(&sl, pos, 100);
//SLPrint(&sl);
//SLInsert(&sl, pos, 200);
//SLPrint(&sl);
//SLInsert(&sl, pos, 300);
//SLPrint(&sl);
//SLInsert(&sl, pos, 400);
//SLPrint(&sl);
SLErase(&sl, pos);
SLPrint(&sl);
SLDestroy(&sl);
}
int main()
{
test01();
return 0;
}
结语:
上面就是我这篇文章的全部内容啦,如果有哪里不妥非常欢迎大家指点!欢迎大家在评论区留言!
下一篇文章我会分享顺序表的练习题哦,欢迎大家关注!
1万+

被折叠的 条评论
为什么被折叠?



