顺序表各种接口的实现(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;
}
相关推荐
liangbm33 分钟前
MATLAB系列05:自定义函数
开发语言·笔记·matlab·教程·函数·自定义函数·按值传递
计算机学姐3 分钟前
基于PHP的电脑线上销售系统
开发语言·vscode·后端·mysql·编辑器·php·phpstorm
三玖诶12 分钟前
如何在 Qt 的 QListWidget 中为某一行添加点击事件
开发语言·qt
InJre19 分钟前
QT widgets 窗口缩放,自适应窗口大小进行布局
开发语言·qt·ui
可愛小吉22 分钟前
Python 课程10-单元测试
开发语言·python·单元测试·tdd·unittest
五味香34 分钟前
C++学习,动态内存
java·c语言·开发语言·jvm·c++·学习·算法
无名之逆35 分钟前
计算机专业的就业方向
java·开发语言·c++·人工智能·git·考研·面试
Aurora200541 分钟前
蓝桥杯2024省C
c语言·算法·蓝桥杯
爱棋笑谦41 分钟前
二叉树计算
java·开发语言·数据结构·算法·华为od·面试
蟹至之1 小时前
字符函数 和 字符串函数 的使用与模拟
c语言·字符串·指针·const关键词