Advertisement
Guest User

Directional Graph in C

a guest
May 16th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | Software | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define GRAPH_DEFAULT_EDGES_CAPACITY 10
  7. #define GRAPH_DEFAULT_NODES_CAPACITY 10
  8.  
  9. struct graph_edge_t;
  10. struct graph_node_t;
  11.  
  12. typedef struct graph_node_t {
  13.     const char* city_name;
  14.     struct graph_edge_t* edges;
  15. } GraphNode;
  16.  
  17.  
  18. typedef struct graph_edge_t {
  19.     GraphNode* source;
  20.     GraphNode* destination;
  21.     struct graph_edge_t* sibling_edge;
  22. } GraphEdge;
  23.  
  24. typedef struct {
  25.     GraphEdge* edges;
  26.     size_t edges_capacity;
  27.     size_t edges_count;
  28.     GraphNode* nodes;
  29.     size_t nodes_capacity;
  30.     size_t nodes_count;
  31. } Graph;
  32.  
  33. void graph_reserve_more_edges(Graph* graph) {
  34.  
  35.     if(graph->edges_capacity > graph->edges_count) {
  36.         return;
  37.     }
  38.  
  39.     GraphEdge* new_storage = malloc(sizeof(GraphEdge) * graph->edges_capacity * 2);
  40.     memset(new_storage, 0, sizeof(graph->edges) * graph->edges_capacity * 2);
  41.  
  42.     memcpy(new_storage, graph->edges, graph->edges_capacity);
  43.     free(graph->edges);
  44.  
  45.     graph->edges = new_storage;
  46.     graph->edges_capacity *= 2;
  47. }
  48.  
  49. void graph_reserve_more_nodes(Graph* graph) {
  50.  
  51.     if(graph->nodes_capacity > graph->nodes_count) {
  52.         return;
  53.     }
  54.  
  55.     GraphNode* new_storage = malloc(sizeof(GraphNode) * graph->nodes_capacity * 2);
  56.     memset(new_storage, 0, sizeof(graph->nodes) * graph->nodes_capacity * 2);
  57.  
  58.     memcpy(new_storage, graph->nodes, graph->nodes_capacity);
  59.     free(graph->nodes);
  60.  
  61.     graph->nodes = new_storage;
  62.     graph->nodes_capacity *= 2;
  63. }
  64.  
  65. GraphEdge* graph_edge_new(Graph* graph, GraphNode* source, GraphNode* destination) {
  66.     GraphEdge edge;
  67.     memset(&edge, 0, sizeof(edge));
  68.  
  69.     edge.source = source;
  70.     edge.destination = destination;
  71.     edge.sibling_edge = source->edges;
  72.  
  73.     graph_reserve_more_edges(graph);
  74.     memcpy(&graph->edges[graph->edges_count], &edge, sizeof(edge));
  75.     source->edges = &graph->edges[graph->edges_count];
  76.     graph->edges_count++;
  77.  
  78.     return &graph->edges[graph->edges_count-1];
  79. }
  80.  
  81. GraphNode* graph_node_new(Graph* graph, const char* city_name) {
  82.     GraphNode node;
  83.     memset(&node, 0, sizeof(GraphNode));
  84.     node.city_name = city_name;
  85.  
  86.     graph_reserve_more_nodes(graph);
  87.     memcpy(&graph->nodes[graph->nodes_count], &node, sizeof(node));
  88.     graph->nodes_count++;
  89.  
  90.     return &graph->nodes[graph->nodes_count-1];
  91. }
  92.  
  93. void graph_destroy(Graph* graph) {
  94.     free(graph->edges);
  95.     free(graph->nodes);
  96.     free(graph);
  97. }
  98.  
  99. Graph* graph_new() {
  100.     Graph *graph = malloc(sizeof(Graph));
  101.     memset(graph, 0, sizeof(*graph));
  102.     graph->edges = malloc(sizeof(graph->edges)*GRAPH_DEFAULT_EDGES_CAPACITY);
  103.     graph->nodes = malloc(sizeof(graph->edges)*GRAPH_DEFAULT_NODES_CAPACITY);
  104.     return graph;
  105. }
  106.  
  107. int main(int argc, char** argv) {
  108.  
  109.     Graph* graph = graph_new();
  110.  
  111.     GraphNode* node_a = graph_node_new(graph, "London");
  112.     GraphNode* node_b = graph_node_new(graph, "Birmingham");
  113.     GraphNode* node_c = graph_node_new(graph, "Manchester");
  114.  
  115.     GraphEdge* edge0 = graph_edge_new(graph, node_a, node_b);
  116.     GraphEdge* edge1 = graph_edge_new(graph, node_b, node_c);
  117.     GraphEdge* edge2 = graph_edge_new(graph, node_c, node_a);
  118.  
  119.     graph_destroy(graph);
  120. };
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement