| #include <stdio.h> #include <stdlib.h> /* 인접 행렬을 이용한 그래프 추상 데이터 타입의 구현 */ #define MAX_VERTICES 50 typedef struct GraphType {     int n;     int adj_mat[MAX_VERTICES][MAX_VERTICES]; }GraphType; //그래프 초기화 void graph_init(GraphType *g) {     int r, c;     g->n = 0;     for (r = 0; r < MAX_VERTICES; r++)         for (c = 0; c < MAX_VERTICES; c++)             g->adj_mat[r][c] = 0; } //정점 삽입 연산 void insert_vertex(GraphType *g, int v) {     if (((g->n) + 1) > MAX_VERTICES)     {         fprintf(stderr, "그래프:정점의 개수 초과");         return;     }     g->n++; } //간선 삽입 연산 void insert_edge(GraphType *g, int start, int end) {     if (start >= g->n || end >= g->n)     {         fprintf(stderr, "그래프:정점 번호 오류");         return;     }     g->adj_mat[start][end] = 1;     g->adj_mat[end][start] = 1; } /* 인접 리스트를 이용한 그래프 추상 데이터 타입의 구현 */ #define MAX_VERTICES 50 typedef struct GraphNode {     int vertex;     struct GraphNode *link }GraphNode; typedef struct GraphType {     int n;     GraphNode *adj_list[MAX_VERTICES]; }GraphType; //그래프 초기화 void graph_init(GraphType *g) {     int v;     g->n = 0;     for (v = 0; v < MAX_VERTICES; v++)         g->adj_list[v] = NULL; } //정점 삽입 연산 void insert_vertex(GraphType *g, int v) {     if ((g->n) + 1 > MAX_VERTICES)     {         fprintf(stderr, "그래프:정점의 개수 초과");         return;     }     g->n++; } //간선 삽입 연산, v를 u의 인접 리스트에 삽입한다. void insert_edge(GraphType *g, int u, int v) {     GraphNode *node;     if (u >= g->n || v >= g->n)     {         fprintf(stderr, "그래프:정점 번호 오류");         return;     }     node = (GraphNode *)malloc(sizeof(GraphNode));     node->vertex = v;     node->link = g->adj_list[u];     g->adj_list[u] = node; } | cs | 
'NOTE > Algorithm' 카테고리의 다른 글
| 자료구조) Binary_Tree (0) | 2015.08.25 | 
|---|---|
| 자료구조) Tree_Traverse (0) | 2015.08.25 | 
| 자료구조) QuickSort (0) | 2015.08.25 | 
| 자료구조) linked_queue (0) | 2015.08.25 | 
| 자료구조) linked_stack (0) | 2015.08.25 |