手撕顺序表

本文详细介绍了动态顺序表的概念,包括其基本概念、静态与动态的区别,以及如何实现顺序表的初始化、增容、打印、销毁、尾插、头插、删除和查找等操作。
摘要由CSDN通过智能技术生成

目录

顺序表的基本概念

接口实现

定义顺序表

 始化顺初序表

增容

 打印

 销毁顺序表

顺序表尾插 

测试

 ​编辑

 头插

 测试

 尾删

测试

 头删

测试

查找

在指定位置插入数据

测试

 删除指定位置的数据

 测试

 完整代码

Seqlist.h

SeqList.c


顺序表的基本概念

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般用数组存储,在数组上完成对对数据的增删查改

顺序表分为静态顺序表动态顺序表

静态顺序表:使用定长数组存储数据

动态顺序表:使用动态开辟的数组存储数据

接口实现

静态顺序表比较局限,在现实中我们一般研究动态顺序表对数据的存储

 命名数据类型,方便下一次修改数据类型

定义顺序表

 始化顺初序表

增容

如果顺序表中元素个数等于顺序表的最大容量就需要进行增容

 打印

 销毁顺序表

顺序表尾插 

 

测试

 

 头插

 测试

 尾删

测试

 头删

测试

查找

 

在指定位置插入数据

测试

 

 删除指定位置的数据

 测试

 完整代码

Seqlist.h

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

typedef int SLDataType;

//动态顺序表
typedef struct SeqList
{
	SLDataType* array;      //指向动态开辟的数组
	int size;               //顺序的元素个数
	int capicity;           //数组的容量大小
}SeqList;

void SeqListInit(SeqList* psl);     //初始化顺序表

void CheckCapicity(SeqList* psl);      //扩容

void SeqListPrint(SeqList* psl);      //打印顺序表

void DestorySeqList(SeqList* psl);     //销毁顺序表

void SeqListPushback(SeqList* psl,SLDataType x);      //尾插

void SeqListPushfront(SeqList* psl, SLDataType x);      //头插

void SeqListpopback(SeqList* psl);      //尾删

void SeqListpopfront(SeqList* psl);      //头删

int SeqListFind(SeqList* ps, SLDataType x);     //查找

void SeqLIstInsert(SeqList* psl, SLDataType pos, SLDataType x);    //在pos位置插入值

void SeqLIstErase(SeqList* psl, SLDataType pos);    //删除pos位置的值

SeqList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "SeqList.h"

void SeqListInit(SeqList* psl)          //初始化顺序表
{
	assert(psl);               //断言,排查控指针

	psl->array = (SLDataType*)malloc(sizeof(SLDataType) * 4);    //动态开辟数组
	if (psl->array == NULL)       //开辟失败
	{
		perror("malloc failed");
		exit(-1);
	}

	psl->size = 0;            //元素为0
	psl->capicity = 4;        //容量为4
}


void CheckCapicity(SeqList* psl)      //扩容
{
	assert(psl);   //断言

	if (psl->size == psl->capicity)      //元素个数等于容量,增容
	{
		SLDataType* tmp = (SLDataType*)realloc(psl->array, psl->capicity * 2 * sizeof(SLDataType));  //增容
		if (tmp == NULL)        //增容失败
		{
			perror("realloc failed");
			exit(-1);
		}

		psl->array = tmp;
		psl->capicity = psl->capicity * 2;
	}
}

void SeqListPrint(SeqList* psl)      //打印顺序表
{
	assert(psl);         //断言
	for (int i = 0; i < psl->size; i++)
	{
		printf("%d ", psl->array[i]);
	}
	printf("\n");
}

void DestorySeqList(SeqList* psl)     //销毁顺序表
{
	assert(psl);             //断言
	free(psl->array);
	psl->array = NULL;
	psl->size = psl->capicity = 0;
}

void SeqListPushback(SeqList* psl,SLDataType x)      //尾插
{
	assert(psl);
	CheckCapicity(psl);
	psl->array[psl->size] = x;
	psl->size += 1;
}

void SeqListPushfront(SeqList* psl, SLDataType x)      //头插
{
	assert(psl);
	CheckCapicity(psl);
	/*for (int i = psl->size; i > 0; i--)
	{
		psl->array[i] = psl->array[i - 1];
	}
	psl->array[0] = x;*/
	int end = psl->size - 1;
	while (end >= 0)
	{
		psl->array[end + 1] = psl->array[end];
		end--;
	}
	psl->array[0] = x;
	psl->size += 1;
}

void SeqListpopback(SeqList* psl)      //尾删
{
	assert(psl);
	if (psl->size == 0)
	{
		return;
	}
	psl->array[psl->size - 1] = 0;
	psl->size--;
}

void SeqListpopfront(SeqList* psl)      //头删
{
	assert(psl);
	if (psl->size == 0)
	{
		return;
	}
	int i = 0;
	while (i < psl->size - 1)
	{
		psl->array[i] = psl->array[i + 1];
		i++;
	}
	psl->size--;
}

int SeqListFind(SeqList* ps, SLDataType x)     //查找
{
	assert(ps);
	for(int i=0;i<ps->size;i++)
	{
		if (ps->array[i] == x)
		{
			return i;
		}
	}
	return -1;
}

void SeqLIstInsert(SeqList* psl, SLDataType pos, SLDataType x)    //在pos位置插入值
{
	assert(psl);
	assert(pos <= psl->size&& pos >= 0);
	CheckCapicity(psl);
	
	int end = psl->size - 1;
	while (end >= pos)
	{
		psl->array[end + 1] = psl->array[end];
		end--;
	}
	psl->array[pos] = x;
	psl->size += 1;
}

void SeqLIstErase(SeqList* psl, SLDataType pos)    //删除pos位置的值
{
	assert(psl);
	assert(pos >= 0 && pos <= psl->size);

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值