Я только начал изучать связанный список, и я пытаюсь извлечь определенную информацию из файла и вставить ее в связанный список, используя функцию push. Когда я пытаюсь просмотреть информацию, чтобы увидеть, правильно ли она вставлена, она просто отображает последнюю строку информации снова и снова. Что я делаю не так? Это мой код:Добавление данных в связанном списке
struct Country
{
string name;
double population;
};
struct Node
{
Country ctry;
Node *next;
};
Node *world;
void push(Node *&world);
int main()
{
push(world);
return 0;
}
void push(Node *&world)
{
ifstream inFile("file.csv");
if (!inFile.fail())
{
cout << "File has opened successfully." << endl;
}
if (inFile.fail())
{
cout << "File has failed to open." << endl;
exit(1);
}
double temp, temp1, temp2, temp3, population;
string countryName;
Node *top = new Node;
for (int i = 0; i < 300; i++)
{
if (inFile.eof())
{
top->next = NULL;
break;
}
inFile >> temp >> temp1 >> temp2 >> temp3 >> population;
getline (inFile,countryName);
top -> ctry.population = population;
top -> next = world;
world = top;
top -> ctry.name = countryName;
top -> next = world;
world = top;
}
for (int j = 0; j < 5; j++)
{
cout << top -> ctry.name << endl;
top -> next;
}
}
См. [Почему ostream :: eof() плохо?] (Http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) –