Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Node
  5. {
  6. int data;
  7. struct Node *next;
  8. } Node;
  9.  
  10. void adding_to_queue(int peak, Node *queue)
  11. {
  12. if(queue == NULL){
  13. queue = (Node*)malloc(sizeof(Node));
  14. queue->data = peak;
  15. queue->next = NULL;
  16. } else {
  17. Node *cur = queue;
  18. while(cur->next != NULL){
  19. cur = cur->next;
  20. }
  21. cur->next = (Node*)malloc(sizeof(Node));
  22. cur->next->data = peak;
  23. cur->next->next = NULL;
  24. }
  25. }
  26.  
  27. int delete_from_queue(Node *queue)
  28. {
  29. int peak = queue->data;
  30. if(queue->next != NULL) queue = queue->next;
  31. else queue = NULL;
  32.  
  33. return peak;
  34. }
  35.  
  36. int is_full(int *numbers_of_peaks, int amount_of_peaks)
  37. {
  38. int i = 0, result = -1;
  39. for(i = 0; i < amount_of_peaks; ++i)
  40. {
  41. if(numbers_of_peaks[i] != 2) result = 1;
  42. }
  43. if(result == -1) result = 0;
  44.  
  45. return result;
  46. }
  47.  
  48. int **adjancency_matrix_creating(int amount_of_peaks)
  49. {
  50. int i = 0, j = 0;
  51.  
  52. int **adjacency_matrix;
  53. adjacency_matrix = (int**)malloc(amount_of_peaks * sizeof(int*));
  54.  
  55. for (i = 0; i < amount_of_peaks; i++)
  56. {
  57. adjacency_matrix[i] = (int*)malloc(amount_of_peaks * sizeof(int));
  58. }
  59.  
  60. //заполняем матрицу смежности
  61. for(i = 0; i < amount_of_peaks; ++i)
  62. {
  63. printf("С какими вершинами смежна %d вершина? После введите 0n", i+1);
  64.  
  65. int is_scanf = -1, peak = -1;
  66. //заполняем смежные вершины
  67. while(peak)
  68. {
  69. is_scanf = scanf("%d", &peak);
  70.  
  71. if(is_scanf)
  72. {
  73. adjacency_matrix[i][peak-1] = 1;
  74. }
  75. }
  76. //заполняем остальное
  77. for(j = 0; j < amount_of_peaks; ++j)
  78. {
  79. if(adjacency_matrix[i][j] != 1)
  80. {
  81. adjacency_matrix[i][j] = 0;
  82. }
  83. }
  84. }
  85. //вывод матрицы смежности
  86. printf("nМатрица смежности:n");
  87.  
  88. for(i = 0; i < amount_of_peaks; ++i)
  89. {
  90. for(j = 0; j < amount_of_peaks; ++j)
  91. {
  92. printf("%d ", adjacency_matrix[i][j]);
  93. }
  94. printf("n");
  95. }
  96.  
  97. return adjacency_matrix;
  98. }
  99.  
  100. int main()
  101. {
  102. //Запрашиваем количество вершин и матрицу смежности
  103. system("chcp 1251");
  104.  
  105. int amount_of_peaks = 0, i = 0, j = 0;
  106.  
  107. printf("Сколько вершин в графе? ");
  108. scanf("%d", &amount_of_peaks);
  109.  
  110. int **adjacency_matrix = adjancency_matrix_creating(amount_of_peaks);
  111.  
  112. //обход
  113. Node *queue = NULL;
  114.  
  115. int peak_start;
  116. int *numbers_of_peaks = (int*)malloc(amount_of_peaks * sizeof(int));
  117.  
  118. for(i = 0; i < amount_of_peaks; ++i)
  119. {
  120. numbers_of_peaks[i] = 0;
  121. }
  122.  
  123. printf("С какой вершины начать обход? ");
  124. scanf("%d", &peak_start);
  125.  
  126. peak_start = peak_start-1;
  127.  
  128. adding_to_queue(peak_start, queue);
  129.  
  130. numbers_of_peaks[peak_start] = 1;
  131.  
  132. int current_peak, checking = -1;
  133.  
  134. do
  135. {
  136. current_peak = delete_from_queue(queue);
  137.  
  138. if(numbers_of_peaks[current_peak] == 1)
  139. {
  140. for(i = 0; i < amount_of_peaks; ++i)
  141. {
  142. if(adjacency_matrix[current_peak][i] == 1)
  143. {
  144. if(numbers_of_peaks[i] == 0)
  145. {
  146. adding_to_queue(i, queue);
  147. numbers_of_peaks[i] = 1;
  148. }
  149. }
  150. }
  151. numbers_of_peaks[current_peak] = 2;
  152. printf("Обработанная вершина: %dn", current_peak);
  153.  
  154. checking = is_full(numbers_of_peaks, amount_of_peaks);
  155. }
  156. } while(checking);
  157.  
  158. for(i = amount_of_peaks-1; i >= 0; i--)
  159. {
  160. free(adjacency_matrix[i]);
  161. }
  162.  
  163. free(adjacency_matrix);
  164.  
  165. return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement