数据结构之顺序表的增删查改


前言

这是我对于数据结构顺序表的增删查改理解以及代码实现
首先分为三个文件。
在这里插入图片描述


一、Seqlist.h

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

typedef int SLDateType;

typedef struct SeqList
{
	SLDateType* a;
	size_t size;//无符号整形
	size_t capacity;
}SeqList;
void SeqListInit(SeqList* ps);
void SeqListDestory(SeqList* ps);

void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType x);
void SeqListPushFront(SeqList* ps, SLDateType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);

// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);

二、函数功能的实现(Seqlist.c)

1.顺序表在pos位置插入 (SeqListInsert)

注意size_t是无符号整形,-1为111111111111111111111111111111111111
代码如下:

void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
	assert(ps);
	if (pos > ps->size)
	{
		printf("pos越界了%d\n",pos);
		return;
	}
	
	SeqListCheckCapacity(ps);//插入前一定要检查空间够不够用

	size_t end = ps->size;
	while (end > pos)//这是while不是if
	{
		ps->a[end] = ps->a[end - 1];
		--end;
	}
	ps->a[pos] = x;
	ps->size++;
}

2.头插(在pos=0处插入)

代码如下:

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	SeqListInsert(ps, 0, x);
}

3.尾插(在pos=size处插入)

代码如下:

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	SeqListInsert(ps, ps->size, x);
}

4. 顺序表在pos位置删除

代码如下:

void SeqListErase(SeqList* ps, size_t pos)
{
	assert(ps);
	if (pos > ps->size)
	{
		printf("越界了\n");
		return;
	}
	size_t begin = pos+1;
	while(begin < ps->size)//这是while不是if
	{
		ps->a[begin-1] = ps->a[begin];
		begin++;
	}
	if (ps->size > 0)
	{
		ps->size--;
	}
}

5头删(在pos=0处删除)

void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	SeqListErase(ps, 0);
}

6.尾删(在pos=size-1处删除)

void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	SeqListErase(ps, ps->size-1);
}

7.销毁顺序表

void SeqListDestory(SeqList* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}

8.查找数据所在位置

int SeqListFind(SeqList* ps, SLDateType x)
{
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

9.初始化顺序表

void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->size = 0;
}

10.打印顺序表

void SeqListPrint(SeqList* ps)
{
	assert(ps);
	int i = 0;
	for (i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}

11.检查空间够不够用

void SeqListCheckCapacity(SeqList* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		size_t newCapacity = ps->capacity==0?4:ps->capacity*2;
		SLDateType* tmp = realloc(ps->a,sizeof(SLDateType)*newCapacity);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capacity = newCapacity;
		}
	}
}

总结

顺序表其实就是结构体里加了个数组(int*a)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值