顺序表(顺序结构)(Sequence List)

目录

SeqList.h

SeqList.c

1.初始化顺序表(SLInit)

2.顺序表销毁(SLDestroy)

3.打印顺序表(SLPrint)

4.返回顺序表元素个数(SLCheckCapacity)

5.尾插(SLPushBack)

6.头插(SLPushFront)

7.尾删(SLPopBack)

8.头删(SLPopFront)

9.指定位置插入(SLInsert)

10.指定位置删除(SLErase)

11.顺序表查找(SLFind)


在基本的四种物理结构(存储结构)中,顺序结构是较为基础的,了解顺序表的实现可以帮助我们更好的理解顺序结构的实现。

SeqList.h

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


typedef int SLDataType;
//typedef int shujuleixing;

// sequence list
typedef struct SeqList
{
	SLDataType* a;
	int size;      // 有效数据
	int capacity;  // 空间容量
}SL;

void SLInit(SL* psl);
void SLDestroy(SL* psl);

void SLPrint(SL* psl);
void SLCheckCapacity(SL* psl);

// 头尾插入删除
void SLPushBack(SL* psl, SLDataType x);
void SLPushFront(SL* psl, SLDataType x);
void SLPopBack(SL* psl);
void SLPopFront(SL* psl);

// 任意下标位置的插入删除
void SLInsert(SL* psl, int pos, SLDataType x);
void SLErase(SL* psl, int pos);

// 找到返回下标
// 没有找到返回-1
int SLFind(SL* psl, SLDataType x);


SeqList.c

1.初始化顺序表(SLInit)

void SLInit(SL* psl)
{
	assert(psl);

	psl->a = NULL;
	psl->size = 0;
	psl->capacity = 0;
}

2.顺序表销毁(SLDestroy)

void SLDestroy(SL* psl)
{
	assert(psl);

	if (psl->a != NULL)
	{
		free(psl->a);
		psl->a = NULL;
		psl->size = 0;
		psl->capacity = 0;
	}
}

3.打印顺序表(SLPrint)

void SLPrint(SL* psl)
{
	assert(psl);

	for (int i = 0; i <= psl->size; i++)
	{
		printf("%d ", psl->a[i]);
	}
	printf("\n");
}

4.返回顺序表元素个数(SLCheckCapacity)

void SLCheckCapacity(SL* psl)
{
	assert(psl);

	if (psl->size == psl->capacity)
	{
		int newCapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(psl->a, sizeof(SLDataType)*newCapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}

		psl->a = tmp;
		psl->capacity = newCapacity;
	}
}

5.尾插(SLPushBack)

void SLPushBack(SL* psl, SLDataType x)
{
	assert(psl);

	SLCheckCapacity(psl);

	psl->a[psl->size] = x;
	psl->size++;
}

6.头插(SLPushFront)

void SLPushFront(SL* psl, SLDataType x)
{
	assert(psl);

	SLCheckCapacity(psl);

	// 挪动数据
	int end = psl->size - 1;
	while (end >= 0)
	{
		psl->a[end + 1] = psl->a[end];
		--end;
	}

	psl->a[0] = x;
	psl->size++;
}

7.尾删(SLPopBack)

void SLPopBack(SL* psl)
{
	assert(psl);
	/*if (psl->size == 0)
	{
		return;
	}*/
	assert(psl->size > 0);

	//psl->a[psl->size - 1] = -1;
	psl->size--;
}

8.头删(SLPopFront)

void SLPopFront(SL* psl)
{
	assert(psl);

	// 暴力检查
	assert(psl->size > 0);

	int begin = 1;
	while (begin < psl->size)
	{
		psl->a[begin - 1] = psl->a[begin];
		++begin;
	}

	psl->size--;
}

9.指定位置插入(SLInsert)

void SLInsert(SL* psl, int pos, SLDataType x)
{
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);

	SLCheckCapacity(psl);

	// 挪动数据
	int end = psl->size - 1;
	while (end >= pos)
	{
		psl->a[end + 1] = psl->a[end];
		--end;
	}

	psl->a[pos] = x;
	psl->size++;
}

10.指定位置删除(SLErase)

void SLErase(SL* psl, int pos)
{
	assert(psl);
	assert(pos >= 0 && pos < psl->size);

	// 挪动覆盖
	int begin = pos + 1;
	while (begin < psl->size)
	{
		psl->a[begin - 1] = psl->a[begin];
		++begin;
	}

	psl->size--;
}

11.顺序表查找(SLFind)

int SLFind(SL* psl, SLDataType x)
{
	assert(psl);

	for (int i = 0; i < psl->size; i++)
	{
		if (psl->a[i] == x)
		{
			return i;
		}
	}

	return -1;
}

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值