顺序表的模块化代码实现

思维导图


模块化代码

头文件中

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

//typedef int SLDataType;
//#define N 100000
//
 静态顺序表 -- 开少了不够用 开多了浪费
//struct SeqList
//{
//    SLDataType a[N];
//    int size;
//};

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);

void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
int SLFind(SL* ps, SLDataType x);

源文件中

SeqList.c

#pragma once
#include"SeqList.h"

void SLInit(SL* ps)
{
    assert(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)
{
    assert(ps);

    free(ps->a);
    ps->a = NULL;
    ps->capacity = ps->size = 0;
}

void SLPrint(SL* ps)
{
    assert(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)
{
    //assert(ps);

     扩容
    //SLCheckCapacity(ps);

    ps->a[ps->size] = x;
    ps->size++;
    //ps->a[ps->size++] = x;

    SLInsert(ps, ps->size, x);
}

void SLPopBack(SL* ps)
{
    //assert(ps);

     暴力检查
    //assert(ps->size > 0);

     温柔的检查
    if (ps->size == 0)
    //    //return;

    ps->a[ps->size - 1] = 0;
    //ps->size--;

    SLErase(ps, ps->size - 1);
}

void SLPushFront(SL* ps, SLDataType x)
{
    /*assert(ps);

    SLCheckCapacity(ps);

    int end = ps->size - 1;
    while (end >= 0)
    {
        ps->a[end + 1] = ps->a[end];
        --end;
    }

    ps->a[0] = x;
    ps->size++;*/

    SLInsert(ps, 0, x);
}

void SLPopFront(SL* ps)
{
    //assert(ps);
    //assert(ps->size > 0);

    //int begin = 1;
    //while (begin < ps->size)
    //{
    //    ps->a[begin - 1] = ps->a[begin];
    //    ++begin;
    //}

    //ps->size--;

    SLErase(ps, 0);
}

void SLInsert(SL* ps, int pos, SLDataType x)
{
    assert(ps);
    assert(pos >= 0 && pos <= ps->size);

    SLCheckCapacity(ps);

    int end = ps->size - 1;
    while (end >= pos)
    {
        ps->a[end + 1] = ps->a[end];
        --end;
    }

    ps->a[pos] = x;
    ps->size++;
}

void SLErase(SL* ps, int pos)
{
    assert(ps);
    assert(pos >= 0 && pos < ps->size);

    int begin = pos + 1;
    while (begin < ps->size)
    {
        ps->a[begin - 1] = ps->a[begin];
        ++begin;
    }

    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;
}

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);

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

    SLPushBack(&s, 10);
    SLPushBack(&s, 20);
    SLPrint(&s);

    SLDestroy(&s);
}

int main()
{
    TestSeqList1();

    return 0;
}

运行结果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值