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;
}
相关推荐
H_BB4 分钟前
DFS实现回溯算法
数据结构·c++·算法·深度优先
汀、人工智能5 分钟前
[特殊字符] 第17课:滑动窗口最大值
数据结构·算法·数据库架构·图论·bfs·滑动窗口最大值
XiYang-DING15 分钟前
【LeetCode】232. 用栈实现队列
算法·leetcode·职场和发展
人道领域16 分钟前
【LeetCode刷题日记】142.环形链表Ⅱ
算法·leetcode·链表
2301_8227032024 分钟前
开源鸿蒙跨平台Flutter开发:基因序列比对基础:Needleman-Wunsch 算法的 Dart 实现
算法·flutter·开源·鸿蒙
Book思议-25 分钟前
【数据结构】「树」专题:树、森林与二叉树遍历之间的关系+408真题
数据结构·算法·二叉树··森林
Fcy64835 分钟前
算法基础详解(4)双指针算法
开发语言·算法·双指针
zk_ken35 分钟前
优化图像拼接算法思路
算法
xwz小王子38 分钟前
Nature Communications从结构到功能:基于Kresling折纸的多模态微型机器人设计
人工智能·算法·机器人
luj_176838 分钟前
从R语言想起的,。。。
服务器·c语言·开发语言·经验分享·算法