简体   繁体   English

链表数组麻烦C ++

[英]Array of Linked Lists Trouble C++

First off, I am very new to C++ and have worked primarily with languages such as Java, JavaScript, C#, and Scala. 首先,我是C ++的新手,并且主要使用Java,JavaScript,C#和Scala等语言。

I am trying to write a program that will take a number from the console, which will determine the number of teams (in form of linked lists (Ex. numTeams = 5; means 5 linked lists)) and then will take the following input such that the first value will be added to the first linked list, then the second value to the second linked list, and so on until all i values are distributed to all all n teams. 我正在尝试编写一个将从控制台获取数字的程序,该程序将确定团队的数量(以链表的形式(例如numTeams = 5;表示5个链表)),然后将使用以下输入,例如将第一个值添加到第一个链接列表,然后将第二个值添加到第二个链接列表,依此类推,直到将所有i个值分配给所有n个团队。 Also, when all linked lists are filled with with a an equal amount of values it will start again at the first linked list and go through them again adding the values to each list. 同样,当所有链接列表都填充有相等数量的值时,它将再次从第一个链接列表开始,然后再次遍历它们,将值添加到每个列表中。

I know I have to make an array of linked lists, but am unsure how to go further than where I am with distributing the values in the fashion I stated. 我知道我必须制作一系列的链表,但是不确定如何按照我所说的方式分配值。

Any help would be very much appreciated! 任何帮助将不胜感激!

Here is what I have so far: 这是我到目前为止的内容:

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;

class balanceTeams{

    // Struct inside the class LinkedList
    struct Node {

          int x;
          Node *next;

     };


public:

  // constructor
  balanceTeams(){

       head = NULL; // set head to NULL

    }

  // New value at the beginning of the list
  void addUnit(int val){

        Node *n = new Node();   // create new Node
        n->x = val;             // set value
        n->next = head;         // make the node point to the next node.
        //  If the list is empty, this is NULL
        head = n;               // head point at the new node.

     }

  // returns the first element in the list and deletes the Node.
  int popUnit(){

          Node *n = head;
          int ret = n->x;

          head = head->next;
          delete n;
          return ret;

     }

  //void swapUnit()
  //void moveUnit()

private:

    Node *head; // pointer to the first Node

  };


int main() {


    string line;
    int numTeams = 0;
    int lines = 0;
    vector<balanceTeams> vect;
    //int team_size = lines / numTeams;


  getline(cin, line);
      numTeams = atoi (line.c_str());
      vect.resize(numTeams);
      cout << "Num teams: " << numTeams << endl;


  while (getline(cin, line)) {

      cout << "Unit " << line << endl;

  }

  return 0;

}

Edit: Three Errors: 编辑:三个错误:

balanceTeams.cpp: In function ‘int main()’:
balanceTeams.cpp:72:23: error: no matching function for call to  
  ‘balanceTeams::addUnit(std::string&)’
   vect[i].addUnit(line);
                       ^
balanceTeams.cpp:72:23: note: candidate is:
balanceTeams.cpp:25:7: note: void balanceTeams::addUnit(int)
  void addUnit(int val){
       ^
balanceTeams.cpp:25:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘int’

All that seems to be missing from your code is: 您的代码似乎缺少的全部是:

1) Some error checking. 1)一些错误检查。 The number of entered teams must be at least one. 参赛队伍必须不少于一队。 If someone enters 0 or a negative value, display an error message, and bail out. 如果有人输入0或负值,则显示错误消息,然后退出。

2) Initialize 2)初始化

size_t i=0;

before the existing loop. 在现有循环之前。

3) Inside the loop, parse the existing value, then call 3)在循环内部,解析现有值,然后调用

vect[i].addUnit(new_value);

4) Increment the index at the end of the loop, for the next iterator 4)对于下一个迭代器,在循环末尾增加索引

i=(i+1) % vect.size();

That seems to be it. 好像就是这样。 Each time around the loop, you'll be adding a value to the next vector. 每次循环时,您都要向下一个向量添加一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM