資料機構之圖的儲存
阿新 • • 發佈:2019-02-05
#ifndef _HEAD_H_ #define _HEAD_H_ #include <stdio.h> #include <stdlib.h> typedef int DataType; struct node { DataType data; struct node *next; }; typedef struct { struct node *front; struct node *rear; }LinkQueue; extern LinkQueue *create_empty_linkqueue(); extern int is_empty_linkqueue(LinkQueue *q); extern int enter_linkqueue(LinkQueue *q,DataType data); //刪頭法 extern DataType delete_linkqueue(LinkQueue *q); #endif
#include "head.h" LinkQueue *create_empty_linkqueue() { LinkQueue *q = NULL; struct node *head = NULL; head = (struct node *)malloc(sizeof(struct node)); head->next = NULL; q = (LinkQueue *)malloc(sizeof(LinkQueue)); q->front = q->rear = head; return q; } int is_empty_linkqueue(LinkQueue *q) { return q->front == q->rear; } int enter_linkqueue(LinkQueue *q,DataType data) { struct node *temp = NULL; temp = (struct node *)malloc(sizeof(struct node)); temp->data = data; //將新結點尾插法插入連結串列尾 temp->next = q->rear->next; q->rear->next = temp; //更新rear q->rear = temp; return 0; } //刪頭法 DataType delete_linkqueue(LinkQueue *q) { struct node *temp = NULL; //儲存原頭結點首地址 temp = q->front; //更新front q->front = q->front->next; free(temp); temp = NULL; return q->front->data; } #if 0 int main() { LinkQueue *q = NULL; int i = 0; q = create_empty_linkqueue(); for(i = 0;i < 10;i++) { enter_linkqueue(q,i); } while(!is_empty_linkqueue(q)) { printf("%d ",delete_linkqueue(q)); } putchar('\n'); return 0; } #endif
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "head.h" #include <strings.h> #define N 9 typedef int VType; typedef struct { VType v[N]; int matrix[N][N]; }Graph; Graph *create_graph() { Graph *g = NULL; int i = 0; g = (Graph *)malloc(sizeof(Graph)); //memset(g,0,sizeof(Graph)); bzero(g,sizeof(Graph)); for(i = 0;i < N;i++) { g->v[i] = i; } return g; } void input_edge(Graph *g) { int i = 0,j = 0; printf("Input edge like (V0,V1) (V0,V2) ...\n"); //(V0,V1) (V0,V2) (V0,V3) ... while(scanf("(V%d,V%d) ",&i,&j) == 2)//scanf字串中空白符在輸入時可跳過空白符 { //getchar(); g->matrix[i][j] = g->matrix[j][i] = 1; } //清輸入緩衝區 while(getchar() != '\n'); return ; } void print_matrix(Graph *g) { int i = 0,j = 0; printf("%-4c",' '); for(i = 0;i < N;i++) { printf("V%-3d",i); } putchar('\n'); for(i = 0;i < N;i++) { printf("V%-3d",i); for(j = 0;j < N;j++) { printf("%-4d",g->matrix[i][j]); } putchar('\n'); } return ; } //訪問標誌陣列 int visited[N]; int first_adj(Graph *g,int v) { int i = 0; for(i = 0;i < N;i++) { if(g->matrix[v][i] == 1) { return i; } } return -1; } int next_adj(Graph *g,int v,int u) { int i = 0; for(i = u + 1;i < N;i++) { if(g->matrix[v][i] != 0) { return i; } } return -1; } void DFS(Graph *g,int v) { int u = 0; visited[v] = 1;//0 printf("V%d ",v); //first_adj return < 0 表示無第一鄰接點 u = first_adj(g,v);//1 while(u >= 0) { if(visited[u] == 0) { DFS(g,u);//1 /*visited[u] = 1;*//*{{{*/ /*printf("V%d ",u);*/ /*{*/ /*visited[v] = 1;//1*/ /*printf("V%d ",v);*/ /*//first_adj return < 0 表示無第一鄰接點*/ /*u = first_adj(g,v);//0*/ /*while(u >= 0)*/ /*{*/ /*if(visited[u] == 0)//0 3*/ /*{*/ /*DFS(g,u);//3*/ /*[>visited[u] = 1;<]*/ /*[>printf("V%d ",u);<]*/ /*{*/ /*visited[v] = 1;//3*/ /*printf("V%d ",v);*/ /*//first_adj return < 0 表示無第一鄰接點*/ /*u = first_adj(g,v);//1*/ /*while(u >= 0)*/ /*{*/ /*if(visited[u] == 0)//1 4*/ /*{*/ /*DFS(g,u);//4*/ /*[>visited[u] = 1;<]*/ /*[>printf("V%d ",u);<]*/ /*}*/ /*u = next_adj(g,v,u);//4*/ /*}*/ /*return ;*/ /*}*/ /*}*/ /*u = next_adj(g,v,u);//3*/ /*}*/ /*return ;*/ /*}*//*}}}*/ } u = next_adj(g,v,u); } //迴圈結束表示頂點v所有鄰接點訪問完 //回溯到上層 return ; } void BFS(Graph *g,int v) { int u = 0; LinkQueue *q = NULL; q = create_empty_linkqueue(); visited[v] = 1; enter_linkqueue(q,v); while(!is_empty_linkqueue(q)) { v = delete_linkqueue(q); printf("V%d ",v); u = first_adj(g,v); while(u >= 0) { if(visited[u] == 0) { visited[u] = 1; enter_linkqueue(q,u); } u = next_adj(g,v,u); } } putchar('\n'); return ; } int main() { Graph *g = NULL; g = create_graph(); input_edge(g); print_matrix(g); // DFS(g,0); BFS(g,0); putchar('\n'); /*input_edge(g);*/ /*puts("=============================");*/ /*print_matrix(g);*/ return 0; }