如何在 C++ 中将链表中的 int 指针值相加?
我做这个家庭作业已经有一段时间了,但我不知道我做错了什么。我的程序应该如何工作:
用户输入任意数量的正数,
数字位于链接列表中,
输入的数字应相加,
除以输入的数字数量,
得出平均值,
然而,它并没有按照我的预期进行,我已经玩了 3 个多小时了。我会联系我的老师,但她还没有回复我的上一条消息,我立即需要帮助。提前致谢。
注意:我需要遍历列表,将所有输入的数字相加,并统计节点的数量。
#include <iostream>
using namespace std;
int num, total, num_entries = 1;
struct number_node
{
int number;
number_node *next;
};
number_node *head_ptr;
number_node *current_ptr;
int get_number_data(int &number);
void add_node(int &number);
void move_current_to_end();
void display_avg();
void delete_list();
int main()
{
if(get_number_data(num))
{
head_ptr = new number_node;
head_ptr->number = num;
head_ptr->next = NULL;
while(get_number_data(num))
{
add_node(num);
}
display_avg();
delete_list();
}
system("pause");
return 0;
}
int get_number_data(int &number)
{
int keep_data = 1;
cout << "Enter a positive number (Enter a negative number to stop): ";
cin >> num;
if(num < 0)
{
keep_data = 0;
}
return(keep_data);
}
void add_node(int &number)
{
number_node *new_rec_ptr;
new_rec_ptr = new number_node;
new_rec_ptr->number = num;
new_rec_ptr->next = NULL;
move_current_to_end();
current_ptr->next = new_rec_ptr;
}
void move_current_to_end()
{
current_ptr = head_ptr;
num_entries++;
while(current_ptr->next != NULL)
{
current_ptr = current_ptr->next;
total = current_ptr->number + total;
}
}
void display_avg()
{
current_ptr = head_ptr;
cout << "Average = " << total / num_entries << endl;
}
void delete_list()
{
number_node *temp_ptr;
current_ptr = head_ptr;
do
{
temp_ptr = current_ptr->next;
delete current_ptr;
current_ptr = temp_ptr;
}
while(temp_ptr != NULL);
}
I've been working at this homework assignment for awhile and I can't figure out what I'm doing wrong. How my program is suppose to work:
User enters as many positive numbers as they so desire,
Numbers are in a linked list,
Numbers entered should be added up,
Divide by the amount of numbers entered,
Resulting in the average,
However, it's not working out as I had intended and I've been playing with this for over 3 hours now. I'd contact my teacher but she hasn't responded to my last message still and I need assistance right away. Thanks in advance.
Note: I need to traverse the list to add up all the entered numbers and count the number of nodes.
#include <iostream>
using namespace std;
int num, total, num_entries = 1;
struct number_node
{
int number;
number_node *next;
};
number_node *head_ptr;
number_node *current_ptr;
int get_number_data(int &number);
void add_node(int &number);
void move_current_to_end();
void display_avg();
void delete_list();
int main()
{
if(get_number_data(num))
{
head_ptr = new number_node;
head_ptr->number = num;
head_ptr->next = NULL;
while(get_number_data(num))
{
add_node(num);
}
display_avg();
delete_list();
}
system("pause");
return 0;
}
int get_number_data(int &number)
{
int keep_data = 1;
cout << "Enter a positive number (Enter a negative number to stop): ";
cin >> num;
if(num < 0)
{
keep_data = 0;
}
return(keep_data);
}
void add_node(int &number)
{
number_node *new_rec_ptr;
new_rec_ptr = new number_node;
new_rec_ptr->number = num;
new_rec_ptr->next = NULL;
move_current_to_end();
current_ptr->next = new_rec_ptr;
}
void move_current_to_end()
{
current_ptr = head_ptr;
num_entries++;
while(current_ptr->next != NULL)
{
current_ptr = current_ptr->next;
total = current_ptr->number + total;
}
}
void display_avg()
{
current_ptr = head_ptr;
cout << "Average = " << total / num_entries << endl;
}
void delete_list()
{
number_node *temp_ptr;
current_ptr = head_ptr;
do
{
temp_ptr = current_ptr->next;
delete current_ptr;
current_ptr = temp_ptr;
}
while(temp_ptr != NULL);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
现在,您正在将数据结构(链接列表)与您打算使用它的用途混合在一起。考虑将您的逻辑分为:
Right now you're mixing your data structure (linked list) with what you intend to use it for. Consider splitting your logic into:
你那里还有很多其他的东西,你没有说你的代码是做什么的,但我会做这样的事情(未经测试):
You've got a lot of other stuff there and you didn't say what your code does, but I'd do something like this (untested):
我知道这对你的作业没有帮助,但这里有一个 C++ STL 程序可以满足你的要求:
祝你好运班级。
~
I know this won't help you with your homework, but here is a C++ STL program that satisfies your requirements:
Good luck with your class.
~
抱歉:会附上评论来提出这个介绍性问题。但显然你需要比我目前更高的代表。
@布兰登。我能否让您清楚地说明是这些函数:
int get_number_data(int &number)
void add_node(int &number)
void move_current_to_end()
void display_avg()
并且只有这些您才可以使用? (我引用你的话:“我只需要让它计算出使用这些函数的节点总数和数量”
如果是这样。为什么?你的讲师是否指定了它们?
Apologies: would have attached a comment to ask this introductory question. But apparently you need a higher rep than i currently have to do so.
@Brandon. Can i get you to clearly state that it is these functions:
int get_number_data(int &number)
void add_node(int &number)
void move_current_to_end()
void display_avg()
and only these that you are allowed to use? (And i quote you: "I just have to have it figure out the total and and # of nodes using those functions"
If so. Why? Have they been specified by your lecturer?