数据结构 - 顺序表(C语言实现)

  顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存 储。在数组上完成数据的增删查改。

目录

顺序表的结构

初始化

检查容量空间

销毁

打印

查找

改数据

在pos位置插入x

删除pos位置的值

头增

尾增

头删

尾删


顺序表的结构

typedef int SLDateType;
typedef struct SeqList
{
	SLDateType* _a;   //指向动态开辟的数组
	size_t _size;      //有效元素个数
	size_t _capacity;  //数组容量空间
}SeqList;

初始化

void SeqListInit(SeqList* ps) 
{
        assert(ps);
        ps->_a = NULL;
        ps->_size = 0;
        ps->_capacity = 0;
}

检查容量空间

void SeqListCheckCapacity(SeqList* ps)  
{
        assert(ps);
        if (ps->_size == ps->_capacity)
        {
               int newCapacity = ps->_capacity == 0 ? 4 : ps->_capacity * 2; //刚开始4,后面每次*2
               SLDateType* tmp = (SLDateType*)realloc(ps->_a, newCapacity *  sizeof(SLDateType));
               if (tmp == NULL)
               {
                       printf("realloc : fail\n");
                       exit(-1);
               }
               //更新
               ps->_a = tmp;
               tmp = NULL;
               ps->_capacity = newCapacity;
        }
}

销毁

void SeqListDestroy(SeqList* ps)
{
        assert(ps);
        if (ps->_a)
        {
               free(ps->_a);
               ps->_a = NULL;
               ps->_size = 0;
               ps->_capacity = 0;
               printf("销毁成功\n");
        }
}

打印

void SeqListPrint(SeqList* ps) 
{
        assert(ps);
        for (int i = 0; i < ps->_size; ++i)
        {
               printf("%d ", ps->_a[i]);
        }
        printf("\n");
}

查找

int SeqListFind(SeqList* ps, SLDateType x)
{
        assert(ps);
        for (int i = 0; i < ps->_size; ++i)
        {
               if (ps->_a[i] == x)
                       return i;
        }
        return -1;
}

改数据

void SeqListModify(SeqList* ps, int pos, SLDateType x) //参数pos需要先查找
{
        assert(ps);
        assert(pos >= 0 && pos < ps->_size);
        ps->_a[pos] = x;
}

在pos位置插入x

void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
        assert(ps);
        assert(pos >= 0 && pos <= ps->_size);
        SeqListCheckCapacity(ps);//检查容量
        int end = ps->_size - 1;
        while (end >= pos && end >= 0)    //数组在尾部+1个位置
        {
               ps->_a[end + 1] = ps->_a[end];
               --end;
        }
        ps->_a[pos] = x;
        ps->_size++;
}

删除pos位置的值

void SeqListErase(SeqList* ps, size_t pos)
{
        assert(ps);
        assert(pos >= 0 && pos < ps->_size);
        if (pos == ps->_size - 1)
        {
               ps->_size--;
               return;
        }
        else
        {
               int begin = pos + 1;  //防止pos=0时的越界访问
               while (begin < ps->_size)
               {
                       ps->_a[begin - 1] = ps->_a[begin];
                       begin++;
               }
               ps->_size--;
        }
}

头增

void SeqListPushFront(SeqList* ps, SLDateType x)  //头增
{
        assert(ps);
        SeqListInsert(ps, 0, x); //重调用

        //SeqListCheckCapacity(ps);//检查容量
        //if (ps->_size > 0) //挪动头数据
        //{
        //      int end = ps->_size - 1;
        //      while (end >= 0)    //数组在尾部+1个位置
        //      {
        //             ps->_a[end + 1] = ps->_a[end];
        //             --end;
        //      }
        //}
        //ps->_sizeof++;
        //ps->_a[0]=x;
}

尾增

void SeqListPushBack(SeqList* ps, SLDateType x)  //尾增
{
        assert(ps);
        SeqListInsert(ps, ps->_size, x); //重调用
}

头删

void SeqListPopFront(SeqList* ps)  //头删
{
        assert(ps);
        assert(ps->_size > 0);
        SeqListErase(ps, 0); //重调用
}

尾删

void SeqListPopBack(SeqList* ps)  //尾删
{
        assert(ps);
        assert(ps->_size > 0);
        ps->_size--; //删除有效数据量
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值