数据结构(一)顺序表

顺序表的概念及结构

线性表

线性表是具有相同特征的数据结构的集合

物理结构 不一定连续

逻辑结构 连续

顺序表

顺序表是线性表的一种,顺序表的底层是数组

物理结构 连续

逻辑结构 连续

顺序表分类

静态顺序表

复制代码
struct SeqList
{
    int arr[100];//定长数组
    int size;//顺序表当前有效的数据结构
};

动态顺序表

复制代码
struct SeqList
{
    int* arr;
    int size;//有效数据个数
    int capacity;//空间大小
};

顺序表的实现

头文件

顺序表结构

创建顺序表

顺序表的初始化和销毁

顺序表扩容

顺序表的打印

头部插入删除 / 尾部插入删除

指定位置之前插入 / 删除

声明顺序表的方法
复制代码
typedef int SLDataType;
//给int取别名,方便修改数组存储的数据类型
//例如将int类型修改为char类型,在.c文件中的int都要一一替换成char

typedef struct SeqList
{
	SLDataType* arr;
	int size;//有效数据个数
	int capacity;//空间大小   
}SL;

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

//顺序表的销毁
void SLDestroy(SL* ps);

//顺序表的打印
void SLPrint(SL s);


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

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

源文件

实现顺序表的方法
复制代码
//顺序表初始化
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;

}

//顺序表都销毁
void SLDestroy(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

//顺序表打印
void SLPrint(SL s)
{
	for (int i = 0; i < s.size; i++)
	{
		printf("%d", s.arr[i]);
	}
	printf("\n");
}

//检查空间是否足够
void checkcapacity(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		//申请空间
		//malloc calloc realloc int arr[100]	增容realloc
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = ps->arr = (SLDataType*)realloc(ps->arr, ps->capacity * 2 * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(1);//直接退出程序
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
}


//尾插
void SLPushBack(SL* ps, SLDataType x)
{	//法一
	/*if (ps == NULL)
	{
		return;
	}*/
	//法二
	assert(ps);//等价于assert(ps != NULL)

	checkcapacity(ps);
	ps->arr[ps->size++] = x;
	//ps->arr[ps->size] = x;
	//++ps->size;
}

//头插
void SLPushFront(SL* ps, SLDataType x)
{

	assert(ps);
	checkcapacity(ps);
	//先让已有数据后移一位
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];//arr[1] = arr[0]
	}
	ps->arr[0] = x;
	ps->size++;
}

//尾删
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);
	//插入数据,空间够不够
	checkcapacity(ps);
	for (int i = ps->size; i>pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];//arr[pos+1] = arr[pos]
	}
	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;
}
相关推荐
小许学java3 小时前
数据结构-ArrayList与顺序表
java·数据结构·顺序表·arraylist·线性表
很㗊6 小时前
C与C++---类型转换
c语言·开发语言
say_fall6 小时前
精通C语言(3. 自定义类型:联合体和枚举)
c语言·开发语言
迎風吹頭髮6 小时前
UNIX下C语言编程与实践38-UNIX 信号操作:signal 函数与信号捕获函数的编写
linux·c语言·unix
La Pulga6 小时前
【STM32】I2C通信—软件模拟
c语言·stm32·单片机·嵌入式硬件·mcu
程序员莫小特6 小时前
老题新解|大整数加法
数据结构·c++·算法
小刘max7 小时前
深入理解队列(Queue):从原理到实践的完整指南
数据结构
蒙奇D索大8 小时前
【数据结构】考研数据结构核心考点:二叉排序树(BST)全方位详解与代码实现
数据结构·笔记·学习·考研·算法·改行学it
洲覆8 小时前
C++ 模板、泛型与 auto 关键字
开发语言·数据结构·c++
MoRanzhi12038 小时前
15. Pandas 综合实战案例(零售数据分析)
数据结构·python·数据挖掘·数据分析·pandas·matplotlib·零售