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;
}
相关推荐
微风欲寻竹影几秒前
Java数据结构——二叉树相关OJ题目详解
java·数据结构
微风欲寻竹影2 分钟前
Java数据结构——二叉树(Binary Tree)详解
java·数据结构·算法
想吃火锅10052 分钟前
【leetcode】3.无重复字符的最长字串js版
算法·leetcode·职场和发展
smith成长之旅6 分钟前
08 | Mem0 框架分析: BM25 的 Sigmoid 归一化
数据库·python·算法
dongf20197 分钟前
R 语言随机森林算法
算法·随机森林·r语言
AZaLEan__21 分钟前
图论:拓扑排序
算法·深度优先
悠仁さん21 分钟前
数据结构 排序
数据结构·算法·排序算法
阿文的代码库24 分钟前
机器学习之精确率和召回率的关系
人工智能·算法·机器学习
咸鱼翻身小阿橙32 分钟前
高斯模糊降噪/磨皮算法降噪图像
前端·opencv·算法·webpack·c#
代码中介商38 分钟前
数据结构进阶(五):最短路径——Dijkstra 与 Floyd 算法
数据结构·算法