LeaflLit 发表于 2022-2-23 22:46:32

S1E46 链表

为什么改成这样不行

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

大马强 发表于 2022-2-23 23:09:31

这不是二维数组规范写法呗
好好遵守人家的规则呀

ba21 发表于 2022-2-27 18:37:22

把代码上全,问题 描述清楚。
你复制代码上来的时候不要用代码格式。(你的代码中有代码格式不兼容的字符)

LeaflLit 发表于 2022-2-27 23:31:08

ba21 发表于 2022-2-27 18:37
把代码上全,问题 描述清楚。
你复制代码上来的时候不要用代码格式。(你的代码中有代码格式不兼容的字符 ...

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

LeaflLit 发表于 2022-2-27 23:31:52

ba21 发表于 2022-2-27 18:37
把代码上全,问题 描述清楚。
你复制代码上来的时候不要用代码格式。(你的代码中有代码格式不兼容的字符 ...

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

人造人 发表于 2022-2-27 23:38:55

LeaflLit 发表于 2022-2-27 23:31
这个是我改过的
void addBook(struct Book *library)
{


把代码上全,问题 描述清楚。

就是要你发完整代码,发完整代码是什么意思?
就是字面意思

LeaflLit 发表于 2022-2-28 09:26:02

人造人 发表于 2022-2-27 23:38
就是要你发完整代码,发完整代码是什么意思?
就是字面意思

{:10_250:}

#include <stdio.h>
#include <stdlib.h>

struct Book
{
        char title;
        char author;
        struct Book *next;
};

void getInput(struct Book *book)
{
        printf("请输入书名:");
        scanf("%s", book->title);
        printf("请输入作者:");
        scanf("%s", book->author);
}

void addBook(struct Book *library)
{
        struct Book *book, *temp;
       
        book = (struct Book *)malloc(sizeof(struct Book));
        if(book == 0)
        {
                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->title);
                printf("作者:%s", book->author);
                book = book->next;
                count++;
                putchar('\n');
        }
       
        putchar('\n');
}


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;
        int 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);
       
        return 0;
}

人造人 发表于 2022-2-28 10:38:06

LeaflLit 发表于 2022-2-28 09:26
#include
#include



要改变一级指针的指向,需要用二级指针
一级指针无法改变一级指针的指向
$ cat main.c
#include <stdio.h>

int x;

void func(int *p) {
    printf("x: %p\n", &x);
    printf("p: %p\n", p);
    p = &x;
    printf("p: %p\n", p);
}

int main(void) {
    int *a = NULL;
    printf("x: %p\n", &x);
    printf("a: %p\n", a);
    func(a);
    printf("a: %p\n", a);
    return 0;
}
$ gcc-debug -o main main.c
$ ./main
x: 0x564470ec15a0
a: (nil)
x: 0x564470ec15a0
p: (nil)
p: 0x564470ec15a0
a: (nil)
$

LeaflLit 发表于 2022-2-28 11:09:02

人造人 发表于 2022-2-28 10:38
要改变一级指针的指向,需要用二级指针
一级指针无法改变一级指针的指向

不能通过形参改变实参的指向的地址是这个意思吗

人造人 发表于 2022-2-28 11:34:40

LeaflLit 发表于 2022-2-28 11:09
不能通过形参改变实参的指向的地址是这个意思吗

页: [1]
查看完整版本: S1E46 链表