首页 > 解决方案 > 如何将路线存储在链表中

问题描述

我想从文本(stdin)制作坐标路线,其中输入将是未确定数量的坐标,例如

[0,1]
[1,1]
[1,2]
.
.
.

所以我想制作一个while循环来扫描坐标并将其添加到链表中,直到它通过所有坐标。唯一的问题是我不知道从哪里开始这样做,因为我想不出如何用 x 和 y 坐标制作链表。任何能让我走上正轨的帮助都将不胜感激,干杯。

标签: cloopslinked-listcoordinates

解决方案


虽然大多数链接列表的教程示例仅使用一个“有效负载”成员:

struct Node {
  int value;
  struct Node* next;
}

没有什么能阻止您添加更多内容:

struct Node {
  int x;
  int y;
  struct Node* next;
}

您可能还希望对有效负载成员进行分组,以使它们与列表基础结构分开:

struct Node {
  struct {
    int x;
    int y;
  } coord;
  struct Node* next;
}

推荐阅读