2012-03-31 62 views
0

//美好的一天。這只是我的源代碼的一部分。我的主要困境是print_list函數只打印鏈接列表中用戶的第一個輸入。我似乎無法指出這個問題,因爲這個功能的邏輯似乎是正確的。 insert_list函數似乎也可以正常工作,但我並不確定。print_list函數僅在鏈接列表中打印用戶的第一個輸入

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

//這是節點

typedef struct node 
{ 
     char name[61]; 
     int month; 
     int day; 
     int year; 
     struct node *next; 
}node; 

//這是列表

typedef struct list 
{ 
     node *head; 
     node *tail; 
}list; 

//這通過接受用戶的輸入創建了節點,並將它們在所述節點

node *create_node() 
{ 
     int x; 
     node *data = (node*) malloc(sizeof(node)); 
     printf("Name: "); 
     fgets(data->name, 61, stdin); 
     printf("Birthdate (mm/dd/yyyy): "); 
     scanf("%d%*[/]%d%*[/]%d", &data->month, &data->day, &data->year); 
     getchar(); 
     if ((data->month)==0||(data->month)>=13||(data->day)<=0||(data->day)>=32||(data->year)<=1977||(data->year)>=3001) 
     { 
       while ((data->month)==0||(data->month)>=13||(data->day)<=0||(data->day)>=32||(data->year)<=1977||(data->year)>=3001) 
       { 
         printf("Invalid Input.\n"); 
         printf("Please Enter a Valid Birthdate(mm/dd/yyyy): \n"); 
         scanf("%d%*[/]%d%*[/]%d", &data->month, &data->day, &data->year); 
         getchar(); 
       } 
     } 
     printf("******************************************************************\n"); 
     for (x=0; x<=strlen(data->name); x++) 
     { 
      if (data->name[x]=='\n') 
      { 
        data->name[x]='\0'; 
      } 
    } 
    printf("Birthday reminder for %s is added.\n", data->name); 
    return data; 
} 


list *create_list(list *plist) 
{ 
     plist->head = NULL; 
     plist->tail = NULL; 
     return plist; 
} 

// this inserts在列表中的節點

list *insert_list(list *plist, node *pnode, node *new_node) 
{ 
    if(plist->head==NULL) 
    { 
      plist->head=new_node; 
      new_node->next=NULL; 
    } 
    else 
    { 
      new_node->next = NULL; 
      pnode->next = new_node; 
      plist->tail = new_node; 
    } 
    return plist; 
} 

//此打印列表

list *print_list(list *plist) 
{ 
     node *current = plist->head; 
     int i; 
     for(i=1;current!=NULL;i++) 
     {  
      printf("[%d] %s\n",i ,current->name); 
      printf("Birth Date: %d/%d/%d\n", current->month, current->day, current->year); 
      current=current->next; 
    } 
} 

//這解除分配列表

list *free_list(list *List) 
{ 
    node *current = List->head; 
    node *temp = NULL; 
    while(current != NULL) 
    { 
      temp = current; 
      current = current->next; 
      free(temp); 
    } 

    List->head = NULL; 
    List->tail = NULL; 
} 

//這是主要的

int main(void) 
{ 
     list* List = (list*) malloc(sizeof(list)); 
     List = create_list(List); 
     char x; 
     node *data = (node *) malloc(sizeof(node)); 
     printf("******************************************************************\n"); 
     printf("     ADD BIRTHDAY REMINDER FORM\n"); 
     printf("******************************************************************\n"); 
     List = insert_list(List, data, create_node(data)); 
     printf("Would you like to add another(y/n)?\n"); 
     scanf("%c", &x); 
     if (x=='y') 
     { 
       while (x=='y') 
       { 
         if (x=='y') 
         { 
           getchar(); 
          printf("******************************************************************\n"); 
           node *data = (node *) malloc(sizeof(node)); 
           List = insert_list(List, data, create_node(data)); 
           printf("Would you like to add another(y/n)?\n"); 
           scanf("%c", &x); 
         } 
       } 
     } 
    print_list(List); 
    free(List); 
    return 0; 

} 

//此代碼已準備好進行編譯

回答

1

我不明白,你打算通過pnodeinsert_list,我想,有錯誤。可能,你的意思是像之前的節點。但是,尾部已經是前一個節點。另外,您在那裏使用一個空節點,即使您爲create_node中的節點分配內存。也許,下面的代碼會更合適:

list *insert_list(list *plist, node *new_node) 
{ 
    if(plist->head==NULL) 
    { 
      plist->head=new_node; 
      plist->tail=new_node; 
      new_node->next=NULL; 

    } 
    else 
    { 
      new_node->next = NULL; 
      plist->tail->next = new_node; 
      plist->tail = new_node; 
    } 
    return plist; 
} 
+0

是的,我用它作爲前一個節點。我嘗試用你的替換我的插入列表代碼,但它仍然只打印其中一個輸入。 – user123456098 2012-03-31 12:31:58

+0

好吧,我想你也必須修改調用代碼。順便說一句,這是印刷,仍然是第一個或最後一個? – Matthias 2012-03-31 13:19:29

+0

我很抱歉,你的代碼工作正常,我正在編譯錯誤的程序:)))謝謝你,上帝保佑! :D – user123456098 2012-03-31 13:24:06

1

你確定你粘貼了你的實際源代碼嗎? 在List = insert_list(List, data, create_node(data));你叫create_node(data)但你的函數node *create_node()採取零參數。我不明白`list * insert_list(list * plist,node * pnode,node * new_node)中node *pnode參數的用途

打印功能對我來說似乎是合法的。嘗試使用-Wall -Wextra -Werror進行編譯以進行額外的錯誤檢測。 `

+0

是我做的,但那裏的錯誤沒有解決打印功能困境 – user123456098 2012-03-31 12:19:57

+0

好,那create_node(data)好像是壞的調用,我認爲下面的代碼從@Matthias解決了插入問題。嘗試使用@Matthias函數與'List = insert_list(List,create_node());' – 2012-03-31 12:29:36

+0

謝謝你告訴我關於create_node(data)是一個糟糕的調用。但我嘗試了兩種建議,但打印功能仍然只打印一個輸出 – user123456098 2012-03-31 12:37:43

1

我不明白爲什麼需要爲insert_list第二個參數pnode。 如果你只是想打印的所有元素的列表,我想作以下修改可以解決您的問題:

首先,添加一行plist->tail=new_nodeinsert_list第一if子句。 其次,將insert_list的第二個參數從data更改爲main中的List->tail

我想指出另一個不重要的事情。你真的需要if (x == 'y)的條件main?我不知道它爲什麼在那裏。

+0

嗨,我已經拿出主數據參數,因爲另一個人說,它沒有任何目的作爲創建節點功能它自己創建自己的節點數據。 – user123456098 2012-03-31 12:50:46