图数据结构代码
使用数组导入边创建邻接矩阵表示法的图
1 #include <stdio.h> 2 3 int matrix[6][6]; 4 5 void creategraph(int *node,int num) 6 { 7 int from; 8 int to; 9 int i; 10 11 for(i = 0;i < num;i++) 12 { 13 from = node[i * 2]; 14 to = node[i * 2 + 1]; 15 matrix[from][to] = 1; 16 } 17 } 18 19 int main() 20 { 21 int node[12][2] = { 22 {1,2}, {2,1}, 23 {1,3}, {3,1}, 24 {2,3}, {3,2}, 25 {2,4}, {4,2}, 26 {3,5}, {5,3}, 27 {4,5}, {5,4} 28 }; 29 30 int i,j; 31 32 for(i = 1; i <= 5;i++) 33 for(j = 1;j <= 5;j++) 34 matrix[i][j] = 0; 35 creategraph(node,12); 36 printf("图的邻接矩阵内容:\n"); 37 for(i = 1;i <= 5;i++) 38 { 39 for(j = 1;j <= 5;j++) 40 printf(" %d ",matrix[i][j]); 41 printf("\n"); 42 } 43 return 0; 44 }
图的邻接表表示
1 #include <stdlib.h> 2 #include <stdio.h> 3 struct node 4 { 5 int vertex; 6 struct node* nextnode; 7 }; 8 9 typedef struct node* graph; 10 struct node head[6]; 11 12 void creategraph(int* node,int num) 13 { 14 graph newnode; 15 graph ptr; 16 int from; 17 int to; 18 int i; 19 20 for(i = 0;i < num;i++) 21 { 22 from = node[i * 2]; 23 to = node[i*2 + 1]; 24 newnode = (graph)malloc(sizeof(struct node)); 25 newnode->vertex = to; 26 newnode->nextnode = NULL; 27 ptr = &(head[from]); 28 while(ptr->nextnode != NULL) 29 ptr = ptr->nextnode; 30 ptr->nextnode = newnode; 31 } 32 } 33 34 int main() 35 { 36 graph ptr; 37 int node[12][2] = { 38 {1,2}, {2,1}, 39 {1,3}, {3,1}, 40 {2,3}, {3,2}, 41 {2,4}, {4,2}, 42 {3,5}, {5,3}, 43 {4,5}, {5,4} 44 }; 45 int i; 46 47 for(i = 1;i <= 5;i++) 48 { 49 head[i].vertex = i; 50 head[i].nextnode = NULL; 51 } 52 creategraph(node,12); 53 printf("图的邻接表内容:\n"); 54 for(i = 1;i <= 5;i++) 55 { 56 printf("顶点%d =>",head[i].vertex); 57 ptr = head[i].nextnode; 58 while(ptr != NULL) 59 { 60 printf(" %d ",ptr->vertex); 61 ptr = ptr->nextnode; 62 } 63 printf("\n"); 64 } 65 return 0; 66 }
创建邻接多重表图结构
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct edge 5 { 6 int vertex1; 7 int vertex2; 8 struct edge* edge1; 9 struct edge* edge2; 10 }; 11 12 typedef struct edge* nextedge; 13 14 struct node 15 { 16 int vertex; 17 struct edge* edge; 18 }; 19 20 typedef struct node* graph; 21 struct node head[6]; 22 23 void creategraph(int* node,int num) 24 { 25 nextedge newnode; 26 nextedge previous; 27 nextedge ptr; 28 int from; 29 int to; 30 int i; 31 32 for(i = 0;i < num;i++) 33 { 34 from = node[i*2]; 35 to = node[i*2+1]; 36 37 newnode = (nextedge)malloc(sizeof(struct edge)); 38 newnode->vertex1 = from; 39 newnode->vertex2 = to; 40 newnode->edge1 = NULL; 41 newnode->edge2 = NULL; 42 previous = NULL; 43 ptr = head[from].edge; 44 while(ptr != NULL) 45 { 46 previous = ptr; 47 if(ptr->vertex1 == from) 48 ptr = ptr->edge1; 49 else 50 ptr = ptr->edge2; 51 } 52 if(previous == NULL) 53 head[from].edge = newnode; 54 else 55 if(previous->vertex1 == from) 56 previous->edge1 = newnode; 57 else 58 previous->edge2 = newnode; 59 previous = NULL; 60 ptr = head[to].edge; 61 while(ptr != NULL) 62 { 63 previous = ptr; 64 if(ptr->vertex1 == to) 65 ptr = ptr->edge1; 66 else 67 ptr = ptr->edge2; 68 } 69 if(previous == NULL) 70 head[to].edge = newnode; 71 else 72 if(previous->vertex1 == to) 73 previous->edge1 = newnode; 74 else 75 previous->edge2 = newnode; 76 } 77 } 78 79 int main() 80 { 81 nextedge ptr; 82 int node[6][2] = { 83 {1,2}, 84 {1,3}, 85 {2,3}, 86 {2,4}, 87 {3,5}, 88 {4,5} 89 }; 90 int i; 91 for(i = 1;i <= 5;i++) 92 { 93 head[i].vertex = i; 94 head[i].edge = NULL; 95 } 96 creategraph(node,6); 97 printf("图的邻接多重表内容:\n"); 98 for(i = 1;i <= 5;i++) 99 { 100 printf("顶点%d =>",head[i].vertex); 101 ptr = head[i].edge; 102 while(ptr != NULL) 103 { 104 printf("(%d,%d)",ptr->vertex1,ptr->vertex2); 105 if(head[i].vertex == ptr->vertex1) 106 ptr = ptr->edge1; 107 else 108 ptr = ptr->edge2; 109 } 110 printf("\n"); 111 } 112 return 0; 113 }