C language implementation of dynamic sequence table

The main operation of the pointer in the structure is to open up the memory of the pointer in the init function. The checkCapacity function checks it before each insertion. If there is no free space, it will open up twice the current maximum capacity. Other operations are not much different from static sequence tables.

seqList.h

#pragma once
#define DataType int

typedef struct seqList {
    DataType * array;
    int size;           //当前元素数
    int capicity;         //最大容量
}*PSeqListD, seqListD;

void SeqListDInit(PSeqListD ps, int capacity);
void SeqListDPushBack(PSeqListD ps, DataType data);
void SeqListDPopBack(PSeqListD ps);
void SeqListDInsert(PSeqListD ps, int pos, DataType data);
void SeqListDErase(PSeqListD ps, int pos);

// 获取元素个数 
int SeqListDSize(PSeqListD ps);
// 获取顺序表的容量 
int SeqListDCapacity(PSeqListD ps);
int SeqListDEmpty(PSeqListD ps);
// 将顺序表中的元素清空 注意:不改变顺序表空间的大小 
void SeqListDClear(PSeqListD ps);
void SeqListDDestroy(PSeqListD * ps);
// 对顺序表进行增容 
int CheckCapacity(PSeqListD ps);

test.c

#include "seqListD.h"

int main()
{
    seqListD seq;
    SeqListDInit(&seq, 5);
    SeqListDPushBack(&seq, 1);
    SeqListDInsert(&seq, 0, 2);
    SeqListDInsert(&seq, 0, 3);
    SeqListDInsert(&seq, 3, 4);
    SeqListDInsert(&seq, 2, 7);
    SeqListDPushBack(&seq, 9);
    SeqListDErase(&seq, 2);
}

seqList.c

#include "seqListD.h"
#include <stdio.h>
#include <malloc.h>
void SeqListDInit(PSeqListD ps, int capacity)
{
    if (NULL == ps || capacity < 0) {
        return;
    }
    ps->size = 0;
    ps->capicity = capacity;
    ps->array = (DataType*)malloc(sizeof(DataType) * capacity);
    if (ps->array == NULL) {
        printf("分配内存失败");
    }
}

void SeqListDDestroy(PSeqListD ps)
{
    if (NULL == ps) {
        return;
    }
    free(ps->array);
    ps->array = NULL;
}
void SeqListDPushBack(PSeqListD ps, DataType data)
{
    if (NULL == ps || NULL == ps->array) {
        return;
    }
    if (CheckCapacity(ps)) {
        ps->array[ps->size] = data;
        ps->size++;
    }
}
void SeqListDPopBack(PSeqListD ps)
{
    if (NULL == ps || NULL == ps->array) {
        return;
    }
    if (ps->size == 0) {
        printf("无可删除的");
        return;
    }
    ps->size--;
}
void SeqListDInsert(PSeqListD ps, int pos, DataType data)
{
    if (NULL == ps || NULL == ps->array) {
        return;
    }
    if (CheckCapacity(ps)) {
        int i = 0;
        for (i = ps->size; i > pos; i--) {
            ps->array[i] = ps->array[i - 1];
        }
        ps->array[pos] = data;
        ps->size++;
    }
}
void SeqListDErase(PSeqListD ps, int pos) {

    if (NULL == ps || NULL == ps->array) {
        return;
    }
    if (ps->size == 0) {
        printf("没有元素可以删\n");
        return;
    }
    int i = 0;
    for (i = pos; i < ps->size - 1; i++) {
        ps->array[i] = ps->array[i + 1];
    }
    ps->size--;
}

// 对顺序表进行增容 
int CheckCapacity(PSeqListD ps)
{
    if (NULL == ps) {
        return 0;
    }
    //防止新空间开辟失败旧空间丢失的情况
    if (ps->size == ps->capicity) {
        DataType * newBase = (PSeqListD)realloc(ps->array, 2 * ps->capicity * sizeof(DataType));
        if (newBase == NULL) {
            printf("扩容失败");
            return 0;
        }
        ps->array = newBase;
        ps->capicity *= 2;
    }
    return 1;
}
// 获取元素个数 
int SeqListDSize(PSeqListD ps)
{
    if (NULL == ps) {
        return -1;
    }
    return ps->size;
}

// 获取顺序表的容量 
int SeqListDCapacity(PSeqListD ps)
{
    if (NULL == ps) {
        return -1;
    }
    return ps->capicity;
}

int SeqListDEmpty(PSeqListD ps)
{
    if (NULL == ps) {
        return -1;
    }
    return ps->size ? 1 : 0;
}

// 将顺序表中的元素清空 注意:不改变顺序表空间的大小 
void SeqListDClear(PSeqListD ps)
{
    if (NULL == ps) {
        return;
    }
    ps->size = 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325899380&siteId=291194637