Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #include "ListGraph.h"
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. void init_list_graph(ListGraph *graph, int nodes) {
  7. /* TODO */
  8. graph->nodes = nodes;
  9. graph->neighbors = (LinkedList **)malloc(nodes * sizeof(LinkedList *));
  10.  
  11. if (graph->neighbors == NULL) {
  12. fprintf(stderr, "List not initialised.\n");
  13. return;
  14. }
  15.  
  16. for (int i = 0; i < nodes; ++i) {
  17. graph->neighbors[i] = (LinkedList *)malloc(sizeof(LinkedList));
  18. if (graph->neighbors[i] == NULL) {
  19. fprintf(stderr, "List not initialised.\n");
  20. return;
  21. }
  22. init_list(graph->neighbors[i]);
  23. }
  24. }
  25.  
  26. void add_edge_list_graph(ListGraph *graph, int src, int *dest) {
  27. /* TODO */
  28. add_nth_node(graph->neighbors[src], graph->neighbors[src]->size, dest);
  29. }
  30.  
  31. int has_edge_list_graph(ListGraph *graph, int src, int dest) {
  32. /* TODO */
  33. struct Node *curr;
  34. int found = 0;
  35.  
  36. if (graph->neighbors[src] == NULL) {
  37. fprintf(stderr, "List not initialised.\n");
  38. return 0;
  39. }
  40.  
  41. if (graph->neighbors[src]->size == 0) {
  42. return 0;
  43. }
  44.  
  45. curr = graph->neighbors[src]->head;
  46. while (curr->next != NULL) {
  47. if (*((int *)curr->data) == dest) {
  48. found = 1;
  49. break;
  50. }
  51. curr = curr->next;
  52. }
  53.  
  54. if (found == 1) {
  55. curr = graph->neighbors[dest]->head;
  56. while (curr->next != NULL) {
  57. if (*((int *)curr->data) == src) {
  58. return 1;
  59. }
  60. curr = curr->next;
  61. }
  62. fprintf(stderr, "Graph is not undirected.\n");
  63. }
  64.  
  65. return 0;
  66. }
  67.  
  68. LinkedList *get_neighbours_list_graph(ListGraph *graph, int node) {
  69. /* TODO */
  70. if (graph->neighbors[node] == NULL) {
  71. fprintf(stderr, "List not initialised.\n");
  72. return NULL;
  73. }
  74.  
  75. return graph->neighbors[node];
  76. }
  77.  
  78. void remove_edge_list_graph(ListGraph *graph, int src, int dest) {
  79. /* TODO */
  80. struct Node *curr, *tmp;
  81. int found = 0;
  82.  
  83. if (graph->neighbors[src] == NULL) {
  84. fprintf(stderr, "List not initialised.\n");
  85. return;
  86. }
  87.  
  88. if (graph->neighbors[dest] == NULL) {
  89. fprintf(stderr, "List not initialised.\n");
  90. return;
  91. }
  92.  
  93. curr = graph->neighbors[src]->head;
  94. for (int i = 0; i < graph->neighbors[src]->size; ++i) {
  95. if (*(int *)curr->data == dest) {
  96. tmp = remove_nth_node(graph->neighbors[src], i);
  97. free(tmp);
  98. found = 1;
  99. break;
  100. } else if (i == graph->neighbors[src]->size - 1) {
  101. fprintf(stderr, "Element not found.\n");
  102. return;
  103. }
  104. curr = curr->next;
  105. }
  106.  
  107. if (found == 1) {
  108. curr = graph->neighbors[dest]->head;
  109. for (int i = 0; i < graph->neighbors[dest]->size; ++i) {
  110. if (*(int *)curr->data == src) {
  111. tmp = remove_nth_node(graph->neighbors[dest], i);
  112. free(tmp);
  113. return;
  114. } else if (i == graph->neighbors[src]->size - 1) {
  115. fprintf(stderr, "Graph is not undirected.\n");
  116. return;
  117. }
  118. curr = curr->next;
  119. }
  120. }
  121. }
  122.  
  123. void clear_list_graph(ListGraph *graph) {
  124. /* TODO */
  125. for (int i = 0; i < graph->nodes; ++i) {
  126. free_list(&graph->neighbors[i]);
  127. }
  128. free(graph->neighbors);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement