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

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

概念

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

    #define N 1000
    typedef int SLDataType;

    typedef struct SeqList //对类型进行重命名,方便后续变换类型
    {
    SLDataType array[N]; //定长数组
    size_t size; //有效数据个数
    }SL;

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

SeqList.h 复制代码
#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;
初始化
  • 每次调用接口,要判断传入的指针是不是空指针

    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开始

把当前元素往右移动一格,传给end+1

end--,往前一格

直到end=0

把x传进来,size++

当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开始

把begin的内容覆盖给begin-1

begin++

直到begin=size-1

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;
}
  • 依次遍历每个元素,找到以后,返回下标,否则返回-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开始

当end大于等于pos时,end开始循环

将end传给end+1

end--,往左移动

直到end=pos

将x传给pos,size++

当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开始

将begin传给begin-1

begin++

直到begin=size-1

size--

当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的合法性,直接通过数组下标修改

声明定义分离实现

SeqList.h 复制代码
//管理数据 -- 增删查改
//初始化
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);
SeqList.c 复制代码
#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;
}
相关推荐
zh_xuan5 分钟前
c++ 单例模式
开发语言·c++·单例模式
老胖闲聊30 分钟前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.11834 分钟前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之1 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
apocelipes1 小时前
Linux c 运行时获取动态库所在路径
linux·c语言·linux编程
豆沙沙包?1 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头2 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
liuyang-neu2 小时前
java内存模型JMM
java·开发语言
int型码农2 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
我很好我还能学4 小时前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++