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;
}
相关推荐
舟舟亢亢3 小时前
算法总结——二叉树【hot100】(上)
java·开发语言·算法
weixin_477271694 小时前
根象:树根。基石。基于马王堆帛书《周易》原文及甲骨文还原周朝生活活动现象(《函谷门》原创)
算法·图搜索算法
普通网友4 小时前
多协议网络库设计
开发语言·c++·算法
努力努力再努力wz4 小时前
【Linux网络系列】:TCP 的秩序与策略:揭秘传输层如何从不可靠的网络中构建绝对可靠的通信信道
java·linux·开发语言·数据结构·c++·python·算法
daxi1505 小时前
C语言从入门到进阶——第9讲:函数递归
c语言·开发语言·c++·算法·蓝桥杯
持续学习的程序员+16 小时前
强化学习Q-chunking算法
算法
Polaris北6 小时前
第二十七天打卡
开发语言·c++·算法
风吹乱了我的头发~6 小时前
Day30:2026年2月20日打卡
算法
blackicexs7 小时前
第五周第五天
算法
不吃橘子的橘猫7 小时前
《集成电路设计》复习资料2(设计基础与方法)
学习·算法·fpga开发·集成电路·仿真·半导体