最好的我们! 2024-03-11 14:14 采纳率: 50%
浏览 4
已结题

顺序线性表的基本操作

我的代码错误的地方在哪?



```c
#define  _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<malloc.h>
#define OK 1 
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef struct
{
    int* elem;
    int length;
    int listsize;
}SqList;

int InitList_Sq(SqList& L)
{
    // 算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
    // 请补全代码
    
    L.elem = new ElemType[LIST_INIT_SIZE];
    if (!L.elem) return(ERROR);
    L.length = 0;
    return OK;
}

int Load_Sq(SqList& L)
{
    // 输出顺序表中的所有元素
    int i;
    if (L.length == 0) printf("The List is empty!");  // 请填空
    else
    {
        printf("The List is: ");
        for (i = 0; i < L.length;i++) printf("%d ",L.elem[i]);  // 请填空
    }
    printf("\n");
    return OK;
}

int ListInsert_Sq(SqList& L, int i, int e)
{
    // 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
    // i的合法值为1≤i≤L.length +1
    // 请补全代码
    int j = 0;
    if (i<1 || i>L.length+1) return ERROR;
    else
    {
        for (j = L.length - 1; j >= i - 1; j--)
        {
            L.elem[j + 1] = L.elem[j];
        }
        L.elem[j] = e;
        L.length++;
        return OK;
    }
    
}

int ListDelete_Sq(SqList& L, int i, int& e)
{
    // 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
    // i的合法值为1≤i≤L.length
    // 请补全代码
    if (i<1 || i>L.length)
        return ERROR;
    else
    {
        e = L.elem[i - 1];
        for (; i <= L.length; i++)
        {
            L.elem[i-1] = L.elem[i];
        }
        L.length--;
        return OK;
    }
    
}

int main()
{
    SqList T;
    int a, i;
    ElemType e, x;
    if (InitList_Sq(T))    // 判断顺序表是否创建成功
    {
        printf("A Sequence List Has Created.\n");
    }
    while (1)
    {
        printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
        scanf("%d", &a);
        switch (a)
        {
        case 1: scanf("%d%d", &i, &x);
            if (ListInsert_Sq( T, i, x)==ERROR) printf("Insert Error!\n"); // 执行插入函数,根据返回值判断i值是否合法
            else printf("The Element %d is Successfully Inserted!\n", x);
            break;
        case 2: scanf("%d", &i);
            if (ListDelete_Sq(T,i,e)==ERROR) printf("Delete Error!\n"); // 执行删除函数,根据返回值判断i值是否合法
            else printf("The Element %d is Successfully Deleted!\n", e);
            break;
        case 3: Load_Sq(T);
            break;
        case 0: return 1;
        }
    }
}

标准输入数据:
1
2 1
1
1 5
2
2
2
1
1
1 5
1
1 8
1
1 9
3
2
3
2
2
2
1
3
0

标准输出答案:
   1|A Sequence List Has Created.
   2|1:Insert element
   3|2:Delete element
   4|3:Load all elements
   5|0:Exit
   6|Please choose:
   7|Insert Error!
   8|1:Insert element
   9|2:Delete element
  10|3:Load all elements
  11|0:Exit
  12|Please choose:
  13|The Element 5 is Successfully Inserted!
  14|1:Insert element
  15|2:Delete element
  16|3:Load all elements
  17|0:Exit
  18|Please choose:
  19|Delete Error!
  20|1:Insert element
  21|2:Delete element
  22|3:Load all elements
  23|0:Exit
  24|Please choose:
  25|The Element 5 is Successfully Deleted!
  26|1:Insert element
  27|2:Delete element
  28|3:Load all elements
  29|0:Exit
  30|Please choose:
  31|The Element 5 is Successfully Inserted!
  32|1:Insert element
  33|2:Delete element
  34|3:Load all elements
  35|0:Exit
  36|Please choose:
  37|The Element 8 is Successfully Inserted!
  38|1:Insert element
  39|2:Delete element
  40|3:Load all elements
  41|0:Exit
  42|Please choose:
  43|The Element 9 is Successfully Inserted!
  44|1:Insert element
  45|2:Delete element
  46|3:Load all elements
  47|0:Exit
  48|Please choose:
  49|The List is: 9 8 5
  50|1:Insert element
  51|2:Delete element
  52|3:Load all elements
  53|0:Exit
  54|Please choose:
  55|The Element 5 is Successfully Deleted!
  56|1:Insert element
  57|2:Delete element
  58|3:Load all elements
  59|0:Exit
  60|Please choose:
  61|The Element 8 is Successfully Deleted!
  62|1:Insert element
  63|2:Delete element
  64|3:Load all elements
  65|0:Exit
  66|Please choose:
  67|The Element 9 is Successfully Deleted!
  68|1:Insert element
  69|2:Delete element
  70|3:Load all elements
  71|0:Exit
  72|Please choose:
  73|The List is empty!
  74|1:Insert element
  75|2:Delete element
  76|3:Load all elements
  77|0:Exit
  78|Please choose:

你的错误输出结果:
   1|A Sequence List Has Created.
   2|1:Insert element
   3|2:Delete element
   4|3:Load all elements
   5|0:Exit
   6|Please choose:
   7|Insert Error!
   8|1:Insert element
   9|2:Delete element
  10|3:Load all elements
  11|0:Exit
  12|Please choose:
  13|The Element 5 is Successfully Inserted!
  14|1:Insert element
  15|2:Delete element
  16|3:Load all elements
  17|0:Exit
  18|Please choose:
  19|Delete Error!
  20|1:Insert element
  21|2:Delete element
  22|3:Load all elements
  23|0:Exit
  24|Please choose:
  25|The Element 0 is Successfully Deleted!
  26|1:Insert element
  27|2:Delete element
  28|3:Load all elements
  29|0:Exit
  30|Please choose:
  31|The Element 5 is Successfully Inserted!
  32|1:Insert element
  33|2:Delete element
  34|3:Load all elements
  35|0:Exit
  36|Please choose:
  37|The Element 8 is Successfully Inserted!
  38|1:Insert element
  39|2:Delete element
  40|3:Load all elements
  41|0:Exit
  42|Please choose:
  43|The Element 9 is Successfully Inserted!
  44|1:Insert element
  45|2:Delete element
  46|3:Load all elements
  47|0:Exit
  48|Please choose:
  49|The List is: 0 0 0
  50|1:Insert element
  51|2:Delete element
  52|3:Load all elements
  53|0:Exit
  54|Please choose:
  55|The Element 0 is Successfully Deleted!
  56|1:Insert element
  57|2:Delete element
  58|3:Load all elements
  59|0:Exit
  60|Please choose:
  61|The Element 0 is Successfully Deleted!
  62|1:Insert element
  63|2:Delete element
  64|3:Load all elements
  65|0:Exit
  66|Please choose:
  67|The Element 0 is Successfully Deleted!
  68|1:Insert element
  69|2:Delete element
  70|3:Load all elements
  71|0:Exit
  72|Please choose:
  73|The List is empty!
  74|1:Insert element
  75|2:Delete element
  76|3:Load all elements
  77|0:Exit
  78|Please choose:
  • 写回答

5条回答 默认 最新

  • 檀越@新空间 2024-03-11 14:16
    关注

    最好的我们! 下午好🌅🌅🌅
    本答案参考ChatGPT-3.5

    根据给出的代码,我发现了一处错误。错误出现在ListInsert_Sq函数中,在插入元素之后没有正确更新j的值。

    以下是修改后的代码:

    int ListInsert_Sq(SqList& L, int i, int e) {
        // 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
        // i的合法值为1≤i≤L.length +1
        // 请补全代码
        int j = 0;
        if (i<1 || i>L.length+1) return ERROR;
        else
        {
            for (j = L.length - 1; j >= i - 1; j--)
            {
                L.elem[j + 1] = L.elem[j];
            }
            L.elem[j + 1] = e; // 修改这一行,将插入元素放在正确的位置
            L.length++;
            return OK;
        }
        
    }
    

    这样修改之后,代码应该能够按照预期输出结果了。请尝试运行修改后的代码,如果还有其他问题,请告诉我。

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

报告相同问题?

问题事件

  • 系统已结题 3月23日
  • 已采纳回答 3月15日
  • 创建了问题 3月11日