【数据结构之顺序表超详解(保姆级教程)】

顺序表

顺序表的基本概念:
用一组地址连续的存储单元一次存储线性表的数据元素。

1.顺序表的特点

  1. 可动态增长的数组
  2. 数据在数组中存储时必须是连续的

缺点

  1. 中间或头部的插入删除很慢,需要挪动数据,时间复杂度是O(N)
  2. 空间不够时,增容会有一定消耗和空间浪费

优点:

  1. 随机访问(随机存取)
  2. 缓存利用率较高

2.顺序表的定义

静态顺序表

typedef int SLDataType;
#define N 10;
//顺序表的静态存储
typedef struct SeqList
{
	SLDataType a[N];//定长数组
	SLDataType size;//有效数据的个数
}SL,SeqList;

动态顺序表

/顺序表,有效数组在数组中必须是连续
//动态顺序表设计(大小可变)
typedef int SLDataType;
#define N 10;

typedef struct SeqList
{
	SLDataType* a;//指向动态开辟的数组
	SLDataType size;//有效数据个数
	SLDataType capicity;//容量空间大小
}SL,SeqList;

本篇文章主要讨论动态顺序表的增删查改。

3.顺序表的初始化

//顺序表初始化
void SeqListInit(SL* ps)
{
	/*s.size = 0;
	s.a = NULL;
	s.capicity = 0;*/
	ps->a = (SLDataType* )malloc(sizeof(SLDataType) * 4);
	if (ps->a == NULL)
	{
		printf("申请位置失败\n");
		exit(-1);
	}

	ps->size = 0;
	ps->capicity = 4;
}

4.顺序表的扩容

在顺序表中插入的时候,我们会遇到顺序表内存不足的情况,那么我们这个时候就需要给顺序表扩容
那么我们如何去扩容呢?
显然最简单的扩一个单位,但万一需要插入的是100个元素呢,这就得扩容100次,效率显然就降低了,有的同学就会说那我们就扩容10000个,显然这也并不合理,万一只插入一个元素呢。
所以,基于上诉两种情况,我们可以每次给顺序表扩容2倍,就是把原来的大小*2,但这也只是一个折中的办法,仍然会出现地址空余造成空间浪费的情况,这是顺序表不可避免的。

void SeqListCheckCapacity(SL* ps)
{
	//如果满了需要增容
	if (ps->size >= ps->capicity)
	{
		ps->capicity *= 2;
		ps->a = (SLDataType*)realloc(ps->a, sizeof
		(SLDataType) *ps->capicity);
		if (ps->a == NULL)
		{
			printf("扩容失败\n");
			exit(-1);
		}
	}
}

5.顺序表打印

用于观察自己所写的接口是否正确

void SeqListprint(SL* ps)
{
	assert(ps);

	//如果满了需要增容
	SeqListCheckCapacity(ps);
	for (int i = 0; i < ps->size; ++i)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

6.顺序表的插入

在对顺序表操作时,我们首先要做的就是判断顺序表的指针和传入的pos是否合法,教材上呢用的是下面的代码来对pos判断是否合法。
在这呢,博主给大家介绍一格 函数:断言函数(assert),它是cassert…h下的一个函数,就是来判断assert()里的是否合法,不合法就直接终止程序,很好用。

if(i < 1 || i>L.size+1) return -1;

6.1.头插

void SeqListPushFront(SL* ps, SLDataType x)
{
    assert(ps);

	SeqListCheckCapacity(ps);//判断顺序表是否需要扩容,如果需要就扩容
	
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];//把顺序表的元素依次后推一个单位
		--end;
	}

	ps->a[0] = x;//将x插入顺序表的表头
	ps->size++;

	//SeqListInsert(ps,0,x);任意位置实现的头插法
}

6.2.尾插

void SeqListPushBack(SL* ps, SLDataType x)
{
	assert(ps);

	SeqListCheckCapacity(ps);//判断顺序表是否需要扩容,如果需要就扩容
	ps->a[ps->size] = x;
	ps->size++;

	//SeqListInsert(ps, ps->size, x);任意位置实现的尾插法
}

6.3.任意位置的插入

//任意位置插入删除
void SeqListInsert(SL* ps, int pos,SLDataType x)
{
	assert(ps);
	assert(pos <= ps->size && pos >= 0);

	SeqListCheckCapacity(ps);

	int end = ps->size - 1;
	while (end >= pos)//找到插入位置
	{
		ps->a[end + 1] = ps->a[end];//将后面的元素倒推一个单位
		--end;//判断循环是否终止
	}
	ps->a[pos] = x;
	ps->size++;//插入后size加一
}

7.顺序表的删除

7.1.头删

void SeqListPopFront(SL* ps) 
{
	assert(ps);

	int start = 0;
	while (start < ps->size-1)
	{
		ps->a[start] = ps->a[start + 1];//把每个元素前一一个单位,头部的数据就自然被取代了
		++start;
	}

	ps->size--;
	//SeqListErase(ps, 0);
}

7.2.尾删

void SeqListPopBack(SL* ps)
{
	assert(ps);

	ps->a[ps->size - 1] = 0;
	ps->size--;
	
	//SeqListErase(ps, ps->size - 1);
}

7.3.任意位置的删除

void SeqListErase(SL* ps, SLDataType pos)
{
	assert(ps);
	assert(pos <= ps->size && pos >= 0);

	int start = pos;//start为要删除的位置
	while (start < ps->size - 1)
	{
		ps->a[start] = ps->a[start + 1];//数据后移一个单位
		++start;
	}

	ps->size--;//删除后size-1
}

8.顺序表的查找

//顺序表查找
int SeqListFind(SL* ps, SLDataType x)
{
	assert(ps);

	int i = 0;
	while (i < ps->size)
	{
		if (ps->a[i] == x)
		{
			return i;
		}

		++i;
	}

	return -1;//顺序表里没有x,查找失败
}

9.顺序表的销毁

每次运行完后,建议将测试的顺序表删除,以释放空间

//顺序表销毁
void SeqListDestroty(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capicity = 0;
}

10.顺序表的完整代码(接口实现)

10.1.头文件

命名为:list.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include <cassert>

//顺序表,有效数组在数组中必须是连续
//动态顺序表设计(大小可变)
typedef int SLDataType;
#define N 10;

typedef struct SeqList
{
	SLDataType* a;//指向动态开辟的数组
	SLDataType size;//有效数据个数
	SLDataType capicity;//容量空间大小
}SL,SeqList;

//顺序表初始化
void SeqListInit(SL* ps);

//顺序表销毁
void SeqListDestroty(SL *ps);
//尾插尾删
void SeqListPushBack(SL* ps, SLDataType x);
void SeqListPopBack(SL* ps);

//头插头删
void SeqListPushFront(SL* ps, SLDataType x);
void SeqListPopFront(SL* ps);

void SeqListCheckCapacity(SL* ps);

//任意位置插入删除
void SeqListInsert(SL* ps, int pos,
	SLDataType x);
void SeqListErase(SL* ps, SLDataType pos);

//顺序表查找
int SeqListFind(SL* ps1, SLDataType x);

10.2.接口的实现

#include"list.h"

//顺序表初始化
void SeqListInit(SL* ps)
{
	/*s.size = 0;
	s.a = NULL;
	s.capicity = 0;*/
	ps->a = (SLDataType* )malloc(sizeof(SLDataType) * 4);
	if (ps->a == NULL)
	{
		printf("申请位置失败\n");
		exit(-1);
	}

	ps->size = 0;
	ps->capicity = 4;
}

//顺序表销毁
void SeqListDestroty(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capicity = 0;
}

//顺序表打印
void SeqListprint(SL* ps)
{
	assert(ps);

	//如果满了需要增容
	SeqListCheckCapacity(ps);
	for (int i = 0; i < ps->size; ++i)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

void SeqListCheckCapacity(SL* ps)
{
	//如果满了需要增容
	if (ps->size >= ps->capicity)
	{
		ps->capicity *= 2;
		ps->a = (SLDataType*)realloc(ps->a, sizeof
		(SLDataType) *ps->capicity);
		if (ps->a == NULL)
		{
			printf("扩容失败\n");
			exit(-1);
		}
	}
}
// 尾插尾删
	void SeqListPushBack(SL* ps, SLDataType x)
{
	/*assert(ps);

	SeqListCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;*/

	SeqListInsert(ps, ps->size, x);
}

void SeqListPopBack(SL* ps)
{
	/*assert(ps);

	ps->a[ps->size - 1] = 0;
	ps->size--;*/
	SeqListErase(ps, ps->size - 1);
}

//头插头删
void SeqListPushFront(SL* ps, SLDataType x)
{
	/*int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}

	ps->a[0] = x;
	ps->size++;*/

	SeqListInsert(ps,0,x);
}

void SeqListPopFront(SL* ps) 
{
	/*assert(ps);

	int start = 0;
	while (start < ps->size-1)
	{
		ps->a[start] = ps->a[start + 1];
		++start;
	}

	ps->size--;*/
	SeqListErase(ps, 0);
}

//任意位置插入删除
void SeqListInsert(SL* ps, int pos,SLDataType x)
{
	assert(ps);
	assert(pos <= ps->size && pos >= 0);

	SeqListCheckCapacity(ps);

	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[pos] = x;
	ps->size++;
}
void SeqListErase(SL* ps, SLDataType pos)
{
	assert(ps);
	assert(pos <= ps->size && pos >= 0);

	int start = pos;
	while (start < ps->size - 1)
	{
		ps->a[start] = ps->a[start + 1];
		++start;
	}

	ps->size--;
}

//顺序表查找
int SeqListFind(SL* ps, SLDataType x)
{
	assert(ps);

	int i = 0;
	while (i < ps->size)
	{
		if (ps->a[i] == x)
		{
			return i;
		}

		++i;
	}

	return -1;
}

10.3.测试文件

命名为:text.c
使用一个函数将测试的函数封装起来,便于测试多组数据
这种方法更规范,也更高逼格。

#include"list.h"
void SeqListprint(SL* ps);

//测试头尾插入删除
void TestSeqList1()
{
	SeqList s;
	SeqListInit(&s);
	SeqListPushBack(&s, 1);
	SeqListPushBack(&s, 2);
	SeqListPushBack(&s, 3);
	SeqListPushBack(&s, 4);
	SeqListPushBack(&s, 5);
	SeqListPushBack(&s, 6);
	SeqListPushBack(&s, 7);
	SeqListPushBack(&s, 8);
	//SeqListPushBack(&s, 9);
	SeqListprint(&s);

	SeqListPopBack(&s);
	SeqListPopBack(&s);
	SeqListprint(&s);

	SeqListPushFront(&s,-1);
	SeqListPushFront(&s, -2);
	SeqListPushFront(&s, -3);
	//SeqListPopBack(&s);
	SeqListprint(&s);

	SeqListPopFront(&s);
	SeqListPopFront(&s);
	SeqListprint(&s);

	int pos = SeqListFind(&s, 5);
	if (pos != -1)
	{
		SeqListErase(&s, pos);
	}
	SeqListprint(&s);
}
int main()
{
	TestSeqList1();

	return 0;
}

11.总结

本文的代码相对于教材的代码更易于初学者理解、熟悉,本文代码的函数的命名和C++的函数库STL类似,这样同学们以后学到c++的STL库的时候就更容易上手。最后敲完一遍代码后,最好再手写一遍代码,以加深对代码的印象和理解,而且以后面试也要手写代码的,就当作练习了。

博主也希望同学们看了本文后能有一定的收获,喜欢本文的话可以一键三连。

最后,本文涉及到动态内存和函数的知识,有不懂的同学可以看一下下面的两个文章
如果对动态内存分布有不懂的可以点这,舔狗级讲解

对函数不理解的点这,同样的舔狗级讲解

  • 18
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贩梦先生007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值