我有一个无向图,需要用邻接表来表示它。我正在使用单个链接列表的数组。 输入为:
11 18 3
1 8 4 7 7 10 11 10 2 1 2 3 8 9 8 3 9 3 9 2 5 6 5 11 1 4 10 6 7 6 2 8 11 7 11 6
首先,我正在创建列表数组。然后,要将节点添加到其中,我从输入中读取并创建2个指针。第一个保留必须添加到列表中的值作为信息,而第二个保留为列表的当前最后一个元素。然后,我将第一个绑定到第二个,第一个绑定到列表的最后一个元素。由于图形是无向的,因此我现在删除指针并创建新的指针以添加到反向对中。
输出不一致,并且从不匹配预期的输出。
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("info.in");
struct LLSI {
int info;
LLSI* urm;
} * c, *d, *cap, *v[100], *e;
int main()
{
int n, m, k, i, x, y;
f >> n >> m >> k;
//creating the array of lists
for (i = 1; i <= n; i++) {
v[i] = new LLSI;
v[i]->urm = 0;
v[i]->info = 0;
}
//adding the nodes into the adjacency list
for (i = 1; i <= m; i++) {
f >> x >> y;
c = new LLSI;
c->info = y;
c->urm = 0;
d = new LLSI;
d = v[x];
while (d->urm)
d = d->urm;
d->urm = c;
v[x]->info++;
d = c;
delete c;
delete d;
d = new LLSI;
d = v[y];
while (d->urm)
d = d->urm;
c = new LLSI;
c->info = x;
c->urm = 0;
d->urm = c;
v[y]->info++;
delete c;
delete d;
}
//outputting the adjancecy list
for (i = 1; i <= n; i++) {
c = new LLSI;
c = v[i];
while (c) {
cout << c->info << " ";
c = c->urm;
}
cout << "\n";
delete c;
}
return 0;
}
答案 0 :(得分:1)
此
//outputting the adjancecy list
for (i = 1; i <= n; i++) {
c = new LLSI;
c = v[i];
while (c) {
cout << c->info << " ";
c = c->urm;
}
cout << "\n";
delete c;
}
应该是这个
//outputting the adjancecy list
for (i = 1; i <= n; i++) {
c = v[i];
while (c) {
cout << c->info << " ";
c = c->urm;
}
cout << "\n";
}
我删除了new
和delete
。
不知道为什么您认为需要创建一个新列表以遍历列表。
在先前的循环中,您有相同的错误。这个
//adding the nodes into the adjacency list
for (i = 1; i <= m; i++) {
f >> x >> y;
c = new LLSI;
c->info = y;
c->urm = 0;
d = new LLSI;
d = v[x];
while (d->urm)
d = d->urm;
d->urm = c;
v[x]->info++;
d = c;
delete c;
delete d;
d = new LLSI;
d = v[y];
while (d->urm)
d = d->urm;
c = new LLSI;
c->info = x;
c->urm = 0;
d->urm = c;
v[y]->info++;
delete c;
delete d;
}
应该是这个
//adding the nodes into the adjacency list
for (i = 1; i <= m; i++) {
f >> x >> y;
c = new LLSI;
c->info = y;
c->urm = 0;
d = v[x];
while (d->urm)
d = d->urm;
d->urm = c;
v[x]->info++;
delete c;
d = v[y];
while (d->urm)
d = d->urm;
c = new LLSI;
c->info = x;
c->urm = 0;
d->urm = c;
v[y]->info++;
delete c;
}
我删除了d = new LLSI;
和delete d;
。使用new
创建一个新对象。如果只想指向一个现有对象,则不需要new
或delete
。
另一个明显的改进是您有很多重复的代码,它们只是在列表x
或列表y
上进行操作而有所不同。重复的代码应放在单独的函数中。
答案 1 :(得分:0)
谢谢您的建议,出于某种原因,我的老师教我我应该使用new
和delete
,尽管这样做更有意义。该代码现在未显示任何输出,但这是新代码的样子:
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("info.in");
struct LLSI {
int info;
LLSI* urm;
} * c, *d, *cap, *v[100], *e;
int main()
{
int n, m, k, i, x, y;
f >> n >> m >> k;
for (i = 1; i <= n; i++) {
v[i] = new LLSI;
v[i]->urm = 0;
v[i]->info = 0;
}
for (i = 1; i <= m; i++) {
f >> x >> y;
c = new LLSI;
c->info = y;
c->urm = 0;
d = v[x];
while (d->urm)
d = d->urm;
d->urm = c;
v[x]->info++;
delete c;
d = v[y];
while (d->urm)
d = d->urm;
c = new LLSI;
c->info = x;
c->urm = 0;
d->urm = c;
v[y]->info++;
delete c;
}
for (i = 1; i <= n; i++) {
c = v[i];
while (c) {
cout << c->info << " ";
c = c->urm;
}
cout << "\n";
}
return 0;
}