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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值