2016-04-22 64 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; 

编辑:

修正错别字和现在测试,以确保代码在这个答案的作品并不会崩溃。