2016-04-22 97 views
0

下面的程序理論上應該創建一個鏈接列表,然後用戶可以添加,顯示或減去(不相關)。目前我的問題是試圖讓我的鏈表上打印多個值。鏈接列表更改節點而不是添加另一個

主要問題我很肯定來自函數add_node,但我似乎無法讓它根本改變。 (有一個減法類選項,但我省略了該函數,因爲它沒有關係。)

我需要更改哪些內容才能添加到鏈接列表中?

任何幫助,將不勝感激

#include <iostream> 
#include <cstring> 

struct ToDoList 
{ 
    std::string start_time; 
    std::string activity_name; 
    int time_for_activity; 
    ToDoList *next; 
}; 

void add_node(ToDoList * & head, std::string start, std::string activity,int time) 
{ 
    ToDoList* temp; 
    head = new ToDoList; 
    temp = new ToDoList; 
    head-> start_time = start; 
    head-> activity_name = activity; 
    head-> time_for_activity = time; 
    head-> next = head; 
    head = temp; 
    temp = temp->next; 
    head->next=NULL; 
}//in theory this should add another node to the list but it isn't working 

int main() 
{ 
    int ans, i = 0; 
    std::string start; 
    std::string activity; 
    int time; 
    ToDoList* head; 
    ToDoList* a; 
    std::cout << "Enter the start time in HH:MM am format: "; 
    std::getline(std::cin, start); 
    std::cout << "Enter the activity name: "; 
    std::getline(std::cin, activity); 
    std::cout << "Enter the time for the activity: "; 
    std::cin >> time; 
    //~ add_node(head); 
    add_node(head,start,activity,time); 

    std::cout << "\nWelcome to the Linked list menu! What would you like to do? \n0 Add a Node \n1 Display the list \n2 Delete a node \n3 Quit \n"; 
    std::cin >> ans; 
    while(ans != 3)//This should print all the values in the list 
    { 
     std::cin.ignore(); 
     if(ans == 0) 
     { 
      std::cout << "Enter the start time in HH:MM am format: "; 
      std::getline(std::cin, start); 
      std::cout << "Enter the activity name: "; 
      std::getline(std::cin, activity); 
      std::cout << "Enter the time for the activity: "; 
      std::cin >> time; 
      add_node(head,start,activity,time); 

     } 
     else if(ans == 1) 
     { 
      a = new ToDoList;//creates new pointer for while loop 
      a = head; 
      while(a != NULL)//loop used for printing 
      { 
       std::cout << i << " " << a->start_time << " " << a->activity_name << " " << a->time_for_activity << "\n"; 
       a = a -> next; 
       i++; 
      } 
      i = 0;//resets integer i 
     } 
     std::cout << "\nWelcome to the Linked list menu! What would you like to do? \n0 Add a Node \n1 Display the list \n2 Delete a node \n3 Quit \n"; 
     std::cin >> ans; 
    } 
    return 0; 
} 

到目前爲止,它只能打印以下內容:

head = new ToDoList; 

您可以:

Enter the start time in HH:MM am format: 10:00 am 
Enter the activity name: Office Hours 
Enter the time for the activity: 30 

Welcome to the Linked list menu! What would you like to do? 
0 Add a Node 
1 Display the list 
2 Delete a node 
3 Quit 
0 
Enter the start time in HH:MM am format: 11:00 am 
Enter the activity name: Lunch 
Enter the time for the activity: 60 

Welcome to the Linked list menu! What would you like to do? 
0 Add a Node 
1 Display the list 
2 Delete a node 
3 Quit 
1 
0 0 
test 
+0

教程[here](http://pastebin.com/DXunz58Q) – sp2danny

回答

1

喲,每次添加新節點時都不需要在頭部創建新節點。此外,即使您正確地修改了這一點,每次添加新節點時都會錯誤地將NULL分配給下一個頭,從而阻止您訪問比第一個節點更多的列表中的任何成員。最後,temp-> next to temp的分配是另一個問題的根源,它會導致您無法訪問第一個以外的列表元素。

下面是您的代碼版本,刪除了不正確的語句。它似乎工作,你可能會看到here

void add_node(ToDoList * & head, std::string start, std::string activity,int time) 
{ 
    ToDoList* temp; 
    temp = new ToDoList; 
    temp-> start_time = start; 
    temp-> activity_name = activity; 
    temp-> time_for_activity = time; 
    temp-> next = head; 
    head = temp; 
} 

而且,雖然它並非主要涉及到你的問題,你可能在你的代碼的版本,我與Ideone試驗看,我初始化頭爲NULL,在主,以及移除不必要的動態分配給指針a。初始化頭是必需的,這樣您的一些循環可以正確終止(即不會終止),同時刪除分配對於防止內存泄漏至關重要。

0

您在add_node每次調用改變head只爲一個空列表做這個。

0

您每次打電話給add_node都會覆蓋頭指針,這意味着您每次都會丟失列表。你安排它的方式也是不正確的,應該改爲(假設你添加到列表的頭部,而不是結束)

void add_node(ToDoList * & head, std::string start, std::string activity,int time) 
{ 
    ToDoList* temp = new ToDoDoList; 
    temp -> start_time = start; 
    temp -> activity_name = activity; 
    temp -> time_for_activity = time; 
    temp -> next = head; 
    head = temp; 
} 

爲了使這項工作,你不應該在main分配頭的指針而是將其初始化爲NULL,以便第一次調用成功確保列表的末尾標記爲NULL。即

int main() 
{ 
    int ans, i = 0; 
    std::string start; 
    std::string activity; 
    int time; 
    ToDoList* head = NULL; 
    .... 

此外,您的程序中有很多內存泄漏。首先,你永遠不會刪除你的列表,因爲它不是一個雙鏈表,所以你必須遞歸地做使用此功能,並調用它head

void CleanList(ToDoList* node) 
{ 
    if (node == NULL) 
    { 
     return; 
    } 
    CleanList(node->next); 
    delete node; 
} 

此外,在main你做

a = new ToDoList;//creates new pointer for while loop 
a = head; 

這也是內存泄漏,你立即失去了參考剛剛分配的內存。相反,你應該簡單地說

a = head; 

編輯:

修正錯別字和現在測試,以確保代碼在這個答案的作品並不會崩潰。

相關問題