C语言实现顺序表

SeqList.h

复制代码
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int SLDataType;
#define INIT_CAPACITY 4
// 顺序表结构体定义
typedef struct SeqList
{
	SLDataType* a;           // 指向动态数组的指针
	int size;                // 当前有效元素个数
	int capacity;            // 当前开辟的空间容量
} SL;

// 初始化顺序表
void SLInit(SL* ps);

// 销毁顺序表,释放动态内存
void SLDestroy(SL* ps);

// 打印顺序表中所有元素
void SLPrint(SL* ps);

// 检查容量是否足够,不够则扩容
void SLCheckCapacity(SL* ps);

// 尾插:在顺序表末尾插入元素 x
void SLPushBack(SL* ps, SLDataType x);

// 尾删:删除顺序表最后一个元素
void SLPopBack(SL* ps);

// 头插:在顺序表开头插入元素 x
void SLPushFront(SL* ps, SLDataType x);

// 头删:删除顺序表第一个元素
void SLPopFront(SL* ps);

// 在 pos 位置插入元素 x(pos 从 0 开始)
void SLInsert(SL* ps, int pos, SLDataType x);

// 删除 pos 位置的元素(pos 从 0 开始)
void SLErase(SL* ps, int pos);

// 查找元素 x,返回其下标,找不到返回 -1
int SLFind(SL* ps, SLDataType x);

SeqList.c

复制代码
#include"SeqList.h"

void SLInit(SL* ps) {
    ps->a = (SLDataType*)malloc(sizeof(SLDataType) * INIT_CAPACITY);
    if (ps->a == NULL) {
        perror("malloc fail");
        return;
    }
    ps->size = 0;
    ps->capacity = INIT_CAPACITY;
}

void SLDestroy(SL* ps)
{
    free(ps->a);
    ps->a = NULL;
    ps->capacity = ps->size = 0;
}

void SLPrint(SL* 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, sizeof(SLDataType) * ps->capacity * 2);
        if (tmp == NULL) {
            perror("realloc fail");
            return;
        }
        ps->a = tmp;
        ps->capacity *= 2;
    }
}

void SLPushBack(SL* ps, SLDataType x)
{
    SLCheckCapacity(ps);
    ps->a[ps->size++] = x;
}

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

void SLPushFront(SL* ps, SLDataType x)
{
    assert(ps);
    SLCheckCapacity(ps);
    for (int i = ps->size - 1;i >= 0;i--) {
        ps->a[i + 1] = ps->a[i];
    }
    ps->a[0] = x;
    ps->size++;
}

void SLPopFront(SL* ps)
{
    assert(ps);
    assert(ps->size > 0);
    for (int i = 1;i < ps->size;i++) {
        ps->a[i - 1] = ps->a[i];
    }
    ps->size--;
}

void SLInsert(SL* ps, int pos, SLDataType x)
{
    assert(ps);
    assert(pos >= 0 && pos <= ps->size);
    SLCheckCapacity(ps);
    for (int i = ps->size - 1;i >= pos;i--) {
        ps->a[i + 1] = ps->a[i];
    }
    ps->a[pos] = x;
    ps->size++;
}

void SLErase(SL* ps, int pos)
{
    assert(ps);
    assert(pos >= 0 && pos < ps->size);
    for (int i = pos + 1;i < ps->size;i++) {
        ps->a[i - 1] = ps->a[i];
    }
    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;
}

(SLDataType*)malloc(sizeof(SLDataType) * INIT_CAPACITY);是给顺序表开辟初始数组空间,存放 `INIT_CAPACITY` 个 `SLDataType` 类型元素的连续空间。

SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * ps->capacity * 2);是给顺序表扩容,把原来数组空间扩大到原来的2倍。

Test.c

复制代码
#include"SeqList.h"

void TestSeqList1()
{
    SL s;
    SLInit(&s);
    SLPushBack(&s, 1);
    SLPushBack(&s, 2);
    SLPushBack(&s, 3);
    SLPushBack(&s, 4);
    SLPushBack(&s, 5);
    SLPushBack(&s, 6);
    SLPushBack(&s, 7);
    SLPushBack(&s, 8);
    SLPushBack(&s, 9);
    SLPrint(&s);

    SLPopBack(&s);
    SLPopBack(&s);
    SLPrint(&s);

    SLPushFront(&s, 9);
    SLPushFront(&s, 8);
    SLPushFront(&s, 5);
    SLPrint(&s);

    SLPopFront(&s);
    SLPrint(&s);

    SLInsert(&s, 3, 999);
    SLPrint(&s);

    SLErase(&s, 3);
    SLPrint(&s);

    printf("%d", SLFind(&s,8));
    
    
}

int main()
{
    TestSeqList1();

    return 0;
}
相关推荐
mygugu17 小时前
归纳理解epoch、batch、batch size、step、iteration深度学习名词
人工智能·算法
AI科技星18 小时前
基于双隐含量(角速度 +质量 )的全量变形公式体系-发现新公式
开发语言·人工智能·线性代数·算法·矩阵·数据挖掘
minji...18 小时前
Linux 基础IO (三) (用户缓冲区/内核缓冲区深刻理解)
java·linux·运维·服务器·c++·算法
困死,根本不会18 小时前
蓝桥杯python备赛笔记之(八)动态规划(DP)
笔记·python·学习·算法·蓝桥杯·动态规划
whycthe18 小时前
c++动态规划算法详解
c++·算法·动态规划
不想看见40418 小时前
Single Number位运算基础问题--力扣101算法题解笔记
数据结构·算法
靠沿18 小时前
【优选算法】专题十二——栈
算法
李昊哲小课19 小时前
Python 高级数据结构
开发语言·数据结构·python
无心水19 小时前
【任务调度:框架】10、2026最新!分布式任务调度选型决策树:再也不纠结选哪个
人工智能·分布式·算法·决策树·机器学习·架构·2025博客之星
我头发还没掉光~19 小时前
【C++写详细总结①】从for循环到算法初步
数据结构·c++·算法