【数据结构合集】线性表的顺序存储结构

该代码实现了线性表的顺序存储结构,包括初始化、摧毁、检查容量、检查空表、显示长度、显示元素、按位置查找、按值查找、插入、删除、头尾压入弹出等操作。程序提供了一个交互式菜单供用户选择操作,并在操作过程中动态调整表的容量。
摘要由CSDN通过智能技术生成

⭐️写在前面的话⭐️

📒博客主页: 程序员好冰
🎉欢迎 【点赞👍 关注🔎 收藏⭐️ 留言📝】
📌本文由 程序员好冰 原创,CSDN 首发!
📆入站时间: 🌴2022 年 07 月 13 日🌴
✉️ 是非不入松风耳,花落花开只读书。
💭推荐书籍:📚《Java编程思想》,📚《Java 核心技术卷》
💬参考在线编程网站:🌐牛客网🌐力扣
🍭 作者水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!🍭


线性表的顺序存储结构

(以下简称顺序表)

一些约定

#include <stdio.h>
#include <stdlib.h>

//状态码处理
#define OK 1
#define ERROR 0

typedef int ElementType;//代表列表中每一个数据的类型
typedef int Status;//代表函数执行之后的返回状态码

typedef struct SqList
{
    ElementType *elem;//定义顺序表
    int length;//表的实际长度
    int max_capacity;//表当前的最大容量
}SqList;

方法的声明

//声明方法
/*对表进行操作*/
Status init_list(SqList*);//1初始化顺序表
Status destory_list(SqList*);//2摧毁顺序表(清空表)
Status check_capcity_isEnough(SqList*);//3检查表的最大容量是否充足
Status check_list_isEmpty(SqList);//4检查表是否为空
Status show_list_length(SqList);//5打印表当前的长度


/*对表中数据进行操作*/
//以下3个函数,不会对表造成改变,故传入SqList
Status show_list(SqList);//6显示表中所有元素
Status getElem_by_local(SqList,int);//7按位置实现查找值
Status getIndex_by_value(SqList,ElementType);//8按值实现查找位置

//以下7个函数,会对表造成改变,故传入SqList*,指针
Status insert_list_by_local(SqList*,int,ElementType);//9按位置实现插入
Status delete_list_by_local(SqList*,int);//10按位置实现删除
Status delete_list_by_value(SqList*,ElementType);//11按值实现删除(全部删除)
Status push_elem_head(SqList*,ElementType);//12头压入数据
Status push_elem_foot(SqList*,ElementType);//13尾压入数据
Status pop_elem_head(SqList*);//14头弹出数据
Status pop_elem_foot(SqList*);//15尾弹出数据

方法声明中,如何判断传入的参数是指针还是参数本身?

当你传递一个参数给函数的时候,这个参数会不会在函数内被改动决定了使用什么参数形式。

  • 如果需要被改动,则需要传递指向这个参数的指针。
  • 如果不用被改动,可以直接传递这个参数。

1、初始化顺序表

//1初始化
Status init_list(SqList* L)
{
    //L->elem=(ElementType*)malloc(L->max_capacity*sizeof(ElementType));
    if(!L->elem){
        printf("表初始化失败.\n");
        return ERROR;//L为NULL,说明空间分配失败
    }
    L->length=0;
    L->max_capacity=0;
    printf("初始化表成功.\n");
    return OK;
}

2、摧毁顺序表

//2摧毁顺序表
Status destory_list(SqList* L)
{
    if(!L->elem){
        printf("摧毁失败.\n");
        return ERROR;
    }
    L->elem=NULL;
    L->length=0;
    L->max_capacity=0;
    printf("摧毁成功.\n");
    return OK;
}

3、检查表的最大容量是否充足

//3检查表的最大容量是否充足,不够的话进行扩充
Status check_capcity_isEnough(SqList *L)
{
    //如果表的长度达到表的最大容量-1,则进行扩容
    if(L->length==L->max_capacity-1){
        //如果表的容量是0,则赋初值10;若不为0,则扩容1倍
        int new_max_capacity = L->max_capacity==0 ? 10 :L->max_capacity*2;
        ElementType *tempElem = (ElementType*)realloc(L->elem,new_max_capacity*sizeof(ElementType));
        if(!tempElem){
            printf("扩容失败.\n");
            return ERROR;
        }
        L->elem = tempElem;
        L->max_capacity = new_max_capacity;
        printf("扩容成功.\n");
        return OK;
    }
    return OK;
}

4、检查表是否为空

//4检查表是否为空
Status check_list_isEmpty(SqList L)
{
    if(L.length!=0){
        printf("表不为空.\n");
        return OK;
    }else
    {
        printf("表为空.\n");
        return OK;
    }
    return OK;
}

5、打印表当前的长度

//5打印表当前的长度
Status show_list_length(SqList L)
{
    printf("表长为%d.\n",L.length);
    return OK;
}

6、显示表中所有元素

//6显示表中所有元素
Status show_list(SqList L)
{
    if(L.length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }
    int i;
    printf("表为:");
    for(i=0;i<L.length;i++){
        printf("%d  ",L.elem[i]);
    }
    printf("\n");
    return OK;
}

7、按位置实现查找值

//7按位置实现查找值
Status getElem_by_local(SqList L,int key)
{
    if(L.length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }
    if(key<1||key>L.length){
        printf("输入的位置有误,请重新操作.\n");
        return ERROR;
    }

    printf("第%d个元素为%d.\n",key,L.elem[key-1]);
    return OK;
}

8、按值实现查找位置

//8按值实现查找位置
Status getIndex_by_value(SqList L,ElementType value)
{
    if(L.length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }
    int i;
    int count;
    for(i=0;i<L.length;i++){
        if(L.elem[i]==value){
            printf("第%d个元素值为%d.\n",i+1,value);
            count++;
        }
    }
    if(count==0){
        printf("表中没有%d这个值.\n",value);
        return ERROR;
    }
    return OK;
}

9、按位置实现插入

//9按位置实现插入
Status insert_list_by_local(SqList *L,int key,ElementType value)
{
    if(key<1||key>L->length+1){
        printf("输入的位置有误,请重新操作.\n");
        return ERROR;
    }
    check_capcity_isEnough(L);
    int i;
    for(i=L->length-1;i>=key-1;i--){
        L->elem[i+1]=L->elem[i];
    }
    L->elem[key-1]=value;
    L->length++;
    show_list(*L);
    return OK;
}

10、按位置实现删除

//10按位置实现删除
Status delete_list_by_local(SqList *L,int key)
{
     if(L->length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }

    if(key<1||key>L->length){
        printf("输入的位置有误,请重新操作.\n");
        return ERROR;
    }
    ElementType tem_ele=L->elem[key-1];
    int i;
    for(i=key-1;i<L->length;i++){
        L->elem[i]=L->elem[i+1];
    }
    L->length--;
    printf("元素%d删除成功.\n",tem_ele);
    show_list(*L);
    return OK;
}

11、按值实现删除(全部删除)

//11按值实现删除(全部删除)
Status delete_list_by_value(SqList *L,ElementType value)
{
    if(L->length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }
    int i;
    int num=0;
    int j=0;//记录保留元素(不为value的元素)的个数
    for(i=0;i<L->length;i++){
        if(L->elem[i]!=value){
            L->elem[j]=L->elem[i];
            j++;

        }else
        {
            num++;
            printf("第%d个元素成功删除.\n",i+1);
        }
    }
    if(j==0){
        printf("表中没有%d这个值.\n",value);
        return ERROR;
    }
    L->length=j;
    printf("一共发现%d个值为%d的元素.\n",num,value);
    show_list(*L);
    return OK;
}

12、头压入数据

//12头压入数据
Status push_elem_head(SqList *L,ElementType value)
{
    check_capcity_isEnough(L);
    int i;
    L->length++;
    for(i=L->length-1;i>0;i--){
        L->elem[i]=L->elem[i-1];
    }
    L->elem[0]=value;
    printf("%d被压入.\n",value);
    printf("头部插入成功.\n");
    show_list(*L);
    return OK;
}

13、尾压入数据

//13尾压入数据
Status push_elem_foot(SqList *L,ElementType value)
{
    check_capcity_isEnough(L);
    L->length++;
    L->elem[L->length-1]=value;
    printf("%d被压入.\n",value);
    printf("尾部压入元素成功.\n");
    show_list(*L);
    return OK;
}

14、头弹出数据

//14头弹出数据
Status pop_elem_head(SqList *L)
{
    if(L->length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }else
    {
        ElementType tem_elem;
        tem_elem = L->elem[0];
        int i;
        for(i=0;i<L->length-1;i++){
            L->elem[i]=L->elem[i+1];
        }
        L->length--;
        show_list(*L);
        printf("%d被弹出.\n",tem_elem);
        printf("头部弹出元素成功.\n");
        return OK;
    }
    return OK;
}

15、尾弹出数据

//15尾弹出数据
Status pop_elem_foot(SqList *L)
{
    if(L->length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }else
    {
        printf("%d被弹出.\n",L->elem[L->length-1]);
        L->length--;
        printf("尾部弹出成功.\n");
        return OK;
    }
    return OK;
}

主函数

int main()
{
    SqList L;
    printf("请继续操作...\n");
    while(1){
        int input;
        int key;
        ElementType value;
        printf("\n==========================\n");
        printf("1、初始化顺序表.\n");
        printf("2、摧毁顺序表.\n");
        printf("3、扩容顺序表容量.\n");
        printf("4、检查顺序表是否为空.\n");
        printf("5、打印表的长度.\n");
        printf("6、打印表中所有元素.\n");
        printf("7、按位置实现查找值.\n");
        printf("8、按值实现查找位置.\n");
        printf("9、按位置实现插入.\n");
        printf("10、按位置实现删除.\n");
        printf("11、按值实现删除.\n");
        printf("12、头部压入元素.\n");
        printf("13、尾部压入元素.\n");
        printf("14、头部弹出元素.\n");
        printf("15、尾部弹出元素.\n");
        printf("==========================\n");
        printf("请输入对应操作的序号:");
        scanf("%d",&input);
        switch(input)
        {
        case 1:
            init_list(&L);
            break;
        case 2:
            destory_list(&L);
            break;
        case 3:
            check_capcity_isEnough(&L);
            break;
        case 4:
            check_list_isEmpty(L);
            break;
        case 5:
            show_list_length(L);
            break;
        case 6:
            show_list(L);
            break;
        case 7:
            printf("请输入要查找的位置(K):");
            scanf("%d",&key);
            getElem_by_local(L,key);
            break;
        case 8:
            printf("请输入要查找的值(V):");
            scanf("%d",&value);
            getIndex_by_value(L,value);
            break;
        case 9:
            printf("请输入要插入的位置和值(K,V):");
            scanf("%d,%d",&key,&value);
            insert_list_by_local(&L,key,value);
            break;
        case 10:
            printf("请输入要删除的位置(K):");
            scanf("%d",&key);
            delete_list_by_local(&L,key);
            break;
        case 11:
            printf("请输入要删除的值(V):");
            scanf("%d",&value);
            delete_list_by_value(&L,value);
            break;
        case 12:
            printf("请输入要头压入的值(V):");
            scanf("%d",&value);
            push_elem_head(&L,value);
            break;
        case 13:
            printf("请输入要尾压入的值(V):");
            scanf("%d",&value);
            push_elem_foot(&L,value);
            break;
        case 14:
            pop_elem_head(&L);
            break;
        case 15:
            pop_elem_foot(&L);
            break;
        default:
            printf("输入的序号有误,请重新输入...\n");
            break;
        }
    }
    return 0;
}

程序源码

#include <stdio.h>
#include <stdlib.h>

//状态码处理
#define OK 1
#define ERROR 0

typedef int ElementType;//代表列表中每一个数据的类型
typedef int Status;//代表函数执行之后的返回状态码

typedef struct SqList
{
    ElementType *elem;//定义顺序表
    int length;//表的实际长度
    int max_capacity;//表当前的最大容量
}SqList;

//声明方法
/*对表进行操作*/
Status init_list(SqList*);//1初始化顺序表
Status destory_list(SqList*);//2摧毁顺序表(清空表)
Status check_capcity_isEnough(SqList*);//3检查表的最大容量是否充足
Status check_list_isEmpty(SqList);//4检查表是否为空
Status show_list_length(SqList);//5打印表当前的长度


/*对表中数据进行操作*/
//以下3个函数,不会对表造成改变,故传入SqList
Status show_list(SqList);//6显示表中所有元素
Status getElem_by_local(SqList,int);//7按位置实现查找值
Status getIndex_by_value(SqList,ElementType);//8按值实现查找位置

//以下7个函数,会对表造成改变,故传入SqList*,指针
Status insert_list_by_local(SqList*,int,ElementType);//9按位置实现插入
Status delete_list_by_local(SqList*,int);//10按位置实现删除
Status delete_list_by_value(SqList*,ElementType);//11按值实现删除(全部删除)
Status push_elem_head(SqList*,ElementType);//12头压入数据
Status push_elem_foot(SqList*,ElementType);//13尾压入数据
Status pop_elem_head(SqList*);//14头弹出数据
Status pop_elem_foot(SqList*);//15尾弹出数据

//1初始化
Status init_list(SqList* L)
{
    //L->elem=(ElementType*)malloc(L->max_capacity*sizeof(ElementType));
    if(!L->elem){
        printf("表初始化失败.\n");
        return ERROR;//L为NULL,说明空间分配失败
    }
    L->length=0;
    L->max_capacity=0;
    printf("初始化表成功.\n");
    return OK;
}

//2摧毁顺序表
Status destory_list(SqList* L)
{
    if(!L->elem){
        printf("摧毁失败.\n");
        return ERROR;
    }
    L->elem=NULL;
    L->length=0;
    L->max_capacity=0;
    printf("摧毁成功.\n");
    return OK;
}

//3检查表的最大容量是否充足,不够的话进行扩充
Status check_capcity_isEnough(SqList *L)
{
    //如果表的长度达到表的最大容量-1,则进行扩容
    if(L->length==L->max_capacity-1){
        //如果表的容量是0,则赋初值10;若不为0,则扩容1倍
        int new_max_capacity = L->max_capacity==0 ? 10 :L->max_capacity*2;
        ElementType *tempElem = (ElementType*)realloc(L->elem,new_max_capacity*sizeof(ElementType));
        if(!tempElem){
            printf("扩容失败.\n");
            return ERROR;
        }
        L->elem = tempElem;
        L->max_capacity = new_max_capacity;
        printf("扩容成功.\n");
        return OK;
    }
    return OK;
}

//4检查表是否为空
Status check_list_isEmpty(SqList L)
{
    if(L.length!=0){
        printf("表不为空.\n");
        return OK;
    }else
    {
        printf("表为空.\n");
        return OK;
    }
    return OK;
}

//5打印表当前的长度
Status show_list_length(SqList L)
{
    printf("表长为%d.\n",L.length);
    return OK;
}

//6显示表中所有元素
Status show_list(SqList L)
{
    if(L.length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }
    int i;
    printf("表为:");
    for(i=0;i<L.length;i++){
        printf("%d  ",L.elem[i]);
    }
    printf("\n");
    return OK;
}

//7按位置实现查找值
Status getElem_by_local(SqList L,int key)
{
    if(L.length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }
    if(key<1||key>L.length){
        printf("输入的位置有误,请重新操作.\n");
        return ERROR;
    }

    printf("第%d个元素为%d.\n",key,L.elem[key-1]);
    return OK;
}

//8按值实现查找位置
Status getIndex_by_value(SqList L,ElementType value)
{
    if(L.length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }
    int i;
    int count;
    for(i=0;i<L.length;i++){
        if(L.elem[i]==value){
            printf("第%d个元素值为%d.\n",i+1,value);
            count++;
        }
    }
    if(count==0){
        printf("表中没有%d这个值.\n",value);
        return ERROR;
    }
    return OK;
}

//9按位置实现插入
Status insert_list_by_local(SqList *L,int key,ElementType value)
{
    if(key<1||key>L->length+1){
        printf("输入的位置有误,请重新操作.\n");
        return ERROR;
    }
    check_capcity_isEnough(L);
    int i;
    for(i=L->length-1;i>=key-1;i--){
        L->elem[i+1]=L->elem[i];
    }
    L->elem[key-1]=value;
    L->length++;
    show_list(*L);
    return OK;
}

//10按位置实现删除
Status delete_list_by_local(SqList *L,int key)
{
     if(L->length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }

    if(key<1||key>L->length){
        printf("输入的位置有误,请重新操作.\n");
        return ERROR;
    }
    ElementType tem_ele=L->elem[key-1];
    int i;
    for(i=key-1;i<L->length;i++){
        L->elem[i]=L->elem[i+1];
    }
    L->length--;
    printf("元素%d删除成功.\n",tem_ele);
    show_list(*L);
    return OK;
}

//11按值实现删除(全部删除)
Status delete_list_by_value(SqList *L,ElementType value)
{
    if(L->length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }
    int i;
    int num=0;
    int j=0;//记录保留元素(不为value的元素)的个数
    for(i=0;i<L->length;i++){
        if(L->elem[i]!=value){
            L->elem[j]=L->elem[i];
            j++;

        }else
        {
            num++;
            printf("第%d个元素成功删除.\n",i+1);
        }
    }
    if(j==0){
        printf("表中没有%d这个值.\n",value);
        return ERROR;
    }
    L->length=j;
    printf("一共发现%d个值为%d的元素.\n",num,value);
    show_list(*L);
    return OK;
}

//12头压入数据
Status push_elem_head(SqList *L,ElementType value)
{
    check_capcity_isEnough(L);
    int i;
    L->length++;
    for(i=L->length-1;i>0;i--){
        L->elem[i]=L->elem[i-1];
    }
    L->elem[0]=value;
    printf("%d被压入.\n",value);
    printf("头部插入成功.\n");
    show_list(*L);
    return OK;
}

//13尾压入数据
Status push_elem_foot(SqList *L,ElementType value)
{
    check_capcity_isEnough(L);
    L->length++;
    L->elem[L->length-1]=value;
    printf("%d被压入.\n",value);
    printf("尾部压入元素成功.\n");
    show_list(*L);
    return OK;
}

//14头弹出数据
Status pop_elem_head(SqList *L)
{
    if(L->length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }else
    {
        ElementType tem_elem;
        tem_elem = L->elem[0];
        int i;
        for(i=0;i<L->length-1;i++){
            L->elem[i]=L->elem[i+1];
        }
        L->length--;
        show_list(*L);
        printf("%d被弹出.\n",tem_elem);
        printf("头部弹出元素成功.\n");
        return OK;
    }
    return OK;
}

//15尾弹出数据
Status pop_elem_foot(SqList *L)
{
    if(L->length==0){
        printf("表为空,请先添加元素.\n");
        return ERROR;
    }else
    {
        printf("%d被弹出.\n",L->elem[L->length-1]);
        L->length--;
        printf("尾部弹出成功.\n");
        return OK;
    }
    return OK;
}

int main()
{
    SqList L;
    printf("请继续操作...\n");
    while(1){
        int input;
        int key;
        ElementType value;
        printf("\n==========================\n");
        printf("1、初始化顺序表.\n");
        printf("2、摧毁顺序表.\n");
        printf("3、扩容顺序表容量.\n");
        printf("4、检查顺序表是否为空.\n");
        printf("5、打印表的长度.\n");
        printf("6、打印表中所有元素.\n");
        printf("7、按位置实现查找值.\n");
        printf("8、按值实现查找位置.\n");
        printf("9、按位置实现插入.\n");
        printf("10、按位置实现删除.\n");
        printf("11、按值实现删除.\n");
        printf("12、头部压入元素.\n");
        printf("13、尾部压入元素.\n");
        printf("14、头部弹出元素.\n");
        printf("15、尾部弹出元素.\n");
        printf("==========================\n");
        printf("请输入对应操作的序号:");
        scanf("%d",&input);
        switch(input)
        {
        case 1:
            init_list(&L);
            break;
        case 2:
            destory_list(&L);
            break;
        case 3:
            check_capcity_isEnough(&L);
            break;
        case 4:
            check_list_isEmpty(L);
            break;
        case 5:
            show_list_length(L);
            break;
        case 6:
            show_list(L);
            break;
        case 7:
            printf("请输入要查找的位置(K):");
            scanf("%d",&key);
            getElem_by_local(L,key);
            break;
        case 8:
            printf("请输入要查找的值(V):");
            scanf("%d",&value);
            getIndex_by_value(L,value);
            break;
        case 9:
            printf("请输入要插入的位置和值(K,V):");
            scanf("%d,%d",&key,&value);
            insert_list_by_local(&L,key,value);
            break;
        case 10:
            printf("请输入要删除的位置(K):");
            scanf("%d",&key);
            delete_list_by_local(&L,key);
            break;
        case 11:
            printf("请输入要删除的值(V):");
            scanf("%d",&value);
            delete_list_by_value(&L,value);
            break;
        case 12:
            printf("请输入要头压入的值(V):");
            scanf("%d",&value);
            push_elem_head(&L,value);
            break;
        case 13:
            printf("请输入要尾压入的值(V):");
            scanf("%d",&value);
            push_elem_foot(&L,value);
            break;
        case 14:
            pop_elem_head(&L);
            break;
        case 15:
            pop_elem_foot(&L);
            break;
        default:
            printf("输入的序号有误,请重新输入...\n");
            break;
        }
    }
    return 0;
}

🚀先看后赞,养成习惯!🚀

🚀 先看后赞,养成习惯!🚀

🎈觉得文章写得不错的老铁们,点赞评论关注走一波!谢谢啦!🎈


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值