【问题标题】:Adding data in a linked list在链表中添加数据
【发布时间】:2016-04-21 01:10:35
【问题描述】:

我刚开始学习链表,我正在尝试从文件中提取某些信息并使用推送功能将其插入到链表中。当我尝试查看信息以查看它是否正确插入时,它只会一遍又一遍地显示信息的最后一行。我究竟做错了什么?这是我的代码:

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;
    }
}

【问题讨论】:

标签: c++ struct linked-list


【解决方案1】:

“世界”是您的链表的开始。

Node *top = new Node;

您在这里创建了一个新节点。我将跳过您填充其内容的部分。

    top -> next = world;
    world = top;

"world" 是指向列表开头的当前指针,正如我所提到的。您现在将其保存在顶部的next 中。然后将world 设置为指向新节点。这成为您列表的新开始。这很好。

    top -> next = world;
    world = top;

您不小心复制了几行代码。由于此时“top”与“world”是同一个指针,因此您只需将列表顶部的节点设置为指向自身即可。这就是你的无限循环。

【讨论】:

  • 节点对我来说真的很困惑,阅读您评论的最后部分我更加困惑。我尝试在循环的第二部分切换国家名称的顶部和世界。
  • @H.C 如果节点的概念让您感到困惑,那么也许您应该开始编写一个简单类型的链表,例如ints。在此之前,用方框和线条绘制每个操作的外观。
【解决方案2】:

你只为一个 Node top 分配内存,并且一直使用它。在 for 循环中放入以下行解决您的问题:

   Node *top = new Node;

【讨论】:

    【解决方案3】:

    嗯,我看到您的代码存在一些问题。

    首先,这条线没有做任何事情:

    top -> next;
    

    如果您正在遍历链表以读取存储的值,您可能会想要执行以下操作:

    for (;;)
    {
        cout << world -> ctry.name << endl;
        if (world->next == null) break;
           else world = world -> next;
    }
    

    您的创建循环中有几个重复的行和循环链接:

        top -> next = world;
        world = top;
    
        top -> ctry.name = countryName;
        top -> next = world;
        world = top;
    

    我认为这就是你的意思:

    world = top;
    for (int i = 0; i < 300; i++)
    {
        if (inFile.eof())
        {
            top->next = NULL;
            break;
        } else top = top -> next;
    
        inFile >> temp >> temp1 >> temp2 >> temp3 >> population;
        getline (inFile,countryName);
    
        top -> ctry.population = population;
        top -> ctry.name = countryName;
        top -> next = new Node;
    
    }
    

    最后,为自己保存一个函数调用并使用 else:

    if (!inFile.fail())
    {
        cout << "File has opened successfully." << endl;
    } else {
        cout << "File has failed to open." << endl;
        exit(1);
    }
    

    我希望这可以帮助您走上正轨。

    【讨论】:

    • 我尝试了你的 for 循环来创建链表并在链表中显示数据。它会显示最后一行,然后跳出循环。
    • 好的,重点是让您开始。不要为你编写程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    相关资源
    最近更新 更多