2017-05-29 68 views
2

我有一个名为book的结构和链接到struct book的列表。 我必须将书籍添加到我的书籍列表中,然后才能显示列表。 我创建了一个函数“printBList”来在屏幕上打印我的列表信息。 但是当我试图打印book.id并且程序停止响应时,我做错了什么。 我相信这行“book * b = head-> book”和“printf(”book ID%d \ n“,b-> id);”是错误的,但我无法找到我必须写的话。 有人可以帮我吗?C中的列表中的结构 - 显示链接列表中存在的结构的成员

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

#define MAXSTRING 100 
#define MAXREVIEWS 100 

typedef enum genres{ 
    fiction, 
    sientific, 
    politics 
}; 

typedef struct 
{ 
    char author[MAXSTRING]; 
    char title[MAXSTRING]; 
    enum genres genre; 
    int id; 
    char reviews[MAXREVIEWS][MAXSTRING]; 
} book; 


typedef struct list 
{ 
    int BookID; 
    struct list * next; 
    struct book * book; 
} BList; 


void printBList(BList * head) 
{ 
    BList * current = head; 
    book *b=head->book; 

    while (current != NULL) { 
     printf("List ID:: %d\n", current->BookID); 
     printf("book ID%d\n", b->id); 
     //printf("%d\n", current->BookID);   
     current = current->next; 
    } 
} 


int main() 
{ 
    BList * head = NULL; 
    head = malloc(sizeof(BList)); 
    if (head == NULL) { 
     return 1; 
    } 

    book b={"author 1","title 1",1,22,"review 1"}; 

    head->next = NULL; 
    head = malloc(sizeof(BList)); 

    head->BookID = 1; 
    head->next = malloc(sizeof(BList)); 
    head->next->BookID = 24; 
    head->next->book; 
    head->next->next = NULL; 

    printBList(head); 
    return 0; 
} 
+1

'的printf更 ( “书籍ID%d \ n” 个,B-> ID);' - >'的printf( “书籍ID%d \ n” 个,电流 - >书本 - > id);' – BLUEPIXY

+0

修复如[this](http://ideone.com/WyzypY) – BLUEPIXY

+0

'typedef enum genres {...};'是毫无意义的;它没有命名一个类型。一个好的编译器会对此提出警告。你可能打算'typedef枚举类型{...}流派;' - 这是有道理的。你应该确保你知道如何打开编译器的'最大'警告,并且你应该注意它给你的每一个警告。请记住,编译器知道更多关于C的知识 - 除非它认为代码中存在错误,否则它不会发出警告。 –

回答

5

的问题是在您打印的方式列表,在这里:

while (current != NULL) { 
    printf("List ID:: %d\n", current->BookID); 
    printf("book ID%d\n", b->id); // <-- HERE 
    current = current->next; 
} 

你跟current工作,但你尝试打印b,这是固定的。

你大概的意思是说这个:

printf("book ID%d\n", current->book->id); 

有更多的问题,但:

而且,你的意思是写:

typedef struct book { 
    .. 
} book; 

您的head访问next ,然后创建的空间,在这里:

head->next = NULL; 
head = malloc(sizeof(BList)); 

所以将其更改为:

head = malloc(sizeof(BList)); 
head->next = NULL; 

此外,该行:

head->next->book; 

什么都不做。

接下来,你应该检查你的警告:

Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c 
main.c:7:1: warning: typedef requires a name [-Wmissing-declarations] 
typedef enum genres{ 
^~~~~~~ 
main.c:34:11: warning: incompatible pointer types initializing 'book *' with an 
     expression of type 'struct book *' [-Wincompatible-pointer-types] 
    book *b=head->book; 
     ^~~~~~~~~~~ 
main.c:53:39: warning: suggest braces around initialization of subobject 
     [-Wmissing-braces] 
    book b={"author 1","title 1",1,22,"review 1"}; 
             ^~~~~~~~~~ 
             {   } 
main.c:61:17: warning: expression result unused [-Wunused-value] 
    head->next->book; 
    ~~~~~~~~~~ ^~~~ 
main.c:53:10: warning: unused variable 'b' [-Wunused-variable] 
    book b={"author 1","title 1",1,22,"review 1"}; 
     ^
5 warnings generated. 

当你的typedef的东西,你需要给一个代名词,所以你枚举做到这一点:

typedef enum genres{ 
    .. 
} genres; 

由于reviews是2D阵列,你可以这样做:

book b={"author 1","title 1",1,22, {"review 1"} }; 

此外,请勿使用幻数,在此情况下为1,因为您有用于此目的的enum


然后,我改变了你的main()了一下,添加同一本书两次,用不同的列表ID。将所有内容放在一起,我们得到:

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

#define MAXSTRING 100 
#define MAXREVIEWS 100 

typedef enum genres{ 
    fiction, 
    sientific, 
    politics 
} genres; 

typedef struct book 
{ 
    char author[MAXSTRING]; 
    char title[MAXSTRING]; 
    enum genres genre; 
    int id; 
    char reviews[MAXREVIEWS][MAXSTRING]; 
} book; 


typedef struct list 
{ 
    int BookID; 
    struct list * next; 
    struct book * book; 
} BList; 


void printBList(BList * head) 
{ 
    BList * current = head; 

    while (current != NULL) { 
     printf("List ID:: %d\n", current->BookID); 
     printf("book ID%d\n", current->book->id); 
     current = current->next; 
    } 
} 


int main() 
{ 
    BList * head = NULL; 
    head = malloc(sizeof(BList)); 
    if (head == NULL) { 
     return 1; 
    } 

    book b={"author 1","title 1", sientific ,22,{"review 1"}}; 

    head->next = NULL; 
    head->BookID = 1; 
    head->book = &b; 

    head->next = malloc(sizeof(BList)); 
    head->next->BookID = 24; 
    head->next->book = &b; 
    head->next->next = NULL; 

    printBList(head); 
    return 0; 
} 

输出:

Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
List ID:: 1 
book ID22 
List ID:: 24 
book ID22 

PS:

  1. 不要忘了去分配你动态分配的空间。 这与free()
  2. 检查malloc()是否成功是一个很好的做法,由 检查它的返回值是否为NULL(意思是失败)。阅读 How detect malloc failure?
+0

你好!非常感谢您的迅速回复!我提出了您所建议的更改,但该程序仍然停止响应。 – evangelia

+0

@evangelia你做了一个很好的尝试,因此我用完整的工作代码更新了我的答案。希望有所帮助! =) – gsamaras

+1

你真棒!非常感谢您的帮助!!! – evangelia