2017-05-29 54 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