顺序表的实现

顺序表的实现其实就是和通讯录差不多的,这里将会带领大家一起学习顺序表,总结四个字就是“增删查改”,接下来让我们开始学习吧!

(觉得还不错的铁铁可以点一个赞哦,谢谢!)

目录

一、Sqlist.h (对结构体定义和函数的声明)

二、Sqlist.c(函数的定义)

三、头插和尾插

四、头删与尾删

五、Sqlist.c

六、测试代码(test.c)以及运行结果


一、Sqlist.h (对结构体定义和函数的声明)

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

typedef int SLDateType;
typedef struct SeqList
{
	SLDateType* a;
	int size;
	int capacity;
}SeqList;

// 对数据的管理:增删查改 
void SeqListInit(SeqList* ps);
void SeqListDestroy(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, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);

二、Sqlist.c(函数的定义)

三、头插和尾插

尾插比较简单,只需要增加赋值且数组有效个数的个数即可,而头插,需要不断挪动数组

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	CheckSeqlist(ps);//进来先检查容量
	ps->size = x;
	ps->size++;
}

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	int end = 0;
	if (end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[0] = x;
	ps->size++;
}
四、头删与尾删

同样的,尾删也比较简单,如果该数组有效数据个数为0,无需尾删,否则就通过将数组最后一个数组赋为0,减小size的大小

头删:将后面的数据赋值给前面,最后减少有效数据的个数

void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	if (ps->size == 0)
		return;
	ps->a[ps->size - 1] = 0;
	ps->size--;
}

void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	int start = 0;
	while (start < ps->size - 1)
	{
		ps->a[start] = ps->a[start + 1];
		start++;
	}
	ps->size--;
}

接下来是删除和插入函数的定义,这两个函数可以指定任何位置进行插入和删除

插入:

删除

void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	CheckSeqlist(ps);//进来先检查容量
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end+1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	int start = ps->size - 1;
	while (start >= pos)
	{
		ps->a[start - 1] = ps->a[start];
		start--;
	}
	ps->size--;
}
五、Sqlist.c
#include"Sqlist.h"
#include<stdlib.h>
void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->a = (SLDateType*)malloc(sizeof(SLDateType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->capacity = 4;
	ps->size = 0;
}

void SeqListDestroy(SeqList* ps)
{
	assert(ps);
	free(ps->a);//开辟了空间一定要释放
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

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

void CheckSeqlist(SeqList* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		SLDateType* tmp = (SLDateType*)realloc(ps->a,sizeof(SLDateType) * ps->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc error");
			return;
		}
		ps->a = tmp;
		ps->capacity = ps->capacity * 2;
	}
}

void SeqListPushBack(SeqList* ps, SLDateType x)
{
	assert(ps);
	CheckSeqlist(ps);//进来先检查容量
	ps->a[ps->size] = x;
	ps->size++;
}

void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
	CheckSeqlist(ps);//进来先检查容量
	int end = ps->size-1;
	while(end >= 0)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}
	ps->a[0] = x;
	ps->size++;
}

void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	if (ps->size == 0)
		return;
	ps->a[ps->size - 1] = 0;
	ps->size--;
}

void SeqListPopBack(SeqList* ps)
{
	assert(ps);
	int start = 0;
	while (start < ps->size - 1)
	{
		ps->a[start] = ps->a[start + 1];
		start++;
	}
	ps->size--;
}

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

void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	CheckSeqlist(ps);//进来先检查容量
	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end+1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	int start = ps->size - 1;
	while (start >= pos)
	{
		ps->a[start - 1] = ps->a[start];
		start--;
	}
	ps->size--;
}
六、测试代码(test.c)以及运行结果
#include"Sqlist.h"

testsqilist1()
{
	SeqList s;
	SeqListInit(&s);
	SeqListPushBack(&s, 1);
	SeqListPushBack(&s, 2);
	SeqListPushBack(&s, 3);
	SeqListPushBack(&s, 4);
	SeqListPushBack(&s, 5);

	SeqListPrint(&s);

	SeqListPushFront(&s, 0);
	SeqListPushFront(&s, 1);
	SeqListPushFront(&s, 2);
	SeqListPushFront(&s, 3);

	SeqListPrint(&s);

	SeqListPopFront(&s);
	SeqListPopFront(&s);
	SeqListPopFront(&s);

	SeqListPrint(&s);

	SeqListPopBack(&s);
	SeqListPopBack(&s);
	SeqListPrint(&s);

	SeqListInsert(&s, 3, 30);
	SeqListPrint(&s);

	SeqListErase(&s, 1);
	SeqListPrint(&s);


	SeqListDestroy(&s);
}

int main()
{

	testsqilist1();
	return 0;
}

以上就是我的顺序表的实现啦!如果有问题,可以在评论区或者私信我说哦,谢谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

haru不是白仁仔呐

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

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

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

打赏作者

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

抵扣说明:

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

余额充值