顺序表各种接口的实现(C)

线性表
  • 线性表是n个具有相同特性的数据元素的有限序列
  • 线性表是一种在实际中广泛使用的数据结构
  • 常见的线性表:顺序表、链表、栈、队列、字符串…
  • 线性表在逻辑上是线性结构,也就说是连续的一条直线。
  • 物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储
顺序表

概念

  • 用一段物理地址连续的存储单元
  • 依次存储数据元素的线性结构,一般情况下采用数组存储。
  • 在数组上完成数据的增删查改
    一般可以分为
  1. 静态顺序表:使用定长数组存储元素
#define N 1000
typedef int SLDataType;

typedef struct SeqList    //对类型进行重命名,方便后续变换类型
{
	SLDataType array[N];  //定长数组
	size_t size;          //有效数据个数
}SL;
  1. 动态顺序表:使用动态开辟的数组存储
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int SLDataType;

typedef struct SeqList
{
	SLDataType* array;  //指向动态开辟的数组
	size_t size;        //有效数据个数
	size_t capacity;    //容量空间的大小
}SL;

![[Pasted image 20240810054754.png]]

初始化
  • 每次调用接口,要判断传入的指针是不是空指针
void SLInit(SL* ps)
{
	assert(ps);
	
	ps->a = (SLDataType*)malloc(sizeof(SLDataType)*4);
	if (ps->a == NULL)
	{
		perror("malloc failed");
		exit(-1);         //程序直接在这里结束
	}
	
	ps->size = 0;
	ps->capacity = 0;
}
  • 动态内存,用malloc开辟空间,先开4个
  • 检查malloc出的地址是否为空指针
  • 将size和capacity初始化为0
销毁
void SLDestroy(SL* ps)
{
	assert(ps);

	free(ps-> a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}
  • 直接释放掉malloc的空间,将a指针置为NULL
  • 将size和capacity置为0
打印
void SLPrint(SL* ps)
{
	assert(ps);
	
	for (int i = 0; i < ps-> size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
  • 从下标0开始往右遍历,直到最后一个元素,即size-1
扩容
void SLCheckCapacity(SL* ps)
{
	assert(ps);
	
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
		if (tmp == NULL)
		{
			perror("realloc failed");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity *= 2;
	}
}
  • 当size等于capacity时,realloc,重新开辟空间,一般扩2倍容量
尾插
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
		if (tmp == NULL)
		{
			perror("realloc failed");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity *= 2;
	}

	ps->a[ps->size] = x;
	ps->size++;
}
  • 在下标size处插入值,size++,如果size等于capacity,就扩容
尾删
void SLPopBack(SL* ps)
{
	assert(ps);
	
	//if (ps->size == 0)
	//	return;
	assert(ps->size > 0);
	
	ps->size--;
}
  • 直接size–,覆盖掉尾部的元素
  • 断言size,size不能减为负数
头插
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	
	SLCheckCapacity(ps);

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

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

需要整体元素往右挪动
而且只能从右往左开始遍历,反过来会覆盖没有处理的元素

end从size-1开始
![[Pasted image 20240810060526.png]]

把当前元素往右移动一格,传给end+1
![[Pasted image 20240810061642.png]]

end–,往前一格
![[Pasted image 20240810060703.png]]

直到end=0
![[Pasted image 20240810060808.png]]

把x传进来,size++
![[Pasted image 20240810060931.png]]

当size=capacity时,就需要扩容

头删
void SLPopFront(SL* ps)
{
	assert(ps);
	
	assert(ps->size > 0);

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

	ps->size--;
}

从左往右,挨个往左移动
begin从下标1开始
![[Pasted image 20240810061444.png]]

把begin的内容覆盖给begin-1
![[Pasted image 20240810062211.png]]

begin++
![[Pasted image 20240810061551.png]]

直到begin=size-1
![[Pasted image 20240810062002.png]]

size–
![[Pasted image 20240810062046.png]]

查找
int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}

	return -1;
}
  • 依次遍历每个元素,找到以后,返回下标,否则返回-1
插入
void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);

	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}

	ps->a[pos] = x;
	ps->size++;
}
  • 先判断pos是否合法,pos可以等于size,因为可以尾插
    end从size-1开始
    ![[Pasted image 20240810064522.png]]

当end大于等于pos时,end开始循环
将end传给end+1
![[Pasted image 20240810064811.png]]

end–,往左移动
![[Pasted image 20240810064743.png]]

直到end=pos
![[Pasted image 20240810064858.png]]

将x传给pos,size++
![[Pasted image 20240810064941.png]]

当pos直接等于size时,相当于尾插
等于0时,相当于头插

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

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

	ps->size--;
}
  • 同样判断pos是否合法
    begin从pos+1开始
    ![[Pasted image 20240810065127.png]]

将begin传给begin-1
![[Pasted image 20240810065200.png]]

begin++
![[Pasted image 20240810065219.png]]

直到begin=size-1
![[Pasted image 20240810065252.png]]

size–
![[Pasted image 20240810065322.png]]

当pos等于size-1时,相当于尾删
等于0时,相等于头删

修改
void SLModify(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	
	assert(pos >= 0 && pos < ps->size);
	
	ps->a[pos] = x;
}
  • 判断pos的合法性,直接通过数组下标修改

声明定义分离实现

//管理数据 -- 增删查改
//初始化
void SLInit(SL* ps);
//销毁
void SLDestroy(SL* ps);
//打印
void SLPrint(SL* ps);
//扩容
void SLCheckCapacity(SL* ps);

//尾插
void SLPushBack(SL* ps, SLDataType x);
//尾删
void SLPopBack(SL* ps);
//头插
void SLPushFront(SL* ps, SLDataType x);
//头删
void SLPopFront(SL* ps);
//查找
int SLFind(SL* ps, SLDataType x);
//插入
void SLInsert(SL* ps, int pos, SLDataType x);
//删除
void SLErase(SL* ps, int pos);
//修改
void SLModify(SL* ps, int pos, SLDataType x);
#include "SeqList.h"

void SLInit(SL* ps)
{
	assert(ps);
	
	ps->a = (SLDataType*)malloc(sizeof(SLDataType)*4);
	if (ps->a == NULL)
	{
		perror("malloc failed");
		exit(-1);         //程序直接在这里结束
	}
	
	ps->size = 0;
	ps->capacity = 0;
}

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

	free(ps-> a);
	ps->a = NULL;
	ps->capacity = ps->size = 0;
}

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

void SLCheckCapacity(SL* ps)
{
	assert(ps);
	
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
		if (tmp == NULL)
		{
			perror("realloc failed");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity *= 2;
	}
}

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	
	if (ps->size == ps->capacity)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, ps->capacity * 2 * (sizeof(SLDataType)));
		if (tmp == NULL)
		{
			perror("realloc failed");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity *= 2;
	}

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

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

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

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

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

void SLPopFront(SL* ps)
{
	assert(ps);
	
	assert(ps->size > 0);

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

	ps->size--;
}

int SLFind(SL* ps, SLDataType x)
{
	assert(ps);
	
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}

	return -1;
}

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

	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		--end;
	}

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

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

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

	ps->size--;
}

void SLModify(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	
	assert(pos >= 0 && pos < ps->size);
	
	ps->a[pos] = x;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值