ડꫀꫀ ꪗꪮꪊ. 2022-04-19 18:36 采纳率: 88.6%
浏览 27
已结题

C++ 请问这个线性表,我的第六个功能:删除,代码函数已经写出来了,为什么运行的时候会有如下问题。如图, 代码贴也在下面。还有第八个功能,显示某个元素的位置,这个怎么实现啊?

img


#include <stdio.h>
#include <stdlib.h>   //修改
#include <string.h>
#include <math.h>
typedef int ElemType;
#define MaxSize 60
#define FALSE 0      //修改
#define TRUE  1      //修改
typedef struct {
    ElemType data[MaxSize];
    int  length;
}Sqlist;

void Initlist_Sq(Sqlist*& L)
{
    L = (Sqlist*)malloc(sizeof(Sqlist));
    L->length = 0;
}
void Creatlist_Sq(Sqlist*& L, char a[], int n)  // 中文 ,逗号
{
    int i;
    //L = (Sqlist*)malloc(sizeof(Sqlist));   修改
    for (i = 0; i < n; i++)
        /*scanf_s("d", &a[i]);*/
        L->data[i] = a[i];
    L->length = n;
}

bool ListInsert_Sq(Sqlist*& L, int i, ElemType e)//插入
{
    int j;
    char f;
    if (i < 1 || i > L->length + 1)
        return FALSE;
    i--;
    for (j = L->length; j > i; j--)
        L->data[j] = L->data[j - 1]; //L->data[j] = L->data[i]; 修改
    L->data[i] = e;                  //L->data[i - 1] = e;      修改
    L->length++;
    return TRUE;
}

void ListTraverse_Sq(Sqlist*& L)//输出
{
    int i;
    for (i = 0; i < L->length; i++)
        printf("%5c", L->data[i]);
    printf("\n");
}

int ListLength_Sq(Sqlist*& L)
{
    return (L->length);
}

bool ListEmpty_Sq(Sqlist*& L)
{
    return (L->length == 0);
}

//void ListTravers_Sq(Sqlist*& L)
//{
//    int i = 2;
//    printf("%d", L->data[i]);
//    printf("\n");
//}

bool ListDelete_Sq(Sqlist*& L, int i, ElemType& e)
{
    int j;
    if ((i < 1) || (i > L->length)) 
        return FALSE;
    i--;
    e = L->data[i];
    for (j = i; j < L->length - 1; j++)
        L->data[j] = L->data[j + 1];
    L->length--;
    return TRUE;
}

void DestroyList_Sq(Sqlist*& L)
{
    free(L);
}

//菜单
void menu()
{
    printf("********1.初始化创建顺序表              2.插入*********\n");
    printf("********3.输出L                         4.输出长度*********\n");
    printf("********5.判断是否为空                  6.删除***\n");
    printf("********7.清空                          8.输出元素a的逻辑位置*********\n");
    
}
int main()
{
    Sqlist* L;   //Sqlist L;  修改
    int choice, i = 7; 
    char f;//修改
      char a[] = { 'a','b','c','d','e' };
    //InitList(L); 修改
    while (1)
    {
        menu();
        printf("请输入菜单序号:\n");
        scanf_s("%d", &choice);
        if (9 == choice) break;
        switch (choice)
        {
        case 1:
        {Initlist_Sq(L);        //初始化      修改
        Creatlist_Sq(L, a, i); //创建顺序表  修改
        break;
        }

        case 2:
        {char ch = 'f';
        i = 4;
        ListInsert_Sq(L, i, ch); //插入位置 i =4 ,插入f
        break;
        }
        case 3: {ListTraverse_Sq(L);            //修改
            break; }
        case 4: {printf("顺序表L的长度为:%d\n", ListLength_Sq(L)); //修改

            break; }
        case 5: {if (ListEmpty_Sq(L))
            printf("顺序表L为空!\n");
              else
            printf("顺序表L不为空!\n");
            break; }
        case 6: {char ah;
           ListDelete_Sq(L,i,ah);
            break; }
              // ListTravers_Sq(Sqlist * &L);
        case 7:
        {DestroyList_Sq(L); break; }
        default:printf("输入错误!!!\n");
            }
        }
              return 0;
        }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 4月27日
    • 创建了问题 4月19日

    悬赏问题

    • ¥15 STM32F103C8T6使用标准库stm32f10x.h驱动ws2812
    • ¥20 我是数控机床电气工程师,主要是做840DSL与one,请问如何自学
    • ¥20 显示器休眠后重新唤醒出现蓝屏,蓝屏代码为DRIVER-POWER.STATE-FAILURE
    • ¥20 alt+tab怎么恢复到以前的模式
    • ¥15 来一个会抓包app支付链接的
    • ¥15 MMdetection安装
    • ¥15 STM32U535系列stop3模式进入和RTC唤醒
    • ¥15 如何提取全民K歌没下载过但播放过很多次的音频?
    • ¥15 树莓派运行detect.py
    • ¥15 pdfplumber提起文本内容如何过滤水印内容