数据结构--顺序表(C语言版)

(一).顺序表的概念及结构

首先,我们来了解一下什么是数据结构呢?

数据结构是顾明思义就是由数据+结构。

常⻅的数值1、2、3、4.....、教务系统⾥保存的⽤⼾信息(姓名、性别、年龄、学历等 等)、⽹⻚⾥⾁眼可以看到的信息(⽂字、图⽚、视频等等),这些就是数据

那结构是什么呢?当海量的数据没有按照顺序排列当我们查找起来就十分麻烦,例如:

上面这一组图片就能很好的说明结构的重要性当我们在左边圈里去找某一只小羊可想而知找到要花费多少时间,而当我们将每只小羊按照顺序排好序之后, 比如我们要找编号为十的这只小羊,我们就可以通过编号直接找到,这样便省了大量的时间。

故结构就是:当我们想要使⽤⼤量使⽤同⼀类型的数据时,通过⼿动定义⼤量的独⽴的变量对于程序来说,可读性 ⾮常差,我们可以借助数组这样的数据结构将⼤量的数据组织在⼀起,结构也可以理解为组织数据的⽅式。

++数据结构:数据结构是计算机存储,组织数据的方式。++

接下来我们来了解一下顺序表的概念是什么呢?

顺序表就是一群数据按照有顺序的方式进行排列,这样便于查找。

顺序表其实就是线性表的其中一种。

首先,我们要来了解线性表的概念,

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是⼀种在实际中⼴泛使 ⽤的数据结构,常⻅的线性表:顺序表、链表、栈、队列、字符串...。

线性表在逻辑上是线性结构,也就说是连续的⼀条直线。但是在物理结构上并不⼀定是连续的, 线性表在物理上存储时,通常以数组和链式结构的形式存储。

(二).顺序表的分类

顺序表分为静态顺序表和动态顺序表两种:

静态顺序表:

这里就是一个静态顺序表,它的结构体数组大小是确定的,而这样就会导致一个缺点 ,空间给少了就不够用,空间给多了就会造成空间浪费。而我们的动态顺序表就能很好的解决这个问题。

动态顺序表:

这里就是动态顺序表,它相比于静态顺序表就是可动态的申请空间,按需申请空间这样就不会造成空间浪费,也不会出现空间不足的现象。

(三).动态顺序表的实现

上面我们了解了顺序表的概念和顺序表的分类,接下来进入代码阶段实现动态顺序表。

3.1创建结构体

cs 复制代码
typedef int SLDatatype;
typedef struct SqList {
	SLDatatype* arr;
	int size;
	int capecity;
}SL;

3.2初始化顺序表

cs 复制代码
//初始化顺序表
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->capecity = ps->size = 0;
}

3.3销毁顺序表

cs 复制代码
//销毁顺序表
void SLDestroy(SL* ps)
{
	if (ps->arr != NULL)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->capecity = ps->size = 0;
}

3.4打印顺序表

cs 复制代码
//打印代码
void SLPrint(SL ps)
{
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d ", ps.arr[i]);
	}
	printf("\n");
}

3.5头插尾插

尾插:

cs 复制代码
//尾部插入
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	//插入数据之前首先要判断空间够不够
	if (ps->size == ps->capecity)//如果当前空间已经满了的话就用relloc增容
	{
		//使用三目表达式,为使现有空间成倍的增加
		int newcapecity = ps->capecity == 0 ? 4 : 2 * ps->capecity;
		SLDataType* tmp = (SLDataType*)relloc(ps->arr, newcapecity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("relloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capecity = newcapecity;
	}
	ps->arr[ps->size] = x;//在顺序表中尾插一个数据;
	ps->size++;
}

头插:

cs 复制代码
//头插
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	//插入数据之前首先要判断空间够不够
	if (ps->size == ps->capecity)//如果当前空间已经满了的话就用relloc增容
	{
		//使用三目表达式,为使现有空间成倍的增加
		int newcapecity = ps->capecity == 0 ? 4 : 2 * ps->capecity;
		SLDataType* tmp = (SLDataType*)relloc(ps->arr, newcapecity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("relloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capecity = newcapecity;
	}
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}

3.6头删尾删

尾删:

cs 复制代码
// 尾部删除
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->arr[ps->size - 1] = -1;
	--ps->size;
}

头删:

cs 复制代码
//头部删除
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	--ps->size;
}

3.7在任意位置插入

cs 复制代码
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);//断言插入位置是否合理
	//插入数据之前首先要判断空间够不够
	if (ps->size == ps->capecity)//如果当前空间已经满了的话就用relloc增容
	{
		//使用三目表达式,为使现有空间成倍的增加
		int newcapecity = ps->capecity == 0 ? 4 : 2 * ps->capecity;
		SLDataType* tmp = (SLDataType*)relloc(ps->arr, newcapecity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("relloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capecity = newcapecity;
	}
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}

3.8在任意位置删除

cs 复制代码
//在任意位置删除
void SLErase(SL* ps, int pos)
{
	assert(pos);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size; i++)
	{
		ps->arr[i] = ps->arr[i+1];
	}
	ps->size--;
}

3.9查找数据

cs 复制代码
//查找数据
int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}

汇总

SqList.h:

cs 复制代码
#pragma once
//SeqList.h进行顺序表结构的声明
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//创建结构体
//静态结构体
//struct seqlist 
//{
//	int arr[100];
//	int size;
//};

typedef int SLDataType;
//动态结构体
typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效数据的个数
	int capacity;//顺序表的大小
}SL;

//初始化顺序表
void SLInit(SL* ps);
//销毁顺序表
void SLDestroy(SL* ps);
//打印代码
void SLPrint(SL ps);

//头部插⼊删除 / 尾部插⼊删除

//尾部插入
void SLPushBack(SL* ps, SLDataType x);
//头部插入
void SLPushFront(SL* ps, SLDataType x);

//头部删除
void SLPopFront(SL* ps);
//尾部删除
void SLPopBack(SL* ps);
//任意位置插入
void SLInsert(SL* ps, int pos, SLDataType x);
//任意位置删除
void SLErase(SL* ps, int pos);
//查找数据
int SLFind(SL* ps, SLDataType x);

SqList.c:

cs 复制代码
//进行顺序表的实现
#include"SeqList.h";
//顺序表初始化
void SLInit(SL* s)
{
	s->arr = NULL;
	s->size = s->capacity = 0;
}
//顺序表的销毁
void SLDestroy(SL* ps) 
{
	if (ps->arr != NULL)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->capacity = 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, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("ralloc fail!");
			return 1;
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}

//尾插
void SLPushBack(SL* ps, SLDataType x)
{
	//首先要判断空间够不够
	
	assert(ps);//断言ps不为空指针
	
	SLCheckcapacity(ps);
	/*ps->arr[ps->size] = x;
	ps->size++;*/
	ps->arr[ps->size++] = x;
}


//头插
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 SLPrint(SL ps)
{
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d ", ps.arr[i]);
	}
	printf("\n");
}
//尾删
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->arr[ps->size - 1] = -1;
	--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);
	SLCheckcapacity(ps);
	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; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}


//查找数据
int SLFind(SL* ps, SLDataType x) 
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}

test.c:

cs 复制代码
//进行顺序表的测试
#include"SeqList.h";

SLtest01()
{
	SL s1;
	//初始化顺序表
	SLInit(&s1);
	//尾插
    SLPushBack(&s1, 1);
	SLPushBack(&s1, 2);
	SLPushBack(&s1, 3);
	SLPushBack(&s1, 4);
	SLPushBack(&s1, 5);
	SLPrint(s1);
	//头插
	//SLPushFront(&s1, 9); 
	//SLPushFront(&s1, 8);
	//SLPrint(s1);

	//尾删
	//SLPopFront(&s1);
	//SLPrint(s1);

	//头删
	//SLPopBack(&s1);
	//SLPrint(s1);

	//在任意位置插入
	//SLInsert(&s1,2,88);
	//SLPrint(s1);

	//在任意位置删除
	//SLErase(&s1, 2);
	//SLPrint(s1);

	//查找数据
	//int ret = SLFind(&s1, 3);
	//printf("找到了,它的下标为%d\n", ret);



	//销毁顺序表
	SLDestroy(&s1);
}



int main()
{
	SLtest01();

	return 0;
}

好了,各位老铁以上就是我跟大家分享的顺序表的全部内容,觉得小编写的还不错的话,记得给小编留下免费的三连哦!谢谢大家!

相关推荐
Starry_hello world1 小时前
二叉树实现
数据结构·笔记·有问必答
万事大吉CC1 小时前
mysql单表查询·3
数据库·mysql
bin91532 小时前
【EXCEL数据处理】000010 案列 EXCEL文本型和常规型转换。使用的软件是微软的Excel操作的。处理数据的目的是让数据更直观的显示出来,方便查看。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化
Miqiuha2 小时前
lock_guard和unique_lock学习总结
java·数据库·学习
嵌入式AI的盲3 小时前
数组指针和指针数组
数据结构·算法
一 乐3 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
reyas5 小时前
B树系列解析
数据结构·b树
Indigo_code5 小时前
【数据结构】【顺序表算法】 删除特定值
数据结构·算法
阿史大杯茶6 小时前
Codeforces Round 976 (Div. 2 ABCDE题)视频讲解
数据结构·c++·算法
Java探秘者7 小时前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea