邦仔 2018-10-23 12:44 采纳率: 33.3%
浏览 498

C语言!!!!单链表 问题

  #include"stdio.h"
 #include"stdlib.h"
void getInput(struct Book *book); 
void addBook(struct Book **library);
void printLibrary(struct Book *library);
void releaseLibrary(struct Book *library);
 struct Book 
 {
    char name[128];
    char author[40];
    int money;
    struct Book *next;
 };
 void getInput(struct Book *book)
 {
    printf("请输入书名:");
    scanf("%s",book->name);
    printf("请输入作者:");
    scanf("%s",book->author);
    printf("请输入售价:");
    scanf("%d",book->money);

 }
 void addBook(struct Book **library)
 {
    struct Book *book,*temp;
    book = (struct Book *)malloc(sizeof(struct Book));
    if (book == NULL)
    {
        printf("内存分配失败!\n");
        exit(1);
     }
    getInput(book);
    if(*library != NULL)
    {
        temp = *library;
        *library = book;
        book->next = temp;
    }
    else
    {
        *library = book;
        book->next = NULL;      
    }

 }
 void printLibrary(struct Book *library)
 {
    struct Book *book;
    int count = 1 ;
    book = library;
    while (book != NUll)
    {
        printf("Book%d:",count)
        printf("书名:%s",book->name);
        printf("作者:%s",book->author);
        printf("售价:%d",book->money);
        book = book->next;
        count ++ ;

     }
 }
 void releaseLibrary(struct Book *library)
{
    while (library!=NULL)
    {
        library=library->next;
        free(library);

    }
}
 int main()
 {
    struct Book *library = NULL;
    char ch; 
    while(1)
    {
        printf("请问是否需要录入书籍信息(Y/N)");
        do
        {
            ch = getchar();
         } while (ch != 'Y' && ch!= 'N');
         if(ch == 'Y')
         {
            addBook(&library);
         }
         else 
         {
            break;
         }
    }
    printf("请问是否需要打印图书信息(Y/N)");
    do
    {
        ch=getchar();
     } while (ch!='Y'&& ch!= 'N');
     if(ch == 'Y')
     {
        printLibrary(library);
     }
     releaseLibrary(library);

//  addBook(&library);
    return 0;
 }

上传不了图片。。。但是我编译 巨多报错,。。。

各位老哥帮忙看一下哪里错了

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-10-23 12:56
    关注
     // Q703963.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include"stdio.h"
     #include"stdlib.h"
    void getInput(struct Book *book); 
    void addBook(struct Book **library);
    void printLibrary(struct Book *library);
    void releaseLibrary(struct Book *library);
     struct Book 
     {
        char name[128];
        char author[40];
        int money;
        struct Book *next;
     };
     void getInput(struct Book *book)
     {
        printf("请输入书名:");
        scanf("%s",book->name);
        printf("请输入作者:");
        scanf("%s",book->author);
        printf("请输入售价:");
        scanf("%d",&book->money);
    
     }
     void addBook(struct Book **library)
     {
        struct Book *book,*temp;
        book = (struct Book *)malloc(sizeof(struct Book));
        if (book == NULL)
        {
            printf("内存分配失败!\n");
            exit(1);
         }
        getInput(book);
        if(*library != NULL)
        {
            temp = *library;
            *library = book;
            book->next = temp;
        }
        else
        {
            *library = book;
            book->next = NULL;      
        }
    
     }
     void printLibrary(struct Book *library)
     {
        struct Book *book;
        int count = 1 ;
        book = library;
        while (book != NULL)
        {
            printf("Book%d:",count);
            printf("书名:%s",book->name);
            printf("作者:%s",book->author);
            printf("售价:%d\n",book->money);
            book = book->next;
            count ++ ;
    
         }
     }
     void releaseLibrary(struct Book *library)
    {
        struct Book *t;
        while (library!=NULL)
        {
            t=library;
            library=library->next;
            free(t);
    
        }
    }
     int main()
     {
        struct Book *library = NULL;
        char ch; 
        while(1)
        {
            printf("请问是否需要录入书籍信息(Y/N)");
            do
            {
                ch = getchar();
             } while (ch != 'Y' && ch!= 'N');
             if(ch == 'Y')
             {
                addBook(&library);
             }
             else 
             {
                break;
             }
        }
        printf("请问是否需要打印图书信息(Y/N)");
        do
        {
            ch=getchar();
         } while (ch!='Y'&& ch!= 'N');
         if(ch == 'Y')
         {
            printLibrary(library);
         }
         releaseLibrary(library);
    
    //  addBook(&library);
        return 0;
     }
    
    

    图片说明

    如果问题得到了解决,麻烦点下我回答右边的采纳,谢谢

    评论

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?