顺序表原码(练习版)

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

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

 

 

 

 

 

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值