栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构 - 顺序表(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 : failn");
                       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--; //删除有效数据量
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/958157.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号