兰舟千帆 2022-01-11 12:36 采纳率: 76.2%
浏览 25
已结题

C语言链表的相关问题,可能有些地方忽略掉了。

在vscode上运行出现段错误,不知道为啥,数据录入后输出失败,求帮忙改一下,并说明一下原因,感谢。



#include<stdio.h>
#include<windows.h>
//头插法建立单链表
struct Book
{
    char title[128];
    char author[40];
    struct Book *next;
};
void getInput(struct Book *book)
{
    printf("please input the title:\n");
    scanf("%s",book->title);
    printf("please input the author:\n");
    scanf("%s",book->author);

}
void addBook(struct Book **library)
{
    struct Book *book,*temp;
    book = (struct Book *)malloc(sizeof(struct Book));
    if(book == NULL)
    {
        printf("failed to apply for space");
        exit(1);
    }
    getInput(book);
    if(*library!=NULL)
    {
        temp = *library;
        *library = book;
        book->next = temp;
    }
    else{
        //如果是空的单链表,那么就把library指向新的节点
        *library = book;
        book->next = NULL;

    }
};
void printLibrary(struct Book *library)
{
    struct Book *book;
    int count =1;
    while(book->next !=NULL)
    {
        printf("Book%d",count);
        printf("Book_title:\n",book->title);
        printf("Book_author\n",book->author);
        book = book->next;
        count++;
    }

}
void releaseLibrary(struct Book *library)
{
    while (library!=NULL)
    {
        /* code */
        free(library);
       
    }
    
}
int main()
{
    int ch;
    struct Book *library = NULL;
    
    while(1)
    {
        printf("if you input the message of book?(Y/N)");
        
        do
        {
            /* code */
            ch =getchar();
        } while (ch!='Y'&&ch!='N');
        if(ch=='Y')
        {
            addBook(&library);
        }
        else{
            break;
        }
    }
    printf("If you print the message of book?(Y/N)");
    do
    {
        ch = getchar();

    }while(ch!='Y'&&ch!='N');
    if(ch == 'Y')
    {
        printLibrary(library);
    }
   

    system("pause'");

}
  • 写回答

1条回答 默认 最新

  • fuill 2022-01-11 13:42
    关注

    修改如下

    
    
    #include<stdio.h>
    //#include<windows.h>
    //头插法建立单链表
    struct Book
    {
        char title[128];
        char author[40];
        struct Book *next;
    };
    void getInput(struct Book *book)
    {
        printf("please input the title:\n");
        getchar();
        scanf("%s",book->title);
        printf("please input the author:\n");
        getchar();
        scanf("%s",book->author);
    }
    void addBook(struct Book **library)
    {
        struct Book *book,*temp;
        book = (struct Book *)malloc(sizeof(struct Book));
        if(book == NULL)
        {
            printf("failed to apply for space");
            exit(1);
        }
        getInput(book);
        if(*library!=NULL)
        {
            temp = *library;
            *library = book;
            book->next = temp;
        }
        else 
        {
            *library = book;
            book->next = NULL;
        }
    }
    void printLibrary(struct Book *book)
    {
        //struct Book *book=Book *library;
        int count =1;
        while(book !=NULL)
        {
            printf("Book%d",count);
            printf("Book_title:%s\n",book->title);
            printf("Book_author:%s\n",book->author);
            book = book->next;
            count++;
        }
    }
    void releaseLibrary(struct Book *library)
    {
        while (library!=NULL)
        {
            free(library);
        }
    }
    int main()
    {
        int ch;
        struct Book *library = NULL;
        while(1)
        {
            printf("if you input the message of book?(Y/N)");
            do
            {
                ch =getchar();
            }
            while(ch!='Y'&&ch!='N');
            if(ch=='Y')
            {
                addBook(&library);
            }
            else 
            {
                break;
            }
        }
        printf("If you print the message of book?(Y/N)");
        do
        {
            ch = getchar();
        } while(ch!='Y'&&ch!='N');
        if(ch == 'Y')
        {
            printLibrary(library);
        }
        //system("pause'");
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月19日
  • 已采纳回答 1月11日
  • 创建了问题 1月11日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来