马尔代夫海峡 发表于 2021-3-28 11:07:48

单链表1相关,创建失败

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

struct Book
{
    char title;
    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 也没什么问题呀,为什么我自己就要引用点啥呢?
后面两个错误就完全不知道因为什么了。也没弄明白自己哪没学明白。求大佬们指点一波

马尔代夫海峡 发表于 2021-3-28 11:08:34

困扰好久了{:10_266:}
页: [1]
查看完整版本: 单链表1相关,创建失败