「已注销」 2023-03-20 09:52 采纳率: 71.4%
浏览 11
已结题

关于#数据结构#的问题,如何解决?(语言-c++)

【问题描述】设顺序表S中的数据元素递增有序。试编写程序,将数据x插入顺序表S,要求插入后保持该表的有序性。

【输入形式】
【输出形式】
【样例输入】8

                25 28  36  78  96  102  980  1000

                88

【样例输出】25 28 36 78 88 96 102 980 1000
【样例说明】设置表长为8

                 原顺序表s:25 28  36  78  96  102  980  1000

                 插入数据:8
#include <stdio.h>
#include <stdlib.h>
#define initsize 20
#define LISTINCREMENT 5
typedef int ElemType;
typedef struct
{
    ElemType *elem;
    int length;
    int listsize;
}SqList;
void IntiList(SqList &L,int n)
{
    L.elem=(ElemType *)malloc(n*sizeof(ElemType));
    if(!L.elem)
        exit(1);
    L.length=n;
    L.listsize=initsize;
} 
int ListInsert(SqList &L,int i,ElemType x)
{
        if(L.length+1>=L.listsize)
        {    
            return 0;
        }
        for(int j=L.length;j>=i;j--)
            L.elem[j]=L.elem[j-1];
        L.elem[i-1]=x;
        L.length++;
}
int main()
{
    int n,i;
    ElemType x,*p;
    SqList L;
    printf("\n");
    scanf("%d",&n);
    IntiList(L,n);
    printf("\n");
    scanf("%d",&x);
    for(i=1,p=L.elem;p<L.elem+L.length&&*p<x;p++)
    {
        ++i;
    }
    ListInsert(L,i,x);
    for(int h=0;h<L.length;h++)
        printf("%d",L.elem[h]);
    return 0;

}

该怎么修改这个代码

  • 写回答

3条回答 默认 最新

  • lshen01 2023-03-20 10:17
    关注

    参考GPT和自己的思路:

    您的代码中已经包含了插入有序表的算法,只需要在输入数据x后找到插入位置并调用ListInsert函数就可以了。请注意,ListInsert函数应该返回一个值,以判断插入是否成功,否则会出现一些潜在的问题。下面是修改后的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #define initsize 20
    #define LISTINCREMENT 5
    typedef int ElemType;
    typedef struct
    {
        ElemType *elem;
        int length;
        int listsize;
    }SqList;
    void IntiList(SqList &L,int n)
    {
        L.elem=(ElemType *)malloc(n*sizeof(ElemType));
        if(!L.elem)
            exit(1);
        L.length=n;
        L.listsize=initsize;
    } 
    int ListInsert(SqList &L,int i,ElemType x)
    {
        if(L.length+1>=L.listsize)
        {    
            return 0;
        }
        for(int j=L.length;j>=i;j--)
        {
            L.elem[j]=L.elem[j-1];
        }
        L.elem[i-1]=x;
        L.length++;
        return 1; // 返回1表示插入成功
    }
    int main()
    {
        int n,i;
        ElemType x,*p;
        SqList L;
        printf("请输入原始顺序表的长度:");
        scanf("%d",&n);
        IntiList(L,n);
        printf("请输入原始顺序表的数据:");
        for(i=0;i<n;i++)
        {
            scanf("%d", &(L.elem[i]));
        }
        printf("请输入要插入的数据:");
        scanf("%d",&x);
        for(i=1,p=L.elem;p<L.elem+L.length&&*p<x;p++)
        {
            ++i;
        }
        if(ListInsert(L,i,x) == 1) // 判断是否插入成功
        {
            printf("插入后的顺序表为:");
            for(int h=0;h<L.length;h++)
            {
                printf("%d ",L.elem[h]);
            }
            printf("\n");
        }
        else
        {
            printf("插入失败!\n");
        }
        return 0;
    }
    

    如上代码中,修改了以下内容:

    1. 添加了提示信息,让用户指定原始顺序表的长度和数据。

    2. 修改了ListInsert函数,增加了返回值,以便判断插入是否成功。

    3. 在main函数中调用ListInsert函数,并根据返回值判断插入是否成功。插入成功时输出插入后的顺序表,插入失败时输出插入失败的提示信息。同时,在输出顺序表时,每个元素之间加一个空格,使得输出更清晰。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月28日
  • 已采纳回答 3月20日
  • 创建了问题 3月20日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改