实现顺序表的增删改查

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

头文件:

复制代码
#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 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼5 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
m0_547486665 天前
《数据结构教程》全套 PPT课件2026
数据结构
旖旎夜光5 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark5 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her15 天前
并查集-听课笔记
笔记·算法·并查集
码流子5 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven5 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark5 天前
从算法设计模式看编程思维的抽象能力4
算法