Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <assert.h>
- #include <stdlib.h>
- #include <string.h>
- #define GRAPH_DEFAULT_EDGES_CAPACITY 10
- #define GRAPH_DEFAULT_NODES_CAPACITY 10
- struct graph_edge_t;
- struct graph_node_t;
- typedef struct graph_node_t {
- const char* city_name;
- struct graph_edge_t* edges;
- } GraphNode;
- typedef struct graph_edge_t {
- GraphNode* source;
- GraphNode* destination;
- struct graph_edge_t* sibling_edge;
- } GraphEdge;
- typedef struct {
- GraphEdge* edges;
- size_t edges_capacity;
- size_t edges_count;
- GraphNode* nodes;
- size_t nodes_capacity;
- size_t nodes_count;
- } Graph;
- void graph_reserve_more_edges(Graph* graph) {
- if(graph->edges_capacity > graph->edges_count) {
- return;
- }
- GraphEdge* new_storage = malloc(sizeof(GraphEdge) * graph->edges_capacity * 2);
- memset(new_storage, 0, sizeof(graph->edges) * graph->edges_capacity * 2);
- memcpy(new_storage, graph->edges, graph->edges_capacity);
- free(graph->edges);
- graph->edges = new_storage;
- graph->edges_capacity *= 2;
- }
- void graph_reserve_more_nodes(Graph* graph) {
- if(graph->nodes_capacity > graph->nodes_count) {
- return;
- }
- GraphNode* new_storage = malloc(sizeof(GraphNode) * graph->nodes_capacity * 2);
- memset(new_storage, 0, sizeof(graph->nodes) * graph->nodes_capacity * 2);
- memcpy(new_storage, graph->nodes, graph->nodes_capacity);
- free(graph->nodes);
- graph->nodes = new_storage;
- graph->nodes_capacity *= 2;
- }
- GraphEdge* graph_edge_new(Graph* graph, GraphNode* source, GraphNode* destination) {
- GraphEdge edge;
- memset(&edge, 0, sizeof(edge));
- edge.source = source;
- edge.destination = destination;
- edge.sibling_edge = source->edges;
- graph_reserve_more_edges(graph);
- memcpy(&graph->edges[graph->edges_count], &edge, sizeof(edge));
- source->edges = &graph->edges[graph->edges_count];
- graph->edges_count++;
- return &graph->edges[graph->edges_count-1];
- }
- GraphNode* graph_node_new(Graph* graph, const char* city_name) {
- GraphNode node;
- memset(&node, 0, sizeof(GraphNode));
- node.city_name = city_name;
- graph_reserve_more_nodes(graph);
- memcpy(&graph->nodes[graph->nodes_count], &node, sizeof(node));
- graph->nodes_count++;
- return &graph->nodes[graph->nodes_count-1];
- }
- void graph_destroy(Graph* graph) {
- free(graph->edges);
- free(graph->nodes);
- free(graph);
- }
- Graph* graph_new() {
- Graph *graph = malloc(sizeof(Graph));
- memset(graph, 0, sizeof(*graph));
- graph->edges = malloc(sizeof(graph->edges)*GRAPH_DEFAULT_EDGES_CAPACITY);
- graph->nodes = malloc(sizeof(graph->edges)*GRAPH_DEFAULT_NODES_CAPACITY);
- return graph;
- }
- int main(int argc, char** argv) {
- Graph* graph = graph_new();
- GraphNode* node_a = graph_node_new(graph, "London");
- GraphNode* node_b = graph_node_new(graph, "Birmingham");
- GraphNode* node_c = graph_node_new(graph, "Manchester");
- GraphEdge* edge0 = graph_edge_new(graph, node_a, node_b);
- GraphEdge* edge1 = graph_edge_new(graph, node_b, node_c);
- GraphEdge* edge2 = graph_edge_new(graph, node_c, node_a);
- graph_destroy(graph);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement