I have an undirected graph and need to represent it usind an adjacency list. I'm using an array of single linked lists. The input is:
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
First, I'm creating the array of lists. Then, to add the nodes into it, I read from the input and create 2 pointers. The first one holds as information the value that must be added into the list, while the second one is the current last element of the list. Then I bind the first one with the second, the fist one now becoming the last element of the list. I delete the pointers and create new ones to add in the inversed pair now, as the graph is undirected.
The output is inconsistent and never matches the expected output.
#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;
}