【数据结构】实现顺序表

目录

一.介绍顺序表

顺序表是用一段物理地址连续 的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储 。

顺序表与通讯录类似,可以完成增删查改 等功能。在此基础上,还可以实现头插、头删、尾插、尾删以及某位置的插入和删除

二.实现顺序表

1.创建多文件

用多文件的好处在通讯录一文中已经说明过了,所以这里直接进入正题:

SeqList.h------函数和类型的声明
SeqList.c------函数的实现
Test.c------测试顺序表

注:为了方便测试,所以没有Test.c没有菜单,直接进行测试

2.顺序表的存储方式

顺序表可以采用两种存储方式:静态存储和动态存储

本文使用的是动态存储 ,因为静态存储只适用于确定知道需要存多少数据的场景,空间开多了浪费,开少了不够用。现实中基本都是使用动态顺序表,根据需要动态的分配空间大小。

c 复制代码
typedef int SLDataType;//重定义方便类型的修改
typedef struct SeqList
{
	SLDataType* a;//指向动态开辟的数组
	int size;//数据的个数
	int capacity;//容量的大小
}SL;

3.函数的声明

该顺序表要实现的函数有:
1.初始化顺序表
2.清理顺序表
3.打印顺序表
4.扩容
5.尾插
6.尾删
7.头插
8.头删
9.查找
10.修改
11.在pos位置插入
12.在pos位置删除

c 复制代码
//初始化
void SLInit(SL* ps);
//清理
void SLDestroy(SL* ps);
//打印
void SLPrint(SL* ps);
//扩容
void SLCapacity(SL* ps);
//尾插
void SLPushBack(SL* ps, SLDataType x);
//尾删
void SLPopBack(SL* ps);
//头插
void SLPushFront(SL* ps, SLDataType x);
//头删
void SLPopFront(SL* ps);
//查找
void SLFind(SL* ps, SLDataType x);
//修改
void SLModify(SL* ps, int pos, SLDataType x);
//在pos位置插入
void SLInsert(SL* ps, int pos, SLDataType x);
//在pos位置删除
void SLErase(SL* ps, int pos);

4.初始化顺序表

刚开始要对传过来的指针进行断言,防止为空(后面的也是)
使用malloc函数为数组开辟一块空间(容量大小自己定),数据个数初始化为0

c 复制代码
void SLInit(SL* ps)
{
	assert(ps);
	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	ps->size = 0;
	ps->capacity = 4;
}

5.清理顺序表

程序结束前,要对内存进行清理,因为使用了动态开辟函数,所以必须对使用的空间进行释放,防止内存泄漏

c 复制代码
void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}

6.打印顺序表

因为顺序表是连续的空间,所以打印顺序表的数据用for循环遍历出来就可以了

c 复制代码
void SLPrint(SL* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

7.扩容

当顺序表的数据满了(等于刚开始开辟的空间大小),就要进行扩容。使用realloc函数,可以对容量进行修改。

c 复制代码
void SLCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		SLDataType* ptr = (SLDataType*)realloc(ps->a, 2 * ps->capacity * sizeof(SLDataType));
		if (ptr == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		else
		{
			ps->a = ptr;
			ps->capacity *= 2;
		}
	}
}

8.尾插

进入尾插这个函数,首先要对检查容量是否已满,满了就扩容。
size是数据个数,数组aps-\>size是下一个数据的下标,尾插一个数把这个数赋给aps-\>size就行了,然后size++

c 复制代码
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}

8.尾删

将最后一个元素置为0,然后size减1

注意:当size为0时就不能再减了,所以对size的范围要断言

c 复制代码
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);
	ps->a[ps->size - 1] = 0;
	ps->size--;
}

9.头插

头插数据,先要检查容量;定义一个变量end,指向的是最后一个元素的下一个位置,然后利用while循环,end的范围>=0(把原来的首元素挪动才能头插),将最后一个元素放进它的下个位置,循环一次end减1,依次将前一个元素置到后一个元素的地址去,直到将首元素的位置变成空的状态,然后头插,size加1

c 复制代码
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCapacity(ps);
	int end = ps->size;
	while (end >= 0)
	{
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}

10.头删

定义一个变量begin等于0,指向首元素。用while循环,将后一个元素覆盖前一个元素,每次循环begin加1,直到最后一个元素向前覆盖完就结束,为头的元素就删除了,然后size减1

注意:当size为0时就不能再减了,所以对size的范围要断言

c 复制代码
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);
	int begin = 0;
	while (begin < ps->size)
	{
		ps->a[begin] = ps->a[begin + 1];
		begin++;
	}
	ps->size--;
}

11.查找

用for循环遍历顺序表,有与x相同的数就找到了,否则没找到

c 复制代码
void SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			printf("找到了\n");
			return;
		}
	}
	printf("没找到\n");
}

12.修改

pos的值经过断言如果是在范围内就直接将pos位置的值修改为x,否则报错

c 复制代码
void SLModify(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	ps->a[pos] = x;
}

13.在pos位置插入

首先对pos的值进行断言,确定其是否在范围内。插入数值,要考虑容量是否已满,所以要检查容量。接下来与头插类似,把pos位置的数据和后面的数据往后挪动,然后在pos位置插入x,size加1

c 复制代码
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	SLCapacity(ps);
	int end = ps->size;
	while (end >= pos)
	{
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

13.在pos位置删除

与头删类似,直到最后一个元素向前覆盖完就结束

c 复制代码
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	int begin = pos;
	while (begin < ps->size)
	{
		ps->a[begin] = ps->a[begin + 1];
		begin++;
	}
	ps->size--;
}

三.全部代码

1.SeqList.h

c 复制代码
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLDataType;//重定义方便类型的修改
typedef struct SeqList
{
	SLDataType* a;//指向动态开辟的数组
	int size;//数据的个数
	int capacity;//容量的大小
}SL;
//初始化
void SLInit(SL* ps);
//清理
void SLDestroy(SL* ps);
//打印
void SLPrint(SL* ps);
//扩容
void SLCapacity(SL* ps);
//尾插
void SLPushBack(SL* ps, SLDataType x);
//尾删
void SLPopBack(SL* ps);
//头插
void SLPushFront(SL* ps, SLDataType x);
//头删
void SLPopFront(SL* ps);
//查找
void SLFind(SL* ps, SLDataType x);
//修改
void SLModify(SL* ps, int pos, SLDataType x);
//在pos位置插入
void SLInsert(SL* ps, int pos, SLDataType x);
//在pos位置删除
void SLErase(SL* ps, int pos);

2.SeqList.c

c 复制代码
#include "SeqList.h"
//初始化
void SLInit(SL* ps)
{
	assert(ps);
	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	ps->size = 0;
	ps->capacity = 4;
}
//清理
void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}
//打印
void SLPrint(SL* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
//扩容
void SLCapacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		SLDataType* ptr = (SLDataType*)realloc(ps->a, 2 * ps->capacity * sizeof(SLDataType));
		if (ptr == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		else
		{
			ps->a = ptr;
			ps->capacity *= 2;
		}
	}
}
//尾插
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;
}
//尾删
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);
	ps->a[ps->size - 1] = 0;
	ps->size--;
}
//头插
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCapacity(ps);
	int end = ps->size;
	while (end >= 0)
	{
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}
//头删
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);
	int begin = 0;
	while (begin < ps->size)
	{
		ps->a[begin] = ps->a[begin + 1];
		begin++;
	}
	ps->size--;
}
//查找
void SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			printf("找到了\n");
			return;
		}
	}
	printf("没找到\n");
}
//修改
void SLModify(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	ps->a[pos] = x;
}
//在pos位置插入
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	SLCapacity(ps);
	int end = ps->size;
	while (end >= pos)
	{
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}
//在pos位置删除
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	int begin = pos;
	while (begin < ps->size)
	{
		ps->a[begin] = ps->a[begin + 1];
		begin++;
	}
	ps->size--;
}

3.Test.c

c 复制代码
#include "SeqList.h"
void test()
{
	SL s1;
	SLInit(&s1);//初始化

	SLPushBack(&s1, 1);
	SLPushBack(&s1, 2);
	SLPushBack(&s1, 3);
	SLPushBack(&s1, 4);
	SLPushBack(&s1, 5);//测试尾插
	SLPrint(&s1);

	SLPopBack(&s1);
	SLPopBack(&s1);//测试尾删
	SLPrint(&s1);

	SLPushFront(&s1, 10);
	SLPushFront(&s1, 20);
	SLPushFront(&s1, 30);
	SLPushFront(&s1, 40);//测试头插
	SLPrint(&s1);

	SLPopFront(&s1);
	SLPopFront(&s1);//测试头删
	SLPrint(&s1);

	SLFind(&s1, 100);//测试查找

	SLModify(&s1, 2, 99);//测试修改
	SLPrint(&s1);

	SLInsert(&s1, 3, 77);//测试pos位置插入
	SLPrint(&s1);

	SLErase(&s1, 1);//测试pos位置删除
	SLPrint(&s1);

	SLDestroy(&s1);

}
int main()
{
	test();
	return 0;
}

~ ~

感谢观看

相关推荐
Eloudy3 分钟前
全文 - version.A - AMBA CHI Chip-to-Chip(C2C)
java·开发语言·数据库·gpu·chiplet
秋名RG10 分钟前
Java历代LTS版本新特性详解(代码增强版)
java·开发语言·python
李少兄10 分钟前
深度剖析 Hutool ObjectUtil
java·开发语言
魏码不凡11 分钟前
Go 编译报错:undefined: webp.Encode 问题排查与解决
开发语言·后端·golang
276695829212 分钟前
youdao/有道翻译APP 算法协议分析
开发语言·前端·python·sign·youdao·有道翻译app算法·有道app协议请求
励志不掉头发的内向程序员20 分钟前
【从零写一个CAD 04】中键拖动平移:抓住一个点,让它一直待在鼠标底下
开发语言·c++·qt·学习·系统架构·计算机外设
tachibana22 小时前
与其他语言相比,使用 Go 有什么好处?
开发语言·后端·golang·go
Logic1012 小时前
C语言/数据结构字符串题解:按字符类型重排(数字→字母→符号)——三趟扫描稳定分区
c语言·数据结构·字符串·时间复杂度·计数排序·算法题·ascii
阿俊-全栈开发6 小时前
LikeShop 单商户SaaS商城分销模块二开:佣金计算与结算逻辑实现
java·开发语言·微服务·likeshop·likeshop开源商城·likeshop多商户
Logic1019 小时前
C语言/数据结构位运算题解:异或XOR找出学习计划中的“独特学习时间“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质