gpt4 book ai didi

c++ - 链表更改节点而不是添加另一个节点

转载 作者:行者123 更新时间:2023-11-28 05:42:53 26 4
gpt4 key购买 nike

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

我很确定主要问题来自函数 add_node 但我似乎根本无法改变它。 (有一个 subtract class 选项,但我省略了这个函数,因为它不相关。)

我需要更改什么才能将其添加到链表中?

任何帮助将不胜感激

#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

最佳答案

你不需要在每次添加新节点时都在头部创建一个新节点。此外,即使您只是纠正了这一点,每次添加新节点时,您都​​会错误地将 NULL 分配给 head 的下一个,从而阻碍您访问列表中比第一个节点更远的任何成员。最后,您将 temp->next 分配给 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 上试验过的代码版本中看到的那样,我在 main 中将 head 初始化为 NULL,并删除了不必要的动态分配指针 a.需要初始化 head 以便您的某些循环可以正确终止(即与永不终止相反),同时删除分配对于防止内存泄漏至关重要。

关于c++ - 链表更改节点而不是添加另一个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36790529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com