链接列表更改节点而不是添加另一个节点

时间:2016-04-22 09:51:52

标签: c++ linked-list

下面的程序理论上应该创建一个链表,然后用户可以添加,显示或减去(不相关)。目前我的问题是尝试在我的链表上打印多个值。

我非常肯定的主要问题来自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;
}

到目前为止,它只打印以下内容:

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

3 个答案:

答案 0 :(得分:1)

每次添加新节点时,都不需要在头部创建新节点。此外,即使您只是纠正了这一点,每次添加新节点时,都会错误地将NULL分配给下一个节点,从而阻碍您对列表中任何成员的访问,而不是第一个节点。最后,你在temp旁边的temp-&gt;的分配是另一个问题来源,它会导致你无法访问除第一个之外的列表元素。

以下是您的代码版本,其中删除了不正确的语句。它似乎有用,你可能会看到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上进行过实验的代码版本中所看到的那样,我在main中初始化为NULL,并删除了不必要的动态分配给指针a。初始化头是必需的,这样你的一些循环可以正常终止(即与永不终止相反),而删除分配对于防止内存泄漏至关重要。

答案 1 :(得分:0)

每次调用head时都会更改add_node

 head = new ToDoList;

您只能为空列表执行此操作。

答案 2 :(得分: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;

修改

修正了拼写错误,现在已经过测试,以确保此答案中的代码有效并且不会崩溃。