实现顺序表的增删改查

实现顺序表的增删改查,先对函数进行定义,再在主函数中使用

头文件:

复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define INIT_CAPACITY 4
typedef int SLDataType;
// 动态顺序表 -- 按需申请
typedef struct SeqList
{
    SLDataType* a;
    int size;     // 有效数据个数
    int capacity; // 空间容量
}SL;

//初始化和销毁
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
//扩容
void SLCheckCapacity(SL* ps);

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

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

顺序表函数实现:

复制代码
#define  _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"

void SLInit(SL* ps)
{
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}
void SLDestroy(SL* ps)
{
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
	free(ps);
}

void SLPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ",ps->a[i]);
	}
	printf("\n");
}
//扩容
void SLCheckCapacity(SL* ps)
{
	assert(ps);
	int Newcapcity = ps->capacity == 0 ? 4 : ps->capacity * 2 ;
	SLDataType* tmp = (SLDataType*)realloc(ps->a, Newcapcity*sizeof(SLDataType));
	if (tmp == NULL)
	{
		perror("realloc fail");
		exit(1);
	}
	ps->a = tmp;
	ps->capacity = Newcapcity;
}

//头部插入删除 / 尾部插入删除
void SLPushBack(SL* ps, SLDataType x)
{
	if (ps->capacity == ps->size)
	{
		SLCheckCapacity(ps);
	}
	
	ps->a[ps->size++] = x;
	
}
void SLPopBack(SL* ps)
{
	ps->size--;
}
void SLPushFront(SL* ps, SLDataType x)
{
	if (ps->capacity == ps->size)
	{
		SLCheckCapacity(ps);
	}
	for (int i = 0; i < ps->size;i++)
	{
		ps->a[ps->size - i] = ps->a[ps->size - i - 1];
	}
	ps->a[0] = x;
	ps->size++;
}
void SLPopFront(SL* ps)
{
	for (int i = 0; i < ps->size-1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}

//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
	if (ps->capacity == ps->size)
	{
		SLCheckCapacity(ps);
	}
	for (int i = 0; ps->size-i > pos; i++)
	{
		ps->a[ps->size - i] = ps->a[ps->size - i - 1];
	}
	ps->a[pos] = x;
	ps->size++;
}
void SLErase(SL* ps, int pos)
{
	for (int i = 0; pos+i<ps->size-1; i++)
	{
		ps->a[pos+i] = ps->a[pos + 1 + i];
	}
	ps->size--;
}
int SLFind(SL* ps, SLDataType x)
{

	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;


}

主函数:

复制代码
#define  _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"

int main()
{
	SL we;
	SLInit(&we);
	SLPushBack(&we, 1);
	SLPushBack(&we, 2);
	SLPushBack(&we, 3);
	SLPushBack(&we, 4);
	SLPushBack(&we, 4);
	SLPushFront(&we, 9);
	SLPopBack(&we);
	SLInsert(&we, 3, 6);
	SLErase(&we, 3);
	SLPrint(&we);
	printf("查找数字3,其下标为%d\n", SLFind(&we, 3));
	return 0;
}
相关推荐
北域码匠5 小时前
高通滤波算法深度解析(High-Pass Filter)
stm32·算法·c#·数字信号处理·嵌入式开发·滤波算法·高通滤波
指掀涛澜天下惊6 小时前
强化学习进阶篇八 策略梯度算法
深度学习·学习·算法·强化学习
乌萨奇也要立志学C++6 小时前
【洛谷】kmp算法
开发语言·算法
禹凕6 小时前
Dijkstra算法详解与应用
python·算法
YaraMemo7 小时前
元启发式算法框架
人工智能·算法·5g·信息与通信·启发式算法·信号处理
weixin_307779137 小时前
一维无粘 Burgers 方程的激波形成问题:MacCormack 格式求解
c++·算法·matlab
HugoStudio_SWAN8 小时前
洛谷 B4500 / B4449 / B3843 凯撒密码、密码强度与密码合规——加密与安全的三道门
c++·学习·程序人生·算法·安全
枫叶林FYL9 小时前
【群体智能集群控制工程实践】第3章 一致性协同算法
算法
禹凕9 小时前
滑动窗口算法实战指南
python·算法
DisonTangor9 小时前
【腾讯混元雪耻归来】 Hy4 preview:770B 参数 MoE 旗舰模型,1M 上下文全面开源
人工智能·算法·开源·aigc·腾讯云·腾讯云ai代码助手