C语言中数据结构——顺序表

🐶博主主页:@ᰔᩚ. 一怀明月ꦿ 

❤️‍🔥专栏系列:线性代数C初学者入门训练题解CC的使用文章「初学」C++

🔥座右铭:“不要等到什么都没有了,才下定决心去做”

🚀🚀🚀大家觉不错的话,就恳求大家点点关注,点点小爱心,指点指点🚀🚀🚀

目录

 

🐰顺序表

🏡顺序表的定义

🏡顺序表的初始化

🏡顺序表空间的检查

🏡顺序表中指定位置插入数据

🏡顺序表中指定位置删除数据

🏡顺序表中的头插数据

🏡顺序表中的尾插数据

🏡顺序表中的头删数据

🏡顺序表中的尾删数据

🏡顺序表中查找数据

🏡顺序表中改动数据

🏡顺序表中的打印数据

🏡顺序表中的销毁数据

🏡顺序表中的源码

🌸main文件

🌸头文件test.h

🌸test.c文件


🐰顺序表

🏡顺序表的定义

有两种顺序表的定义,一种是静态的,一种是动态的

1.静态顺序表的定义

静态顺序表
1.空间是固定的,没有办法存储超过空间的数据
2.如果空间开辟大了,浪费空间
不推荐使用静态顺序表,没有实际用途
#define N 10
typedef int SLDatatype;

struct SeqList
{
    int a[N];
    int size;
};

2.动态顺序表的定义

动态定义的顺序表,有效规避了静态的不足
typedef int SLDatatype;
typedef struct SeqList
{
    SLDatatype* a;//有效数据
    SLDatatype size;//存储的有效数据个数
    SLDatatype capacity;//存放数据的最大容量
}SL;

🏡顺序表的初始化

初始时,顺序表动态开辟了4个空间,有效数据的个数为0,最大容量为4

void SLInit(SL* psl)
{
    assert(psl);
    psl->a=(SLDatatype*)malloc(sizeof(SLDatatype)*4);//初始化时开辟4个SLDatatype类型的空间给a
    if(psl->a==NULL)
    {
        perror("malloc fail");
        return ;
    }
    psl->size=0;
    psl->capacity=4;
}

🏡顺序表空间的检查

我们在给顺序表插入数据之前,我们应该检查一下顺序表的空间是否充足,如果充足我们就插入数据,如果不充足,就开辟更大的空间,然后再进行插入数据

void SLCheckCapacity(SL* psl)
{
    assert(psl);
    if(psl->size==psl->capacity)
    {
        SLDatatype* tmp=(SLDatatype*)realloc(psl->a,sizeof(SLDatatype)*psl->capacity*2);//在原来的的空间,扩为原来空间的二倍
        if(tmp==NULL)
        {
            perror("realloc fail");
            return ;
        }
        else
        {
            psl->a=tmp;
            psl->capacity=psl->capacity*2;
        }
    }
}

🏡顺序表中指定位置插入数据

我们可以在自己指定的位置插入数据,这里需要注意的是,我们指定的位置必须合法,也就说,只能从头到尾之间插入数据,且尾也可以插入数据

void SLInsert(SL* psl,int pos,SLDatatype x)
{
    assert(psl);
    assert(pos>=0&&pos<=psl->size);//判断插入位置是否符合合法
    SLCheckCapacity(psl);
    int end=psl->size-1;
    while(end>=pos)
    {
        psl->a[end+1]=psl->a[end];
        end--;
    }
    psl->a[pos]=x;
    psl->size++;
}

🏡顺序表中指定位置删除数据

我们可以在自己指定的位置删除数据,这里需要注意的是,我们指定的位置必须合法,也就说,只能从头到尾之间删除数据

void SLEarse(SL* psl,int pos)
{
    assert(psl);
    assert(pos>=0&&pos<psl->size);//判断插入位置是否符合合法
    int start=pos+1;
    while(start<psl->size)
    {
        psl->a[start-1]=psl->a[start];
        start++;
    }
    psl->size--;
}

🏡顺序表中的头插数据

顺序表中的头插数据有两种方法

1.第一种方法

这种方法就是将最后的数据移动到后一个位置,然后倒数第二个数据往最后一个位置移动,依此类推,直到第一个数据移到第二个位置,然后在第一个位置插入数据

void SLPushFront(SL* psl,SLDatatype x)
{
    assert(psl);
    SLCheckCapacity(psl);
    //从后往前移动数据
    int end=psl->size-1;
    while(end>=0)
    {
        psl->a[end+1]=psl->a[end];
        end--;
    }
    psl->a[0]=x;
    psl->size++;
}

1.第二种方法

这种方法复用了顺序表中指定位置插入数据

void SLPushFront(SL* psl,SLDatatype x)
{
    assert(psl);
    SLInsert(psl,0,x);
}

🏡顺序表中的尾插数据

顺序表中的尾插数据有两种方法

1.第一种

直接在最后一个数据的位置后面插入数据

void SLPushBack(SL* psl,SLDatatype x)
{
    assert(psl);
    SLCheckCapacity(psl);
    psl->a[psl->size]=x;
    psl->size++;
}

2.第二种

这种方法复用了顺序表中指定位置插入数据

void SLPushBack(SL* psl,SLDatatype x)
{
    assert(psl);
    SLInsert(psl,psl->size,x);
}

🏡顺序表中的头删数据

顺序表中的头删数据有两种方法

1.第一种:

将第二个数据放到第一个数据的位置,把第三个的数据放到第二个数据最开始的位置,以此类推,直到最后一个数据放到倒数第二个数据最开始的位置

void SLPopFront(SL* psl)
{
    assert(psl);
    assert(psl->size>0);
    //从前往后移动
    int start=0;
    while(start<psl->size-1)
    {
        psl->a[start]=psl->a[start+1];
        start++;
    }
    psl->size--;
}

2.第二种:

这种方法复用了顺序表中指定位置删除数据

void SLPopFront(SL* psl)
{
    assert(psl);
    SLEarse(psl,0);
}

🏡顺序表中的尾删数据

顺序表中的头删数据有两种方法

1.第一种

void SLPophBack(SL* psl)
{
    assert(psl);
    //(1)if是温柔处理顺序表为空
    if(psl->size==0)
    {
        printf("顺序表数据为空,不能尾删\n");
        return ;
    }
    else
    {
        psl->size--;
    }
    //(2)暴力解法
    assert(psl->size>0);
    psl->size--;
}

2.第二种

这种方法复用了顺序表中指定位置删除数据

void SLPophBack(SL* psl)
{
    assert(psl);
    SLEarse(psl,psl->a[psl->size-1]);
}

🏡顺序表中查找数据

如果找到数据我们就返回它的下标位置,如果找不到就返回-1,这里这种算法有一点缺陷,就是如果数据中有重复的数据,则只会返回这个数据第一次出现的位置

int SLFind(SL* psl,SLDatatype x)
{
    assert(psl);
    for(int i=0;i<psl->size;i++)
    {
        if(psl->a[i]==x)
        {
            return i;
        }
    }
    return -1;
}

🏡顺序表中改动数据

直接在想要改变数据的位置,直接赋新值,但是要注意想要改变数据的位置是合法的

void SLModify(SL* psl,int pos,SLDatatype x)
{
    assert(psl);
    assert(pos>=0&&pos<psl->size);
    psl->a[pos]=x;
}

🏡顺序表中的打印数据

依次打印出顺序表中存储的数据

void SLPrint(SL* psl)
{
    assert(psl);
    for(int i=0;i<psl->size;i++)
    {
        printf("%d ",psl->a[i]);
    }
    printf("\n");
}

🏡顺序表中的销毁数据

将动态分配的空间归还给系统,将数据个数置为0,最大容量置为0

void SLDestroy(SL* psl)
{
    assert(psl);
    free(psl->a);
    psl->a=NULL;
    psl->size=0;
    psl->capacity=0;
}

🏡顺序表中的源码

为了更形象观察顺序表,这里使用了菜单,如果想要方便调试,建议大家不要使用菜单

🌸main文件

//顺序表
//顺序表的本质就是一个数组
//链表不支持二分查找,数组可以

//顺序表的增、删、查、改

#include"test.h"
void menu(void)
{
    printf("****************顺序表****************\n");
    printf("        1.头插数据      2.尾插数据      \n");
    printf("        3.头删数据      4.尾删数据      \n");
    printf("        5.自定义插数据   6.自定义删数据   \n");
    printf("        7.查找数据      8.修改数据      \n");
    printf("        9.打印数据      x.销毁数据      \n");
    printf("        e.退出                        \n");
    printf("*************************************\n");
}
void test(void)
{
    SL s1;
    SLInit(&s1);
    char input=0;
    do
    {
        menu();
        printf("请选择:>");
        scanf("%c",&input);
        if(input=='1')
        {
            SLDatatype x=0;
            printf("请输入插入的数据\n");
            scanf("%d",&x);
            SLPushFront(&s1,x);
            printf("数据完成插入^_^\n");
        }
        else if(input=='2')
        {
            SLDatatype x=0;
            printf("请输入插入的数据\n");
            scanf("%d",&x);
            SLPushFront(&s1,x);
            printf("数据完成插入^_^\n");
        }
        else if(input=='3')
        {
            SLPophBack(&s1);
            printf("数据完成删除@_@\n");
        }
        else if(input=='4')
        {
            SLPophBack(&s1);
            printf("数据完成删除@_@\n");
        }
        else if(input=='5')
        {
            int pos=0;
            int x=0;
            printf("请输入你想要插入数据的位置\n");
            scanf("%d",&pos);
            printf("请输入插入的数据\n");
            scanf("%d",&x);
            SLInsert(&s1,pos,x);
            printf("数据完成插入^_^\n");
        }
        else if(input=='6')
        {
            int pos=0;
            printf("请输入你想要删除数据的位置\n");
            scanf("%d",&pos);
            SLEarse(&s1,pos);
            printf("数据完成删除@_@\n");
        }
        else if(input=='7')
        {
            int x=0;
            printf("请输入查找的数据\n");
            scanf("%d",&x);
            int num=SLFind(&s1,x);
            if(num!=-1)
            printf("数据的位置为%d\n",num);
            else
                printf("顺序表里没有此数据\n");
        }
        else if(input=='8')
        {
            int pos=0;
            printf("请输入你想要修改数据的位置\n");
            scanf("%d",&pos);
            int x=0;
            printf("请输入修改的数据\n");
            scanf("%d",&x);
            SLModify(&s1,pos,x);
            printf("修改数据成功O_o\n");
        }
        else if(input=='9')
        {
            SLPrint(&s1);
            printf("打印完成...\n");
        }
        else if(input=='x')
        {
            SLDestroy(&s1);
            printf("销毁数据成功...\n");
        }
        else if(input=='e')
        {
            printf("退出顺序表...\n");
        }
        else
        {
            printf("无此选项,请重新选择\n");
        }
        getchar();
    }while(input!='e');
}
int main()
{
    test();
    return 0;
}

🌸头文件test.h

#ifndef test_h
#define test_h
#include <stdio.h>
#include<stdlib.h>
#endif /* test_h */

#include<assert.h>

//静态顺序表
//1.空间是固定的,没有办法存储超过空间的数据
//2.如果空间开辟大了,浪费空间
//不推荐使用静态顺序表,没有实际用途
//#define N 10
//typedef int SLDatatype;
//
//struct SeqList
//{
//    int a[N];
//    int size;
//};



typedef int SLDatatype;
typedef struct SeqList
{
    SLDatatype* a;
    SLDatatype size;//存储的有效数据个数
    SLDatatype capacity;//容量
}SL;


//STL的命名风格(C++)
//顺序表的初始化
void SLInit(SL* psl);
//打印顺序表
void SLPrint(SL* psl);
//销毁顺序表
void SLDestroy(SL* psl);
//尾插数据
void SLPushBack(SL* psl,SLDatatype x);
//头插数据
void SLPushFront(SL* psl,SLDatatype x);
//尾删数据
void SLPophBack(SL* psl);
//头删数据
void SLPopFront(SL* psl);
//在pos位置插入一个数据
void SLInsert(SL* psl,int pos,SLDatatype x);
//在pos位置删除一个数据
void SLEarse(SL* psl,int pos);
//查找数据
//找到了返回下标,没找到返回-1
int SLFind(SL* psl,SLDatatype x);
//修改指定位置的数据
void SLModify(SL* psl,int pos,SLDatatype x);

🌸test.c文件

#include"test.h"
void SLInit(SL* psl)
{
    assert(psl);
    psl->a=(SLDatatype*)malloc(sizeof(SLDatatype)*4);//初始化时开辟4个SLDatatype类型的空间给a
    if(psl->a==NULL)
    {
        perror("malloc fail");
        return ;
    }
    psl->size=0;
    psl->capacity=4;
}

void SLDestroy(SL* psl)
{
    assert(psl);
    free(psl->a);
    psl->a=NULL;
    psl->size=0;
    psl->capacity=0;
}
void SLPrint(SL* psl)
{
    assert(psl);
    for(int i=0;i<psl->size;i++)
    {
        printf("%d ",psl->a[i]);
    }
    printf("\n");
}
//插入的时候要去判断容量是否充足
//可以写一个检查容量是否充足的函数
//如果空间不足,就需要扩容

void SLCheckCapacity(SL* psl)
{
    assert(psl);
    if(psl->size==psl->capacity)
    {
        SLDatatype* tmp=(SLDatatype*)realloc(psl->a,sizeof(SLDatatype)*psl->capacity*2);//在原来的的空间,扩为原来空间的二倍
        if(tmp==NULL)
        {
            perror("realloc fail");
            return ;
        }
        else
        {
            psl->a=tmp;
            psl->capacity=psl->capacity*2;
        }
    }
}
//尾插数据
//尾插数据的原理:在最后一个数据后面再添加一个数据
void SLPushBack(SL* psl,SLDatatype x)
{
    assert(psl);
//    SLCheckCapacity(psl);
//    psl->a[psl->size]=x;
//    psl->size++;
    
    SLInsert(psl,psl->size,x);
}
//头插数据
void SLPushFront(SL* psl,SLDatatype x)
{
    assert(psl);
    SLCheckCapacity(psl);
    //从后往前移动数据
    int end=psl->size-1;
    while(end>=0)
    {
        psl->a[end+1]=psl->a[end];
        end--;
    }
    psl->a[0]=x;
    psl->size++;
   // SLInsert(psl,0,x);
}
//尾删数据
void SLPophBack(SL* psl)
{
    assert(psl);
//    if是温柔处理顺序表为空
//    if(psl->size==0)
//    {
//        printf("顺序表数据为空,不能尾删\n");
//        return ;
//    }
//    else
//    {
//        psl->size--;
//    }
//    暴力解法
//    assert(psl->size>0);
//    psl->size--;
    
    SLEarse(psl,psl->a[psl->size-1]);
}
//头删数据
void SLPopFront(SL* psl)
{
    assert(psl);
    //方法一:
//    assert(psl->size>0);
//    //从前往后移动
//    int start=0;
//    while(start<psl->size-1)
//    {
//        psl->a[start]=psl->a[start+1];
//        start++;
//    }
//    psl->size--;
    
    //方法二:
    SLEarse(psl,0);
}

void SLInsert(SL* psl,int pos,SLDatatype x)
{
    assert(psl);
    assert(pos>=0&&pos<=psl->size);//判断插入位置是否符合合法
    SLCheckCapacity(psl);
    int end=psl->size-1;
    while(end>=pos)
    {
        psl->a[end+1]=psl->a[end];
        end--;
    }
    psl->a[pos]=x;
    psl->size++;
}
void SLEarse(SL* psl,int pos)
{
    assert(psl);
    assert(pos>=0&&pos<psl->size);//判断插入位置是否符合合法
    int start=pos+1;
    while(start<psl->size)
    {
        psl->a[start-1]=psl->a[start];
        start++;
    }
    psl->size--;
}
int SLFind(SL* psl,SLDatatype x)
{
    assert(psl);
    for(int i=0;i<psl->size;i++)
    {
        if(psl->a[i]==x)
        {
            return i;
        }
    }
    return -1;
}
void SLModify(SL* psl,int pos,SLDatatype x)
{
    assert(psl);
    assert(pos>=0&&pos<psl->size);
    psl->a[pos]=x;
}

🌸🌸🌸如果大家还有不懂或者建议都可以发在评论区,我们共同探讨,共同学习,共同进步。谢谢大家! 🌸🌸🌸  

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用C语言创建顺序的基本步骤: 1. 定义顺序结构体 ```c #define MAXSIZE 100 // 定义顺序的最大长度 typedef struct { int data[MAXSIZE]; // 存储元素的数组 int length; // 当前顺序的长度 } SqList; // 定义顺序类型 ``` 2. 初始化顺序 ```c void InitList(SqList *L) { int i; for (i = 0; i < MAXSIZE; i++) { L->data[i] = 0; // 将元素全部置为0,顺序为空 } L->length = 0; // 初始化顺序长度为0 } ``` 3. 插入元素 ```c int ListInsert(SqList *L, int pos, int elem) { int i; if (pos < 1 || pos > L->length + 1) { // 判断插入位置是否合法 return 0; } if (L->length >= MAXSIZE) { // 判断顺序是否已满 return 0; } for (i = L->length; i >= pos; i--) { // 将插入位置后的元素向后移动一个位置 L->data[i] = L->data[i - 1]; } L->data[pos - 1] = elem; // 插入新元素 L->length++; // 顺序长度加1 return 1; } ``` 4. 删除元素 ```c int ListDelete(SqList *L, int pos) { int i; if (pos < 1 || pos > L->length) { // 判断删除位置是否合法 return 0; } for (i = pos - 1; i < L->length - 1; i++) { // 将删除位置后的元素向前移动一个位置 L->data[i] = L->data[i + 1]; } L->length--; // 顺序长度减1 return 1; } ``` 5. 获取元素 ```c int GetElem(SqList *L, int pos) { if (pos < 1 || pos > L->length) { // 判断获取位置是否合法 return 0; } return L->data[pos - 1]; // 返回指定位置的元素值 } ``` 6. 遍历顺序 ```c void TraverseList(SqList *L) { int i; for (i = 0; i < L->length; i++) { printf("%d ", L->data[i]); // 输出每个元素的值 } printf("\n"); } ``` 7. 完整代码 ```c #include <stdio.h> #define MAXSIZE 100 // 定义顺序的最大长度 typedef struct { int data[MAXSIZE]; // 存储元素的数组 int length; // 当前顺序的长度 } SqList; // 定义顺序类型 void InitList(SqList *L) { int i; for (i = 0; i < MAXSIZE; i++) { L->data[i] = 0; // 将元素全部置为0,顺序为空 } L->length = 0; // 初始化顺序长度为0 } int ListInsert(SqList *L, int pos, int elem) { int i; if (pos < 1 || pos > L->length + 1) { // 判断插入位置是否合法 return 0; } if (L->length >= MAXSIZE) { // 判断顺序是否已满 return 0; } for (i = L->length; i >= pos; i--) { // 将插入位置后的元素向后移动一个位置 L->data[i] = L->data[i - 1]; } L->data[pos - 1] = elem; // 插入新元素 L->length++; // 顺序长度加1 return 1; } int ListDelete(SqList *L, int pos) { int i; if (pos < 1 || pos > L->length) { // 判断删除位置是否合法 return 0; } for (i = pos - 1; i < L->length - 1; i++) { // 将删除位置后的元素向前移动一个位置 L->data[i] = L->data[i + 1]; } L->length--; // 顺序长度减1 return 1; } int GetElem(SqList *L, int pos) { if (pos < 1 || pos > L->length) { // 判断获取位置是否合法 return 0; } return L->data[pos - 1]; // 返回指定位置的元素值 } void TraverseList(SqList *L) { int i; for (i = 0; i < L->length; i++) { printf("%d ", L->data[i]); // 输出每个元素的值 } printf("\n"); } int main() { SqList list; InitList(&list); ListInsert(&list, 1, 10); ListInsert(&list, 2, 20); ListInsert(&list, 3, 30); TraverseList(&list); ListDelete(&list, 2); TraverseList(&list); printf("%d\n", GetElem(&list, 2)); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值