Graph Class
Lesson 3 of 14 in Coddy's Graphs - Data Structures Series #9 course.
The Graph class is built around a single field, vertices, which is the adjacency list: a map from each vertex key to the list of its neighbors. Every operation we add later (adding edges, listing neighbors, removing edges) is just a small change to this map.
The constructor starts the graph in its empty state: no vertices, no edges. So the map is just an empty map. The methods we add in the next lessons will grow it.
Challenge
EasyWrite a class Graph with a constructor that takes no input.
Initialize a single field vertices as an empty map (or your language's equivalent of a key-to-list adjacency map).
Try it yourself
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "graph.h"
static int _cmp_int(const void* a, const void* b) {
int ai = *(const int*)a, bi = *(const int*)b;
return (ai > bi) - (ai < bi);
}
int main() {
Graph g;
Graph_init(&g);
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
line[strcspn(line, "\r\n")] = '\0';
char* cmd = strtok(line, " \t");
if (!cmd) continue;
if (strcmp(cmd, "verticesEmpty") == 0) { printf("%s\n", g.vertexCount == 0 ? "true" : "false"); }
}
return 0;
}