将数据存储到链表中
Storing Data into a Linked List C++
输入格式如下:
5
1 2 9.0
1 3 12.0
2 4 18.0
2 3 6.0
2 5 20.0
3 5 15.0
0
1 5
第一个数字是图中顶点的个数。接下来到0的线是图的边。第一个和第二个数字是顶点,第三个数字是它们之间的边的距离。我不知道如何将数据存储到列表邻接的每个顶点时读取它。例如,顶点1将有两个列表单元格,包含2 9.0和3 12.0。我还需要把1,9和1,12放入顶点2和3。但是我不知道如何将数据存储到ListCells
Code so Far:
#include <cstdio>
using namespace std;
typedef ListCell* List;
struct ListCell
{
ListCell* next;
int vertex;
double weight;
ListCell(int v, double w, ListCell* nxt)
{
vertex = v;
weight = w;
next = nxt;
}
};
struct Vertex
{
bool signaled;
long distance;
Vertex next;
List adjacency;
};
struct Graph
{
int numVertices;
Vertex* vertexInfo;
Graph(int n)
{
numVertices = n;
vertexInfo = new Vertex[n+1];
for(int i = 1; i <= n; i++)
{
vertexInfo[i].signaled = false;
}
}
};
//==============================================================
// readIn
//==============================================================
//
//==============================================================
void readIn()
{
int n, p1, p2;
double edge;
scanf("%i ", &n);
Graph(n);
while(scanf("%i " &p1) != 0)
{
}
}
我用一种适合业务逻辑的方式来定义数据结构。
我建议你看一看《计算机编程艺术》,了解一些最佳实践。
关注"Linear Lists"章节
提示:遍历列表并附加新节点(请注意角落情况):
Vertex* v = vertexInfo[i];
while (v->next!=null) {
v = v->next;
}
v->next = new Vertex(....);
相关文章:
- 反向给定链表中的K节点
- 为什么C中的通用链表中存储的数据已损坏
- 将从格式化文本文件读取的文本数据存储到链表
- C++将存储在链表中的两个大数字相乘
- 相同的地址存储在链表的头部
- 如何将自己的链表实现从存储整数更改为存储个人数据
- 显示来自链表的稀疏矩阵,该链表仅存储了非零元素
- 存储类对象的C++模板链表-未处理的异常访问冲突读取位置
- 使用c++通过文件存储和检索链表
- 为什么链表使用指针而不是将节点存储在节点内部
- 在双链表中,指针不是存储彼此的地址,而是指向相同的位置
- 打印存储在链表中的数据时出现问题
- C++ 将链表存储在链表中
- 将地址和布尔值存储在一个单词中,以实现无锁双链表
- 从文件中读取,存储到链表中,然后再次打印到文件中
- 如何使用链表来存储表,并在表中搜索项
- 为什么这个自定义链表不存储 c++ 中给出的每个元素?
- 将值存储在链表中的结构中
- malloc和heap:用于存储大小和链表信息的额外内存
- 将数据存储到链表中