顺序表原码(练习版)

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;
}

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

相关推荐
三道渊4 小时前
C语言:文件I/O
c语言·开发语言·数据结构·c++
kali-Myon5 小时前
CTFshow-Pwn142-Off-by-One(堆块重叠)
c语言·数据结构·安全·gdb·pwn·ctf·
计算机安禾6 小时前
【数据结构与算法】第19篇:树与二叉树的基础概念
c语言·开发语言·数据结构·c++·算法·visual studio code·visual studio
Zarek枫煜6 小时前
[特殊字符] C3语言:传承C之高效,突破C之局限
c语言·开发语言·c++·单片机·嵌入式硬件·物联网·算法
爱编码的小八嘎8 小时前
C语言完美演绎6-11
c语言
星辰徐哥8 小时前
C语言网络编程:TCP/IP协议栈、套接字、服务器/客户端通信深度解析
c语言·网络·tcp/ip
老花眼猫8 小时前
数学艺术图案画-繁花(四)
c语言·经验分享·青少年编程·游戏程序
Tanecious.8 小时前
蓝桥杯备赛:Day1-奖学金
c语言·c++·蓝桥杯
爱编码的小八嘎10 小时前
C语言完美演绎6-14
c语言
江公望12 小时前
GNU C语句表达式,10分钟讲清楚
c语言·开发语言·c++