We are told our input file would be a simple list of numbers:
1 3 4
2 3
3 4
4 1 2
Where the first number is the source node, and the proceeding numbers are it’s adjacent nodes.
I am trying to figure out how to best store this.
I wanted to firstly initialize a “graph”, an array that contains all these nodes.
Then upon reading the file, line by line, I would store the root node into the graph array, and then update the node’s outlist (adjacent nodes) with the following numbers until we reach the end of the line, repeating this for each line until EOF.
However I’m struggling on how to initialize the graph, do I just assume a certain size and realloc() once the size is hit? Do I read the file first and count the number of lines to find out the size, then re-read the file to store the nodes? Is there any other way?
Here is the code for my data structures:
int initialize (Graph *mygraph, int MaxSize) {
mygraph->MaxSize = MaxSize;
mygraph->table = (Node *)malloc(sizeof(Node) * MaxSize);
return 0;
}
int insert_node (Graph *mygraph, int n, char *name) {
mygraph->table[n].name = strdup(name);
mygraph->table[n].outdegree = 0;
return 0;
}
int insert_link (Graph *mygraph, int source, int target) {
List *newList = (List *)malloc(sizeof(List));
newList->index = target;
newList->next = mygraph->table[source].outlist;
mygraph->table[source].outlist = newList;
return 0;
}
So upon reading the file,
-
I initialize the graph.
-
I read the first number, store it as a new graph node.
-
I read the next numbers until hitting “\n”, and store these as graph links to the above root node.
-
I do this for each line until hitting EOF.
As you can see I have no idea what the “MaxSize” until the whole file is read.
Thanks!
I’m rather new to C so sorry if I’ve done anything silly.
You could have some initial guess for
MaxSize(e.g. 8) and grow when needed your data (perhaps bygraph->MaxSize += graph->MaxSize/2) usingrealloc, or just bymalloc-ing a bigger new chunk, copying the older chunk inside, thenfree-ing that older chunk). Don’t forget to check the successful result of anymallocorcallocorrealloccall, they could (rarely) fail.Notice that I have no idea of how your
GraphandNodetype is declared (just guessing).I am assuming and guessing you have declared something like
So for example your
insert_nodefunction might beYou probably don’t need
insert_nodeto return a value (otherwise you won’t always return 0). So I made it avoidreturning function (i.e. a “procedure” or “routine”).