如何在 C++ 中将链表中的 int 指针值相加?

发布于 2024-10-06 19:26:04 字数 1813 浏览 2 评论 0原文

我做这个家庭作业已经有一段时间了,但我不知道我做错了什么。我的程序应该如何工作:
用户输入任意数量的正数,
数字位于链接列表中,
输入的数字应相加,
除以输入的数字数量,
得出平均值,
然而,它并没有按照我的预期进行,我已经玩了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

王权女流氓 2024-10-13 19:26:04

现在,您正在将数据结构(链接列表)与您打算使用它的用途混合在一起。考虑将您的逻辑分为:

  • 您的 I/O 代码。
  • 链表的实现。
  • 一个接受链表并计算平均值的函数。

Right now you're mixing your data structure (linked list) with what you intend to use it for. Consider splitting your logic into:

  • Your I/O code.
  • The linked list implementation.
  • A function that takes a linked list, and computes the average.
赠佳期 2024-10-13 19:26:04

你那里还有很多其他的东西,你没有说你的代码是做什么的,但我会做这样的事情(未经测试):

 int count = 0;
 int total = 0;

 for (ptr = head_ptr; ptr != NULL; ptr = ptr->next)
 {
     total += ptr->number;
     count++;
 }

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):

 int count = 0;
 int total = 0;

 for (ptr = head_ptr; ptr != NULL; ptr = ptr->next)
 {
     total += ptr->number;
     count++;
 }
和我恋爱吧 2024-10-13 19:26:04

我知道这对你的作业没有帮助,但这里有一个 C++ STL 程序可以满足你的要求:

  • 用户想要多少输入
  • 数字存储在链接列表中
  • 数字相加
  • 计算并显示平均值

祝你好运班级。

#include <list>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <numeric>

int main()
{
  std::list<double> l;

  std::copy(std::istream_iterator<double>(std::cin),
    std::istream_iterator<double>(),
    std::insert_iterator<std::list<double> >(l, l.begin()));

  size_t size = l.size();
  if(size)
    std::cout << std::accumulate(l.begin(), l.end(), 0.0) / l.size()
      << std::endl;
}

I know this won't help you with your homework, but here is a C++ STL program that satisfies your requirements:

  • As many inputs as the user desires
  • Numbers are stored in a linked list
  • Numbers are added up
  • Calculates and displays average

Good luck with your class.

#include <list>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <numeric>

int main()
{
  std::list<double> l;

  std::copy(std::istream_iterator<double>(std::cin),
    std::istream_iterator<double>(),
    std::insert_iterator<std::list<double> >(l, l.begin()));

  size_t size = l.size();
  if(size)
    std::cout << std::accumulate(l.begin(), l.end(), 0.0) / l.size()
      << std::endl;
}

~

贱人配狗天长地久 2024-10-13 19:26:04

抱歉:会附上评论来提出这个介绍性问题。但显然你需要比我目前更高的代表。

@布兰登。我能否让您清楚地说明是这些函数:

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文