C语言动态实现顺序表

C语言动态实现顺序表的思想就是将结构体的数组设置为指针,通过malloc和realloc对其增容,其他操作可相似与普通顺序表
SeqList.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int Datatypedef;
typedef struct Seq
{
    Datatypedef* pdata;
    int size;
    int capacity;
}Seq,*pSeq;


void Init(pSeq a);
int Cheakcapacity(pSeq a);
void Pushback(pSeq a, Datatypedef data);
void Popback(pSeq a);
void Inest(pSeq a,int pos, Datatypedef data);
void Erase(pSeq a, int pos);
void Find(pSeq a, Datatypedef data);
void Clear(pSeq a);
void FreeSeq(pSeq a);
void PrintSeq(pSeq a);
void LookSeq(pSeq a);

SeqList.c

#include"SeqlistD.h"




void Init(pSeq a)
{
    assert(a);
    a->capacity = 5;
    a->size = 0;
    a->pdata = (Datatypedef*)malloc(sizeof(Datatypedef)*a->capacity);
    if (NULL == a->pdata)
    {
        printf("fail to malloc\n");
        return;
    }
}
int Cheakcapacity(pSeq a)
{
    assert(a);
    int newcapacity = a->capacity + 5;
    if (a->capacity == a->size)
    {
        a->pdata = (Datatypedef*)realloc(a->pdata, newcapacity*sizeof(Datatypedef));
        if (NULL == a->pdata)
        {
            printf("fail to realloc\n");
            return -1;
        }
        a->capacity = newcapacity;
    }
    return 1;
}
void Pushback(pSeq a, Datatypedef data)
{
    assert(a);
    int ret = 0;
    ret = Cheakcapacity(a);
    if (ret == 1)
    {
        a->pdata[a->size++] = data;
    }
}
void Popback(pSeq a)
{
    assert(a);
    if (a->size == 0)
    {
        printf("fail to pop\n");
        return;
    }
    a->size--;
}
void Inest(pSeq a, int pos,Datatypedef data)
{
    assert(a);
    int ret = 0,i=0;
    if (pos <0|| pos > a->size)
    {
        printf("error pos\n");
        return;
    }
    ret = Cheakcapacity(a);
    if (ret == 1)
    {
        for (i = a->size; i >= pos; i--)
        {
            a->pdata[i] = a->pdata[i-1];
        }
        a->pdata[pos] = data;
        a->size++;
    }

}
void LookSeq(pSeq a)
{
    assert(a);
    printf("size:%d  capacity:%d\n", a->size, a->capacity);
}
void Erase(pSeq a, int pos)
{
    assert(a);
    int ret = 0, i = 0;
    if (pos <0 || pos > a->size)
    {
        printf("error erase\n");
        return;
    }
    for (i = pos; i < a->size-1; i++)
    {
        a->pdata[i] = a->pdata[i + 1];
    }
    a->size--;
}
void Clear(pSeq a)
{
    assert(a);
    a->size = 0;
}






void PrintSeq(pSeq a)
{
    assert(a);
    int i = 0;
    for (i = 0; i < a->size; i++)
    {
        printf("%d  ", a->pdata[i]);
    }
    printf("\n");
}
void FreeSeq(pSeq a)
{
    assert(a);
    free(a->pdata);
    a->size = 0;
    a->capacity = 0;
}
void TestSeq()
{
    Seq a;
    Init(&a);
    Pushback(&a, 1);
    Pushback(&a, 2);
    Pushback(&a, 3);
    Pushback(&a, 4);
    Pushback(&a, 5);
    LookSeq(&a);
    PrintSeq(&a);
    Pushback(&a, 6);
    Inest(&a, 0, 0);
    Erase(&a, 0);
    LookSeq(&a);
    PrintSeq(&a);
    FreeSeq(&a);
}

test.c

“`

include”SeqlistD.h”

int main()
{
TestSeq();
}
“`数据结构的学习就是需要大家多加练习,练习可以使大家收悉一门语言,而且可以掌握结构的思想,由于是刚开始学习数据结构,如果有什么问题欢迎大家提出,谢谢

猜你喜欢

转载自blog.csdn.net/weixin_40909099/article/details/79912197