顺序表原码(练习版)

List.h 文件 函数目录

复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int SLdatatype;  //方便以后修改存储的数据类型
//声明一个动态顺序表
typedef struct SeqList
{
	SLdatatype* arr;
	int size;  //顺序表中的有效数据个数
	int capacity; //顺序表中的总内存大小
}SL;
//初始化动态顺序表
void InitSeqList(SL* ps);
void SLprint(SL sl);

//free之前要实现顺序表的增删查找的操作

//实现尾插
void Pushback(SL* ps, SLdatatype x);
//实现头插
void Pushfront(SL* ps, SLdatatype x);
//实现尾删
void Popback(SL* ps);
//实现头删
void Popfront(SL* ps);
//实现在指定位置之前插入数据
void Frontadd(SL* ps, int pos, SLdatatype x);
//实现在指定位置删除数据
void SLdel(SL* ps, int pos);
//实现查找指定数据
int Find(SL* ps, SLdatatype x);

//申请内存就要free
void Freesl(SL* ps);

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int SLdatatype;  //方便以后修改存储的数据类型
//声明一个动态顺序表
typedef struct SeqList
{
	SLdatatype* arr;
	int size;  //顺序表中的有效数据个数
	int capacity; //顺序表中的总内存大小
}SL;
//初始化动态顺序表
void InitSeqList(SL* ps);
void SLprint(SL sl);

//free之前要实现顺序表的增删查找的操作

//实现尾插
void Pushback(SL* ps, SLdatatype x);
//实现头插
void Pushfront(SL* ps, SLdatatype x);
//实现尾删
void Popback(SL* ps);
//实现头删
void Popfront(SL* ps);
//实现在指定位置之前插入数据
void Frontadd(SL* ps, int pos, SLdatatype x);
//实现在指定位置删除数据
void SLdel(SL* ps, int pos);
//实现查找指定数据
int Find(SL* ps, SLdatatype x);

//申请内存就要free
void Freesl(SL* ps);

List.c 实现函数

复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include "list.h"
//初始化顺序表
void InitSeqList(SL* ps)
{
	ps->arr = NULL;
	ps->size = 0;
	ps-> capacity = 0;
}
void CheckSL(SL* ps)
{
	//尾插时要先判断有没有足够的空间插入
	//if(ps->capacity==0)  //这种判断方法无法判断空间够不够,只能判断空间有无申请成功
	if (ps->capacity == ps->size) //两者相同时表示空间已满
	{
		//空间已满就要申请空间  用realloc
		//申请多少空间呢?  最好申请原来capacity的2倍或3倍的空间
		//realloc(ps->arr, ps->capacity * 2 * sizeof(SLdatatype));//但此时capacity初始化为0
		//确定了要申请的空间
		//ps->capacity = 0 ? 4 : 2 * ps->capacity;
		SLdatatype Newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		//申请空间时可能会申请失败,所以不能直接把realloc的返回给arr
		SLdatatype* tmp = (SLdatatype*)realloc(ps->arr, Newcapacity * sizeof(SLdatatype));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		//到这里内存就申请成功了
		ps->arr = tmp;
		ps->capacity = Newcapacity;//当时写到这里的时候忘记改变原来顺序表的内存了
	}
}
//free申请的空间
void Freesl(SL* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
void Pushback(SL* ps, SLdatatype x)
{
	assert(ps);
	CheckSL(ps);
	ps->arr[ps->size] = x;   //插入所需数据
	ps->size++;
}
void Pushfront(SL* ps, SLdatatype x)
{
	assert(ps);
	CheckSL(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 SLprint(SL sl)
{
	for (int i = 0; i < sl.size; i++)
	{
		printf("%d ", sl.arr[i]);
	}
	printf("\n");
}
void Popback(SL* ps)
{
	assert(ps);
	assert(ps->size);
	//ps->arr[ps->size - 1] = -1;  不影响顺序表的增删查改操作即可
	--ps->size;
}
void Popfront(SL* ps)
{
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
void Frontadd(SL* ps, int pos, SLdatatype x)
{
	assert(ps);
	assert(ps->size);  //检测顺序表是否为空
	assert(pos >= 0 && pos <= ps->size);
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}
void SLdel(SL* ps, int pos)
{
	assert(ps);
	assert(ps->size);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	 ps->size--;
}
int Find(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 测试代码

复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include"list.h"
int main()
{
	//建立一个结构体变量
	SL sl;
	InitSeqList(&sl);
	Pushback(&sl, 1);
	Pushback(&sl, 2);
	Pushback(&sl, 3);
	Pushback(&sl, 4);
	Pushback(&sl, 5);
	SLprint(sl);
	Pushfront(&sl, 4);
	SLprint(sl);
	Popback(&sl);
	SLprint(sl);
	Popfront(&sl);
	SLprint(sl);
	Frontadd(&sl, 2, 4);
	SLprint(sl);
	SLdel(&sl, 2);
	SLprint(sl);
	int ret = Find(&sl, 4);
	if (ret == -1)
	{
		printf("没找到了");
	}
	else
	{
		printf("找到了");
	}




	Freesl(&sl);
	

	return 0;
}

以上是我练习写的,会出教程,纪念一下

相关推荐
mount_myj1 小时前
长长久久【C语言】
c语言
Legendary_0085 小时前
LDR6500:USB‑C DRP PD协议芯片技术详解与应用实践
c语言·开发语言
dgaf8 小时前
DX12 快速教程(17) —— 立体图标与合并渲染
c语言·c++·3d·图形渲染·d3d12
念恒123069 小时前
进程控制---自定义Shell
linux·c语言
程序员JerrySUN11 小时前
Jetson边缘嵌入式实战课程第二讲:JetPack 和 SDK Manager 是什么
c语言·开发语言·网络·udp·音视频
我不是懒洋洋11 小时前
布谷鸟过滤器:比布隆过滤器更优雅的判重方案
c语言·经验分享
忡黑梨11 小时前
eNSP_从直连到BGP全网互通
c语言·网络·数据结构·python·算法·网络安全
handler0113 小时前
Git 核心指令速查
linux·c语言·c++·笔记·git·学习
学会去珍惜13 小时前
学会C语言可以做什么
c语言·网络编程·游戏开发·嵌入式系统·系统编程
『昊纸』℃14 小时前
Mac上编译C语言的简易方法
c语言·mac·教程·xcode·编译