ZhKuStu 2022-04-07 12:36 采纳率: 66.7%
浏览 73
已结题

c语言图书管理系统-单链表

问题遇到的现象和发生背景

无法正常输出图书名字

问题相关代码,请勿粘贴截图
#include <iostream>
#include <stdlib.h>
typedef int  ElemType;
typedef struct Node
{
    ElemType ISBN;
    char name[10];
    char writer[10];
    char location[10];
    float price;
    struct Node*next;
}book;
void InitList(book *&L)
{
    L=(book *)malloc(sizeof(book));
    if(L==NULL)
    {
        printf("内存分配失败!!\n");
        exit(0);
    }
    L->next==NULL;
}
void creatList(book *&L,ElemType n)//n为要插入图书的数量 
{
    int i;
    book *s,*r;
    r=L;//r为为指针;
    for(i=0;i<n;i++)
    {
        s=(book *)malloc(sizeof(book));
        printf("请输入ISBN号:\n");
        scanf("%d",&(s->ISBN));
        printf("请输入书名:\n");
        scanf("%s",s->name);
        printf("请输入出版社:\n");
        scanf("%s",s->location);
        printf("请输入作者名:\n");
        scanf("%s",s->name);
        printf("请输入价格:\n");
        scanf("%f",&s->price);
        r->next=s;
        r=s;
    }
    r->next=NULL; 
}
void printfbooklist(book *&L)
{
    book *pmove=L->next;
    printf("ISBN    出版社   书名    作者名   价格 \n");
    while(pmove)
    {
        printf("%5d %s %s %s  %5f\n",pmove->ISBN,pmove->location,pmove->name,pmove->writer,pmove->price);
        pmove=pmove->next;
    }
}
int main()
{
    int k,j;
    book *L;
    InitList(L);
    printf("请输入要插入的图书数量:\n");
    scanf("%d",&k);
    creatList(L,k);
    printfbooklist(L);
}

运行结果及报错内容

img

我的解答思路和尝试过的方法


我想要达到的结果
  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-04-07 14:02
    关注

    修改处见注释,供参考:

    #include <iostream>
    #include <stdlib.h>
    typedef int  ElemType;
    typedef struct Node
    {
        ElemType ISBN;
        char name[128];     //修改
        char writer[32];   //修改
        char location[128];//修改
        float price;
        struct Node* next;
    }book;
    void InitList(book*& L)
    {
        L = (book*)malloc(sizeof(book));
        if (L == NULL)
        {
            printf("内存分配失败!!\n");
            exit(0);
        }
        L->next == NULL;
    }
    void creatList(book*& L, ElemType n)//n为要插入图书的数量 
    {
        int i;
        book* s, * r;
        r = L;//r为为指针;
        for (i = 0; i < n; i++)
        {
            s = (book*)malloc(sizeof(book));
            printf("请输入ISBN号:\n");
            scanf("%d", &(s->ISBN));
            getchar();                //修改
            printf("请输入书名:\n");
            scanf("%s", s->name);
            getchar();               //修改
            printf("请输入出版社:\n");
            scanf("%s", s->location);
            getchar();              //修改 
            printf("请输入作者名:\n");
            scanf("%s", s->writer);   //scanf("%s", s->name);//修改
            getchar();              //修改
            printf("请输入价格:\n");
            scanf("%f", &s->price);
            getchar();             //修改  
            r->next = s;
            r = s;
        }
        r->next = NULL;
    }
    void printfbooklist(book*& L)
    {
        book* pmove = L->next;
        printf("ISBN    出版社   书名    作者名   价格 \n");
        while (pmove)
        {
            printf("%5d %s %s %s  %5f\n", pmove->ISBN, pmove->location, pmove->name, pmove->writer, pmove->price);
            pmove = pmove->next;
        }
    }
    int main()
    {
        int k, j;
        book* L;
        InitList(L);
        printf("请输入要插入的图书数量:\n");
        scanf("%d", &k);
        creatList(L, k);
        printfbooklist(L);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月15日
  • 已采纳回答 4月7日
  • 创建了问题 4月7日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。