2016-06-16 98 views
1

如果我有一個結構,例如允許處理圖書館的書籍,我該如何在此代碼中實現一個列表?結構和列表C

我寫了這段代碼,實際上我做了大部分我需要的事情,但我需要用列表來完成。我還需要閱讀並保存一個文件。我知道如何去做,但並不是確切的代碼放置位置。

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <assert.h> 
#define max 100 
#define LEN 1000 

/* STRUTTURA DATI */ 
typedef struct 
{ 
char author[max]; 
char title[max]; 
char edit[max]; 
int year; 
} Book; 

typedef struct 
{ 
Book elements[LEN]; 
int num_books; 
} List; 


/* PROTOTIPI DI FUNZIONE */ 
int menu(); 
List add_book(); 
void print_list(List l); 
void old_book(List l); 
int search_author(List l, char author[]); 

int main() 
{ 
int choice; 
List LL; 
char author[max]; 
int ishethere; 

while(choice=menu()) 
{ 
    switch(choice) 
    { 
    case 1: 
     system("CLS"); 
     printf("Hey, hey, YOU! LISTEN! Add a book! :3\n"); 
     LL=add_book(); 
     printf("\n"); 
     print_list(LL); 
     system("PAUSE"); 
     break; 
    case 2: 
     system("CLS"); 
     printf("So... Wanna know what's the oldest book?"); 
     old_book(LL); 
     printf("\n"); 
     system("PAUSE"); 
     break; 
    case 3: 
     system("CLS"); 
     printf("Do you want to search an author? Come on lets go and play!"); 
     fflush(stdin); 
     gets(author); 
     ishethere=search_author(LL, author); 
     if(ishethere!=-1) 
      printf("He is in the list\n\n"); 
     else 
      printf("I don't know him. Sorry."); 
     system("PAUSE"); 
     break; 
    case 4: 
     system("CLS"); 
     print_list(LL); 
     system("PAUSE"); 
     break; 
    case 0: 
     return(0); 
    } 
    } 
} 

/* DEFINIZIONI DI FUNZIONI */ 
int menu() 
{ 
int choice; 
printf("* * * Books' Menu * * *\n"); 
printf("* [1] Add Books  *\n"); 
printf("* [2] Search Old Book *\n"); 
printf("* [3] Search Author *\n"); 
printf("* [4] Print Books  *\n"); 
printf("* [0] Bye bye!  *\n"); 
printf("* * * * * * * * * * * *\n"); 
printf("What do you want to do? "); 
scanf("%d", &choice); 
while(choice<0 || choice>4) 
{ 
    printf("DAMMIT! From 0 to 4, can you read?\n"); 
    printf("What do you want to do? "); 
    scanf("%d", &choice); 
    return choice; 
} 
return choice; 
system("PAUSE"); 
} 

List add_book() 
{ 
List l; 
int i; 
printf("How many books do you want to add? "); 
scanf("%d", &l.num_books); 
fflush(stdin); 
for(i=0; i<l.num_books; i++) 
{ 
    printf("Type author: "); 
    gets(l.elements[i].author); 
    printf("Type title: "); 
    gets(l.elements[i].title); 
    printf("Type editor: "); 
    gets(l.elements[i].edit); 
    printf("Type year: "); 
    scanf("%d", &l.elements[i].year); 
    fflush(stdin); 
} 

return l; 
} 

void print_list(List l) 
{ 
int i; 
for (i=0; i<l.num_books; i++) 
{ 
    printf("Book n.%d\n", i+1); 
    printf("Author: %s\n", l.elements[i].author); 
    printf("Title: %s\n", l.elements[i].title); 
    printf("Editor: %s\n", l.elements[i].edit); 
    printf("Year: %d\n\n", l.elements[i].year); 
} 
} 

void old_book(List l) 
{ 
int i; 
Book old=l.elements[0]; 
int index_old=0; 
for (i=0; i<l.num_books; i++) 
{ 
    if(old.year>l.elements[i].year) 
    { 
     old=l.elements[i]; 
     index_old=i; 
    } 
} 
printf("The oldest book is: \n"); 
printf("'%s', by %s, published in %d from %s.\n\n", old.title, old.author, old.year, old.edit); 
} 

int search_author(List l, char author[]) 
{ 
int i, isitthere=-1; 
for (i=0; i<l.num_books; i++) 
    if(strcmp(l.elements[i].author, author)==0) 
    { 
    isitthere=1; 
} 
return isitthere; 
} 

回答

0

你應該創建一個你想要在你的列表中的元素的節點,但不像你做的那樣。當你說只是typedef結構{...}你不能把一個指針括號內,因爲你不能說你想點什麼類型的節點

typedef struct node List; 
struct node 
{ 
Book elements[LEN]; 
int num_books; 
List * next; 
}; 

:我可以給你舉個例子。 當你創建一個列表時,你必須有一個指針指向下一個元素,所以這是正確的方法。如果你想像你一樣,我建議你給一個名稱結構例如

typedef struct node{ 
Book elements[LEN]; 
int num_books; 
struct node * next; 
} List; 

這也應該工作。

+0

我明白了。它是唯一需要修改的嗎? – ennedes

+0

現在您有列表的節點。你創建一個頭指針,並像其他列表一樣創建一個列表。只是當你試圖訪問'Book'的元素時指出它,請注意' - >'和'.'因爲例如如果你想訪問作者,你應該像這樣訪問'head - >元素[i] .author' –

+0

謝謝。我是否必須將上面寫的列表形式放入第一個typedef? – ennedes