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;
}
相关推荐
Hgfdsaqwr2 小时前
实时控制系统优化
开发语言·c++·算法
2301_821369612 小时前
嵌入式实时C++编程
开发语言·c++·算法
sjjhd6522 小时前
多核并行计算优化
开发语言·c++·算法
weixin_395448913 小时前
main.c_cursor_0130
前端·网络·算法
半壶清水3 小时前
[软考网规考点笔记]-操作系统核心知识及历年真题解析
网络·网络协议·算法
Tansmjs3 小时前
实时数据可视化库
开发语言·c++·算法
WBluuue3 小时前
Codeforces 1075 Div2(ABC1C2D1D2)
c++·算法
2401_838472514 小时前
C++模拟器开发实践
开发语言·c++·算法
s1hiyu4 小时前
实时控制系统验证
开发语言·c++·算法