数据结构基础——线性表之顺序表的插入删除查找操作(附C语言代码)

目录

前言

基本操作

插入

 删除

 按值查找

总结 


前言

本文主要记录自己学习数据结构的过程与收获,欢迎各位批评指正。

在这放个上一节关于顺序表实现静态/动态分配的链接~http://t.csdn.cn/JPZvHhttp://t.csdn.cn/JPZvH

基本操作

        本文只介绍插入删除按值查找操作。

插入

        根据顺序表在一段地址连续的存储单元中依次存储数据元素的特点,当在次序 i 插入一个数据时,原来次序为 i ~length 元素(如果有的话)的次序均加1,即后移一位。顺序表当前长度 length 加1。

        需要注意的是,插入操作的前提是应当同时满足以下条件:

  1.   1 < i < length+1 ;       
  2.    length < Maxsize (即顺序表最大长度) ;     

 代码如下所示:

        首先调用库,用来支持bool类型。

#include <stdbool.h>

        按位序插入函数,为bool类型,可用来检验插入操作是否成功,方便后续调试。 

bool ListInSert(SqList *L,int loc,int num){   //loc为所要插入的位序
    if( loc< 1 || loc >L->length+1){          //判断i的合法性
        return false;
    }
    if(L->length > L->Maxsize){              //判断是否还有位置插入
        return false;
    }
    for(int i = L->length;i>=loc;i--){
        L->data[i] = L->data[i-1];
    }
    L->data[loc-1] = num;
    L->length++;
    return true;
}

        主函数如下:

          其中创建、初始化、增加表长在上一篇博客中已经提到,具体可转http://t.csdn.cn/JPZvH

int main(void)
{
    SqList L;       //创建一个顺序表L
    InitList(&L);   //初始化
    printf("The initial list:\n");
    for(int j =0;j<L.Maxsize;j++){
        printf("data[%d]=%d ",j,L.data[j]);
    }
    printf("\n");
    IncreaseSize(&L,2);         //增加顺序表长度
    printf("Increase the length of the list:\n");
    for(int j =0;j<L.Maxsize;j++){
        printf("data[%d]=%d ",j,L.data[j]);
    }

    //按位序插入
    ListInSert(&L,1,1);
    ListInSert(&L,2,2);
    ListInSert(&L,3,3);
    ListInSert(&L,4,5);
    printf("\n");                          //先插入4个数
    printf("Insert the result for the first time:\n");
    for(int j =0;j<L.length;j++){                   //  打印结果
        printf("data[%d]=%d ",j,L.data[j]);
    }
    if(ListInSert(&L,4,4)==0){      //插入4,并判断插入是否成功
        printf("\n");
        printf("Fail!!!");
        return 0;
    }
    printf("\n");
    printf("The second time inserts the result:\n");
    for(int j =0;j<L.length;j++){                //打印结果
        printf("data[%d]=%d ",j,L.data[j]);
    }

    return 0;
}

效果如下所示:         

 删除

         与插入操作相反,当删除次序为 i 一个数据时,原来次序为 i+1 ~length 元素(如果有的话)的次序均减1,即前移一位,顺序表当前长度 length 减1。

        需要注意的是,进行删除操作时,次序 i 应当 满足 1 < i < length 。

代码如下所示:

         与插入操作相同,首先调用库,用来支持bool类型。

#include <stdbool.h>

        按位序删除函数如下:

bool ListDelect(SqList *L,int loc ,int* e){  //loc为位序
    if( loc<1 || loc >L->length){
        printf("%d\n",loc);
        return 0;
    }
    *e=L->data[loc-1];
    for(int i = loc ; i < L->length;i++){   //loc后的所有元素

        L->data[i-1] = L->data[i ];        //前移一位

    }
    L->length--;
    return true;
}

        主函数与前文类似,添加了删除操作。

int main(void)
{
    SqList L;       //创建一个顺序表L
    InitList(&L);   //初始化
    printf("The initial list:\n");
    for(int j =0;j<L.Maxsize;j++){
        printf("data[%d]=%d ",j,L.data[j]);
    }
    printf("\n");
    IncreaseSize(&L,2);         //增加顺序表长度
    printf("Increase the length of the list:\n");
    for(int j =0;j<L.Maxsize;j++){
        printf("data[%d]=%d ",j,L.data[j]);
    }


    //插入部分(位序)
    ListInSert(&L,1,1);
    ListInSert(&L,2,2);
    ListInSert(&L,3,3);
    ListInSert(&L,4,5);
    printf("\n");                          //先插入4个数
    printf("Insert the result for the first time:\n");
    for(int j =0;j<L.length;j++){                   //  打印结果
        printf("data[%d]=%d ",j,L.data[j]);
    }
    if(ListInSert(&L,4,4)==0){      //插入4,并判断插入是否成功
        printf("\n");
        printf("Fail!!!");
        return 0;
    }
    printf("\n");
    printf("The second time inserts the result:\n");
    for(int j =0;j<L.length;j++){                //打印结果
        printf("data[%d]=%d ",j,L.data[j]);
    }


    //按位序删除
    int e;
    if(ListDelect(&L,5,&e)==0){      //删除位序为5的元素,并判断删除是否成功
        printf("\n");
        printf("Fail Delect!!!");
        return 0;
    }
    printf("\n");
    printf("The result of the deletion:\n");
    for(int j =0;j<L.length;j++){         //打印结果
        printf("data[%d]=%d ",j,L.data[j]);
    }

    return 0;
}

效果如下所示:

 按值查找

        原理很简单,只需要遍历一遍顺序表,找到和目标值匹配的元素的位序返回即可。若找不到则返回-1.

        查找函数代码如下:

int GetElem(SqList *L,int e){                 
    for(int i = 0 ; i < L->length ; i++){
        if(L->data[i] == e){
            return i+1;         //返回该值的位序
        }
    }
    return -1;               //若查找失败则返回-1.
}

        主函数代码在上文的基础上增加查找函数,以查找 值为4 的元素位序为例。代码如下:

int main(void)
{
    SqList L;       //创建一个顺序表L
    InitList(&L);   //初始化
    printf("The initial list:\n");
    for(int j =0;j<L.Maxsize;j++){
        printf("data[%d]=%d ",j,L.data[j]);
    }
    printf("\n");
    IncreaseSize(&L,2);         //增加顺序表长度
    printf("Increase the length of the list:\n");
    for(int j =0;j<L.Maxsize;j++){
        printf("data[%d]=%d ",j,L.data[j]);
    }


    //插入部分(位序)
    ListInSert(&L,1,1);
    ListInSert(&L,2,2);
    ListInSert(&L,3,3);
    ListInSert(&L,4,5);
    printf("\n");                          //先插入4个数
    printf("Insert the result for the first time:\n");
    for(int j =0;j<L.length;j++){                   //  打印结果
        printf("data[%d]=%d ",j,L.data[j]);
    }
    if(ListInSert(&L,4,4)==0){      //插入4,并判断插入是否成功
        printf("\n");
        printf("Fail!!!");
        return 0;
    }
    printf("\n");
    printf("The second time inserts the result:\n");
    for(int j =0;j<L.length;j++){                //打印结果
        printf("data[%d]=%d ",j,L.data[j]);
    }


    //按位序删除
    int e;
    if(ListDelect(&L,5,&e)==0){      //删除位序为5的元素,并判断删除是否成功
        printf("\n");
        printf("Fail Delect!!!");
        return 0;
    }
    printf("\n");
    printf("The result of the deletion:\n");
    for(int j =0;j<L.length;j++){         //打印结果
        printf("data[%d]=%d ",j,L.data[j]);
    }

    //按值查找
    int loc;
    loc = GetElem(&L,4);                  
    printf("\nThe element bit order is %d",loc);

    return 0;
}

效果如下所示:显示 值为4 的元素位序为 4 。

总结 

        本文阐述了顺序表的一些基本操作并附上C语言代码。

        关于按索引进行插入,删除的操作如有需要可评论留言。 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值