[C数据结构]顺序表

[C数据结构]顺序表,第1张

以下是函数声明:

#pragma once
#include
#include
#include
typedef int SLDataType;

typedef struct SeqList//动态顺序表
{
	SLDataType* a;//指向动态空间数组的地址
	int size;//已存储的元素个数
	int capacity;//当前动态数组最多能存储的元素个数
}SL;

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

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

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

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

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

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

//对任意位置的插入
void SLInsert(SL* ps, int pos, SLDataType x);

//对任意位置的删除
void SLErase(SL* ps, int pos);

//查找顺序表元素
int SLFind(SL* ps, SLDataType x);

//对任意位置修改
void SLModify(SL* ps, int pos, SLDataType x);

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

以下函数实现:

#include"SeqList.h"

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

//检查空间大小,决定是否扩容。
void SLCheckCapcity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		ps->capacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		//int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* ptr = (SLDataType*)realloc(ps->a, (ps->capacity) * sizeof(SLDataType));
		if (ptr == NULL)
		{
			perror("realloc");
			exit(-1);//结束程序
			//return;
		}
		ps->a = ptr;
		//ps->capacity=newCapacity;
	}
}

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

//尾插
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapcity(ps);
	ps->a[ps->size] = x;
	ps->size++;

	//用任意插入函数取代尾插函数
	//SLInsert(ps, ps->size, x);
}

//头插
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLCheckCapcity(ps);
	//挪动数据
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;

	//用任意插入函数取代头插函数
	//SLInsert(ps, 0, x);
}

//尾删
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size>0);
	ps->size--;
	/*if (ps->size == 0)
		printf("顺序表已空,无法继续删除\n");
	else
		ps->size--;*/

	//任意位置删除函数代替尾删函数
	//SLErase(ps, ps->size-1);
}

//头删
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size > 0);
	int start = 1;
	while (start < (ps->size))//ps->size作为下标是对应着即将尾插的元素,所以小于ps->size.
	{
		ps->a[start-1] = ps->a[start];
		start++;
	}
	ps->size--;

	//任意位置删除函数代替头删函数
	//SLErase(ps, 0);
}

//任意位置插入
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);//ps->size作为下标时表示的是下一个元素的下标,尾插是特殊的任意位置插入.
	SLCheckCapcity(ps);
	int end = ps->size;//end作为下标,是被覆盖的那个元素。
	while (end>=pos+1)
	{
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

//任意位置删除
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size - 1);
	int begin = pos;//begin作为下标,是被覆盖的那个元素。
	while (begin < ps->size - 1)
	{
		ps->a[begin] = ps->a[begin + 1];
		begin++;
	}
	ps->size--;
}


//查找顺序表元素
int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
			return i;
	}
	return -1;
}

//对任意位置修改
void SLModify(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	ps->a[pos] = x;
}

//销毁顺序表
void SLDestory(SL* ps)
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
		ps->size = 0;
		ps->capacity = 0;
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/874769.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-13
下一篇2022-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存