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

我的这个程序是实现线性表的基本操作 我的思路都在下面了 算法也写出来了 但运行不起来 就是具体 不知道算法与程序之间的关系 怎么实现?求指导!


#include<stdio.h>
#include<string.h>
#include<math.h>
typedef int ElemType;

#define MaxSize 60
typedef struct {
    ElemType data[MaxSize];
    int length;
}Sqlist;

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;
}


void Initlist_Sq(Sqlist*& L)
{
    L = (Sqlist*)malloc(sizeof(Sqlist));
    L->length = 0;
}

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[i];
    L->data[i - 1] = e;
    L->length++;
    return TRUE;

}


void ListTraverse_Sq(Sqlist*& L)
{
    int i;
    for (i = 0; i < L->length; i++)
        printf("%d", 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->lengt))
        return FALSE;
    i--;
    e = L->data[i];
    for (j = ilj < 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");
}




int main()
{
    Sqlist L; int choice;
    InitList(L);
    while (1)
    {
        menu();
        printf("请输入菜单序号:\n");
        scanf("%d", &choice);
        if (10 == choice) break;
        switch (choice)
        {
        case 1:Initlist_Sq(L); break;
        case 2:ListInsert_Sq(Sqlist * &L, int i, ELemType e); break;
        case 3:ListTraverse_Sq(Sqlist * &L); break;
        case 4:Initlist_Sq(Sqlist * &L); break;
        case 5:ListEmpty_Sq(Sqlist * L); break;
        case 6: ListTravers_Sq(Sqlist * &L); break;
        /*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;
}
  • 写回答

3条回答 默认 最新

  • qzjhjxj 2022-04-12 16:45
    关注

    修改如下,实现菜单1-5选项的功能,供参考:

    #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("%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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月21日
  • 已采纳回答 4月13日
  • 创建了问题 4月12日

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 eclipse连接sap后代码跑出来空白
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案