顺序表基本功能的实现 — 纯代码

目录

顺序表基本功能的实现

一 、SList.h文件

二、SList.c文件

三、Test.c文件


顺序表基本功能的实现

一 、SList.h文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* arr;
	int size;
	int capacity;
}SL;

//顺序表初始化
void SLInit(SL* ps);

//头插
void SLPushFront(SL* ps,SLDataType x);

//尾插
void SLPushBack(SL* ps, SLDataType x);

//头删
void SLPopFront(SL* ps);

//尾删
void SLPopBack(SL* ps);

//特定位置插入
void SLInsert(SL* ps, int pos, SLDataType x);

//特定位置删除
void SLErase(SL* ps, int pos);

//更改特定位置数据
void SLUpdata(SL* ps, int pos, SLDataType x);

//查找 获取特定元素下标
int SLFind(SL* ps, SLDataType x);

//索引 给定下标寻找元素
SLDataType SLSearch(SL* ps, int pos);

//打印顺序表
void SLPrint(SL* ps);

//顺序表的销毁
void SLDestroy(SL* ps);

二、SList.c文件

#include"SeqList.h"

//顺序表初始化
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLCheckCapacity(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
}

//头插
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps != NULL);//防止传进空指针
	SLCheckCapacity(ps);//判断空间是否充足
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}

//尾插
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps != NULL);
	SLCheckCapacity(ps);
	ps->arr[ps->size] = x;
	ps->size++;
}

//头删
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

//尾删
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}

//特定位置之前插入
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	ps->size++;
}

//特定位置删除
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(ps->size != 0 && pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

//更改特定位置数据
void SLUpdata(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	ps->arr[pos] = x;
}

//查找 获取特定元素下标
int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}

//索引 给定下标寻找元素
SLDataType SLSearch(SL* ps, int pos)
{
	assert(ps);
	if (pos >= 0)
	{
		return ps->arr[pos];
	}
	else {
		return pos;
	}
}

//打印顺序表
void SLPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}

//顺序表的销毁
void SLDestroy(SL* ps)
{
	if (ps->arr != NULL)
	{
		free(ps->arr);
		ps->arr = NULL;
	}
	ps->size = ps->capacity = 0;
}

三、Test.c文件

#include"SeqList.h"

void test01()
{
	SL sl;

	//顺序表初始化
	SLInit(&sl);

	//头插
	SLPushFront(&sl, 1);
	SLPushFront(&sl, 2);
	SLPushFront(&sl, 3);
	SLPushFront(&sl, 4);
	SLPrint(&sl);

	//尾插
	SLPushBack(&sl, 100);
	SLPushBack(&sl, 200);
	SLPrint(&sl);

	//头删
	SLPopFront(&sl);
	SLPopFront(&sl);
	SLPrint(&sl);

	//尾删
	SLPopBack(&sl);
	SLPopBack(&sl);
	SLPrint(&sl);

	//特定位置之前插入
	SLInsert(&sl, 1, 120);
	SLInsert(&sl, 1, 150);
	SLInsert(&sl, 1, 180);
	SLPrint(&sl);

	//特定位置删除
	SLErase(&sl, 1);
	SLErase(&sl, 1);
	SLPrint(&sl);

	//更改特定位置数据
	SLUpdata(&sl, 1, 300);
	SLPrint(&sl);

	//查找 获取特定元素下标
	int find = SLFind(&sl, 300);
	if (find >= 0)
	{
		printf("找到了!下标为%d\n", find);
	}
	else{
		printf("没找到!\n");
	}

	//索引 给定下标寻找元素
	SLDataType search = SLSearch(&sl, 1);
	if (search >= 0)
	{
		printf("下标对应的元素为 %d\n", search);
	}
	else {
		printf("输入的下标%d不合法!\n",search);
	}

	//顺序表的销毁
	SLDestroy(&sl);
}

int main()
{
	test01();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值