ડꫀꫀ ꪗꪮꪊ. 2022-04-13 13:25 采纳率: 88.6%
浏览 20
已结题

C++线性表的实现 代码可以运行 但是功能还无法实现 不知道哪里出了问题 望指导


#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, ElemType a[], int n)  // 中文 ,逗号
{
    int i;
    //L = (Sqlist*)malloc(sizeof(Sqlist));   修改
    for (i = 0; i < n; i++)
        L->data[i] = a[i];
    L->length = n;
}
 
bool ListInsert_Sq(Sqlist*& L, int i, ElemType e)
{
    int j;
    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("%5d", 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)) //if ((i < 1) || (i->lengt))
        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.输出元素a的逻辑位置           8.插入*********\n");
    printf("********9.清空                            *********\n\n");
}
int main()
{
    Sqlist* L;   //Sqlist L;  修改
    int choice, a[10] = { 0,1,2,3,4,5,6,7,8,9 }, i = 10, e;  //修改
    //InitList(L); 修改
    while (1)
    {
        menu();
        printf("请输入菜单序号:\n");
        scanf_s("%d", &choice);
        if (10 == choice) break;
        switch (choice)
        {
        case 1:Initlist_Sq(L);        //初始化      修改
               Creatlist_Sq(L, a, i); //创建顺序表  修改
               break;
        case 2:i = 11; e = 20;         //修改
               ListInsert_Sq(L, i, e); //插入位置 i = 11 ,插入数 e = 20 
               //ListInsert_Sq(Sqlist * &L, int i, ELemType e);   修改
               break;   
        case 3:ListTraverse_Sq(L);            //修改
               //ListTraverse_Sq(Sqlist * &L)   修改
               break;      
        case 4:printf("顺序表L的长度为:%d\n", ListLength_Sq(L)); //修改
                                                //Initlist_Sq(Sqlist * &L);
                break;       
        case 5:if (ListEmpty_Sq(L))           //ListEmpty_Sq(Sqlist * L);
                  printf("顺序表L为空!\n");
               else
                  printf("顺序表L不为空!\n");
               break;         
        //case 6:ListTraverse_Sq(L); break;// ListTravers_Sq(Sqlist * &L);
        //case 7:PrintList(L); break;
        //case 8:ListInsert_Sq(Sqlist * &L, int i, ELemType e); break;
        //case 9:DestroyList_Sq(Sqlist * &L); break;
        default:printf("输入错误!!!\n");
        }
    }
    return 0;
}
 
  • 写回答

1条回答 默认 最新

  • yun6853992 2022-04-13 13:39
    关注

    你的代码是ok的啊,然后也是可运行的代码啊,运行按照你代码的逻辑是没有问题的

    至于在这个基础上想实现什么功能,就看你的功能目的了,自己设计啊

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月17日
  • 已采纳回答 10月9日
  • 创建了问题 4月13日

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?