数据结构:顺序表

目录

一、数据结构相关概念

二、顺序表的概念及结构

三、顺序表分类

四、动态顺序表的实现

4.1 头文件

4.2 各个功能的实现

4.2.1 初始化与销毁

 4.2.2 申请空间

4.2.3 头插、尾插、头删、尾删

4.2.4 指定位置插入与删除

4.3 测试

五、完整源码

 sxb.h

sxb.c

 test.c


一、数据结构相关概念

      数据结构是由“数据”和“结构”两词组合而来,什么是数据?常见的数值1、2、3、4.....、教务系统里保存的用户信息(姓名、性别、年龄、学历等等)、网页里肉眼可以看到的信息(文字、图⽚、视频等等),这些都是数据什么是结构?当我们想要大量使用同⼀类型的数据时,通过手动定义大量的独立的变量对于程序来说,可读性非常差,我们可以借助数组这样的数据结构将大量的数据组织在⼀起,结构也可以理解为组织数据的方式。

概念:数据结构是计算机存储、组织数据的方式,数据结构是指相互之间存在⼀种或多种特定关系的数据元素的集合。数据结构反映数据的内部构成,即数据由那部分构成,以什么方式构成,以及数据元素之间呈现的结构。通过数据结构,能够有效将数据组织和管理在⼀起。按照我们的方式任意对数据进行增删改查等操作。

最基础的数据结构:数组。

9cec0af41c674ebe8f28d7b508600952.png

有了数组,为什么还要学习其他的数据结构?假设数据量非常庞大,频繁的获取数组有效数据个数会影响程序执行效率,最基础的数据结构能够提供的操作已经不能完全满足复杂算法实现。

二、顺序表的概念及结构

     顺序表是线性表的一种,而线性表是n个具有相同特性的数据元素的有限序列,线性表是⼀种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串... 线性表在逻辑上是线性结构,也就说是连续的⼀条直线,但是在物理结构上并不⼀定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储,这个物理结构是否连续就是看物理地址上是否连续。

三、顺序表分类

    顺序表与数组的区别:顺序表的底层结构是数组,对数组的封装,实现了常用的增删改查等接⼝。顺序表分为静态顺序表和动态顺序表。

静态顺序表:

f20395c62a604f4db6c715c500d8a2d9.png空间给少了不够用,给多会浪费,这是静态顺序表的缺陷。

动态顺序表:

6bdb17b5fd60442a97e2fc930e8ce8dc.png

 很明显,动态顺序表是优于静态顺序表的,按需申请空间,既不会空间不够用,也不会造成空间浪费。

四、动态顺序表的实现

4.1 头文件

    我们需要自己创建一个头文件,在头文件中列出我们所需要的函数(只是列出函数名,然后在另一个源文件中完成这些函数的功能)

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;//方便转换类型,如果想要字符类型的顺序表只要把int换成char即可
typedef struct SeqList
{
    SLDataType* arr;
    int size;     // 有效数据个数
    int capacity; // 空间容量
}SL;


void SLInit(SL* ps);//初始化顺序表
void SLDestroy(SL* ps);//销毁顺序表

void SLCheckCapacity(SL* ps);//申请空间


void SLPushBack(SL* ps, SLDataType x);//尾部插入数据
void SLPopBack(SL* ps);//尾部删除数据
void SLPushFront(SL* ps, SLDataType x);//头部插入数据
void SLPopFront(SL* ps);//头部删除数据
void SLprint(SL* ps);//顺序表打印


void SLInsert(SL* ps, int pos, SLDataType x);//指定位置插入数据
void SLErase(SL* ps, int pos);//指定位置删除数据

4.2 各个功能的实现

4.2.1 初始化与销毁

void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;//数组置空,有效数据和空间容量都是0
}
void SLDestroy(SL* ps)
{
	if (ps->arr != NULL)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;//数组置空,有效数据和空间容量归0
}

 4.2.2 申请空间

    先判断有效数据和空间容量是否相等,如果相等就说明数组空间已经满了需要申请一段空间,首次申请前空间容量为0,初始给4个空间容量,后续每次申请,空间容量乘二,开辟的空间给到数组。 

void SLCheckCapacity(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType*tem = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (tem == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->arr = tem;
		ps->capacity = newcapacity;
	}
}

4.2.3 头插、尾插、头删、尾删

void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);//插入前都需要检查一下空间是否足够
	int i;
	for (i = ps->size; i >0; i--)
	{
		ps->arr[i] = ps->arr[i-1];//每个位置的数据都要往后移一位
	}
	ps->arr[0] = x;
	ps->size++;//记录有效数据
}
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;//只需要加在数组最后一位就行了
}
void SLPopFront(SL* ps)
{
	assert(ps);
	int i;
	for (i = 0; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i + 1];//每一位数据往前移
	}
	ps->size--;
}
void SLPopBack(SL* ps)
{
	assert(ps);
	ps->size--;//把最后一位直接删除就行
}

4.2.4 指定位置插入与删除

void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	int i=0;
	for (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);
	SLCheckCapacity(ps);
	int i = 0;
	for (i = pos; i < ps->size-1; i++)//指定位置之后数据都往前移一位
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

4.2.5 顺序表打印

      和打印数组几乎一样

void SLprint(SL* ps)
{
	int i;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

4.3 测试

    实现完函数的功能后需要通过测试来看代码是否有误自己给的一组数据也是没啥问题,测试过程中最好是一次测试一个功能,这样在程序出错时方便找到错误所在。

五、完整源码

 sxb.h

#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 SLDestroy(SL* ps);
void SLCheckCapacity(SL* ps);
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
void SLprint(SL* ps);
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);

sxb.c

#include"sxb.h"
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
void SLDestroy(SL* ps)
{
	if (ps->arr != NULL)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType*tem = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (tem == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->arr = tem;
		ps->capacity = newcapacity;
	}
}
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	int i;
	for (i = ps->size; i >0; i--)
	{
		ps->arr[i] = ps->arr[i-1];
	}
	ps->arr[0] = x;
	ps->size++;
}
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;
}
void SLPopFront(SL* ps)
{
	assert(ps);
	SLCheckCapacity(ps);
	int i;
	for (i = 0; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
void SLPopBack(SL* ps)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->size--;
}
void SLprint(SL* ps)
{
	int i;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	int i=0;
	for (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);
	SLCheckCapacity(ps);
	int i = 0;
	for (i = pos; i < ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

 test.c

#include"sxb.h"
void sxb()
{
	SL s1;
	SLInit(&s1);
	SLPushFront(&s1, 3);
	SLPushFront(&s1, 2);
	SLPushFront(&s1, 1);
	SLprint(&s1);
	SLPushBack(&s1, 4);
	SLPushBack(&s1, 5);
	SLPushBack(&s1, 6);
	SLprint(&s1);
	SLPopFront(&s1);
	SLprint(&s1);
	SLPopBack(&s1);
	SLprint(&s1);
	SLInsert(&s1,2,9);
	SLprint(&s1);
	SLErase(&s1, 2);
	SLprint(&s1);
    SLDestroy(&s1);
}
int main()
{
	sxb();
	return 0;
}


     本篇的内容就到这里了,希望对各位有帮助,如果有错误欢迎指出,如果喜欢的话,点赞+关注,下期内容不迷路。

000f17d6da9c4a14a854f7e0f2c99553.gif

  • 29
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值