鱼C论坛

 找回密码
 立即注册
查看: 1835|回复: 1

单链表1相关,创建失败

[复制链接]
发表于 2021-3-28 11:07:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
照着小甲鱼的视频打的,但是出了些问题。
#include <stdio.h>
#include <stdlib.h>

struct Book
{
    char title[128];
    struct Book *next;
};

void getInput(struct Book *book)
{
    printf("书名:");
    scanf("%s", book->title);
}

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 *Book)
{
    struct Book *book;
    int count = 1;
    
    book = library;       
    while (book != NULL)
    {
        printf("book%d\n", count);
        printf("书名:%s", book->title);
        book = book->next;
        count++;
    }
    
}

void releaseLibrary(struct Book *library)
{
    struct Book *temp;

    while (*library != NULL)
    {
        temp = *library;
        *library = (*library)->next;
        free(temp);
    }

}

int main(void)
{
    struct Book *library = NULL;


    addbook(&library);
    printLibrary(library);
    releaseLibrary(library);

    return 0;
}


然后报错了

  
cpjiayu.cpp: In function 'void printLibrary(Book*)':
cpjiayu.cpp:46:12: error: 'library' was not declared in this scope
   46 |     book = library;
      |            ^~~~~~~
cpjiayu.cpp: In function 'void releaseLibrary(Book*)':
cpjiayu.cpp:61:21: error: no match for 'operator!=' (operand types are 'Book' and 'long long int')
   61 |     while (*library != NULL)
      |            ~~~~~~~~ ^~
      |            |
      |            Book
cpjiayu.cpp:63:16: error: cannot convert 'Book' to 'Book*' in assignment
   63 |         temp = *library;
      |                ^~~~~~~~
      |                |
      |                Book
cpjiayu.cpp:64:30: error: base operand of '->' has non-pointer type 'Book'
   64 |         *library = (*library)->next;
      |                              ^~

视频里小甲鱼直接就book = library 也没什么问题呀,为什么我自己就要引用点啥呢?
后面两个错误就完全不知道因为什么了。也没弄明白自己哪没学明白。求大佬们指点一波
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-28 11:08:34 | 显示全部楼层
困扰好久了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 01:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表