实现顺序表的增删改查

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

头文件:

复制代码
#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;
}
相关推荐
leiming64 小时前
C++ vector容器
开发语言·c++·算法
Xの哲學5 小时前
Linux流量控制: 内核队列的深度剖析
linux·服务器·算法·架构·边缘计算
yaoh.wang6 小时前
力扣(LeetCode) 88: 合并两个有序数组 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·双指针
LYFlied7 小时前
【每日算法】 LeetCode 56. 合并区间
前端·算法·leetcode·面试·职场和发展
艾醒7 小时前
大模型原理剖析——多头潜在注意力 (MLA) 详解
算法
艾醒7 小时前
大模型原理剖析——DeepSeek-V3深度解析:671B参数MoE大模型的技术突破与实践
算法
jifengzhiling8 小时前
零极点对消:原理、作用与风险
人工智能·算法
鲨莎分不晴8 小时前
【前沿技术】Offline RL 全解:当强化学习失去“试错”的权利
人工智能·算法·机器学习
XFF不秃头9 小时前
力扣刷题笔记-全排列
c++·笔记·算法·leetcode