数据结构之顺序表的实现(含全部代码)

从今天起开始编写数据结构中各种数据结构和算法的实现
以下是顺序表的实现

//所需要的头文件
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<string.h>
//定义数据类型
typedef int SQDataType;
//创建顺序表
typedef struct SList 
{
	SQDataType* a;
	int size;//元素个数
	int capacity;//容量
}SL;

线性表中的顺序表的实现,主要实现函数如下

//以下是所需要实现的函数
//初始化
void SListInit(SL* ps);
//打印
void SListPrint(SL* ps);
//销毁顺序表
void SListDestory(SL* ps);
//检查是否增容
void SListCheckCapacity(SL* ps);
//尾插
void SListPushBack(SL* ps, SQDataType x);
//头插
void SListPushFront(SL* ps, SQDataType x);
//尾删
void SListPopBack(SL* ps);
//头删
void SListPopFront(SL* ps);
//pos 前插入x
void SListInsert(SL* ps, int pos, SQDataType x);
//销毁pos处的元素
void SListErase(SL* ps, int pos);
//查找
int SListFind(SL* ps, SQDataType x);
//修改
void SListModity(SL* ps, int pos, SQDataType x);

//函数的实现
#include"SList.h"

void SListInit(SL* ps)
{
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}
void SListPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
void SListDestory(SL* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}
//检查是否增容
void SListCheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 2 : ps->capacity * 2;
		SQDataType* tmp = (SQDataType*)realloc(ps->a, newcapacity * sizeof(SQDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capacity = newcapacity;
		}
	}
}
//尾插
void SListPushBack(SL* ps, SQDataType x)
{
	/*SListCheckCapacity(ps);
	ps->a[ps->size] = x;
	ps->size++;*/
	SListInsert(ps, ps->size, x);
}
//头插
void SListPushFront(SL* ps, SQDataType x)
{
	SListCheckCapacity(ps);
	int end = ps->size - 1;
	while(end>=0)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
	//SListInsert(ps, 0, x);
}
//尾删
void SListPopBack(SL* ps);
//头删
void SListPopFront(SL* ps);
//pos 前插入x
void SListInsert(SL* ps, int pos, SQDataType x)
{
	assert(pos <= ps->size);
	SListCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}
//销毁pos处的元素
void SListErase(SL* ps, int pos)
{
	assert(pos < ps->size);
	int cur = pos + 1;
	while (cur<ps->size)
	{
		ps->a[cur - 1] = ps->a[cur];
		cur++;
	}
	ps->size--;
}
//查找
int SListFind(SL* ps, SQDataType x)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
			return i;
	}
	return -1;
}
//修改
void SListModity(SL* ps, int pos, SQDataType x)
{
	assert(pos <= ps->size - 1);
	ps->a[pos] = x;
}



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

求知.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值