fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #define MAX 50
  5. #define TRUE 1
  6. #define FALSE 0
  7.  
  8. typedef struct graphnode {
  9. int vertex;
  10. struct graphnode *link;
  11. }graphnode;
  12.  
  13. typedef struct graphtype {
  14. int n;
  15. graphnode *adj[MAX];
  16. }graphtype;
  17. typedef int element;
  18. typedef struct {
  19. element queue[MAX];
  20. int front, rear;
  21. }queue;
  22.  
  23. int visited[MAX] = { FALSE };
  24.  
  25. void error(char *message)
  26. {
  27. fprintf(stderr, "%s\n", message);
  28. exit(1);
  29. }
  30. void init(graphtype *g)
  31. {
  32. g->n = 0;
  33. for (int v = 0; v < MAX; v++)
  34. g->adj[v] = NULL;
  35. }
  36.  
  37. void insertv(graphtype *g, int v)
  38. {
  39. if ((g->n) + 1 > MAX)
  40. error("그래프 정점 개수 초과");
  41. for (int i = 0; i < v; i++)
  42. g->n++;
  43. }
  44.  
  45. void inserte(graphtype *g, int u, int v)
  46. {
  47. if (u >= g->n || v >= g->n)
  48. error("그래프 정점 오류 번호");
  49. graphnode *node = (graphnode *)malloc(sizeof(graphnode));
  50. node->vertex = v;
  51. node->link = g->adj[u];
  52. g->adj[u] = node;
  53. }
  54.  
  55. void print(graphtype *g)
  56. {
  57. for (int i = 0; i < g->n; i++)
  58. {
  59. graphnode *p = g->adj[i];
  60. printf("정점 %d의 인접 리스트 ",i);
  61. while (p != NULL)
  62. {
  63. printf("-> %d", p->vertex);
  64. p = p->link;
  65. }
  66. printf("\n");
  67. }
  68. }
  69. void initq(queue *q)
  70. {
  71. q->front = q->rear = 0;
  72. }
  73. int is_empty(queue *q)
  74. {
  75. return (q->front == q->rear);
  76. }
  77.  
  78. int is_full(queue *q)
  79. {
  80. return((q->rear + 1) % MAX == q->front);
  81. }
  82.  
  83. void enqueue(queue *q, element item)
  84. {
  85. if (is_full(q))
  86. error("큐 포화상태");
  87. q->rear = (q->rear) + 1 % MAX;
  88. q->queue[q->rear] = item;
  89. }
  90.  
  91. element dequeue(queue *q)
  92. {
  93. if (is_empty(q))
  94. error("큐가 공백상태");
  95. q->front = (q->front + 1) % MAX;
  96. return q->queue[q->front];
  97. }
  98.  
  99. void dfs(graphtype *g, int v)
  100. {
  101. graphnode *w;
  102. visited[v] = TRUE;
  103. printf("%d-> ", v);
  104. for (w = g->adj[v]; w!=NULL; w = w->link)
  105. {
  106. if (!visited[w->vertex])
  107. dfs(g, w->vertex);
  108. }
  109. }
  110. void bfs(graphtype *g, int v)
  111. {
  112. graphnode *w;
  113. queue q;
  114. initq(&q);
  115. visited[v] = TRUE;
  116. printf("%d-> ", v);
  117. enqueue(&q, v);
  118. while (!is_empty(&q))
  119. {
  120. v = dequeue(&q);
  121. for (w = g->adj[v]; w; w = w->link)
  122. {
  123. if (!visited[w->vertex])
  124. {
  125. visited[w->vertex] = TRUE;
  126. printf("%d-> ",w->vertex);
  127. enqueue(&q, w->vertex);
  128. }
  129. }
  130. }
  131. }
  132.  
  133.  
  134. void main()
  135. {
  136. graphtype *g;
  137. g = (graphtype *)malloc(sizeof(graphtype));
  138. init(g);
  139. insertv(g, 4);
  140.  
  141. inserte(g, 0, 2);
  142. inserte(g, 2, 1);
  143. inserte(g, 2, 3);
  144. inserte(g, 0, 4);
  145. inserte(g, 4, 5);
  146. inserte(g, 1, 5);
  147.  
  148. printf("깊이 우선 탐색\n");
  149. bfs(g, 0);
  150. printf("\n");
  151. free(g);
  152. getch();
  153. }
  154.  
  155.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:134:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main()
      ^~~~
prog.c: In function ‘main’:
prog.c:152:2: warning: implicit declaration of function ‘getch’; did you mean ‘getc’? [-Wimplicit-function-declaration]
  getch();
  ^~~~~
  getc
/usr/bin/ld: /home/61SPIk/ccOMvAdp.o: in function `main':
prog.c:(.text.startup+0xdf): undefined reference to `getch'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty