存檔:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define maxv 10 4 #define max 10 5 typedef char elem; 6 typedef int elemtype; 7 #include "queue.h" 8 #include "mgraph.h" 9 void main() 10 { 11 mgraph g; 12 printf("1.初始化函數測試:\n"); 13 initial(g); 14 printf("2.創建函數測試:\n"); 15 create(g); 16 printf("3.輸出函數測試:\n"); 17 printg(g); 18 printf("4.輸出頂點度函數測試:\n"); 19 degree(g); 20 printf("5.深度優先遍歷函數測試:\n"); 21 dfstraverse(g); 22 printf("6.廣度優先遍歷函數測試:\n"); 23 bfs(g); 24 }
1 //有向圖的鄰接矩陣,頂點數據為字符型 2 typedef struct MGraph 3 { 4 elem vexes[maxv];//頂點表 5 int edges[maxv][maxv];//鄰接矩陣 6 int n,e;//頂點數n和邊數e 7 }mgraph; 8 bool visited[maxv];//訪問標志數組 9 void initial(mgraph &g)//初始化函數 10 { 11 int i,j; 12 g.e=0; 13 g.n=0; 14 for(j=0;j<maxv;j++) 15 g.vexes[j]=0;//建立頂點表 16 for(i=0;i<maxv;i++) 17 { 18 for(j=0;j<maxv;j++) 19 { 20 g.edges[i][j]=0;//初始化鄰接矩陣 21 } 22 } 23 } 24 int locate(mgraph g,elem u)//查找頂點對應的數組下標值 25 { 26 for(int i=0;i<g.n;i++) 27 { 28 if(g.vexes[i]==u) 29 return i; 30 } 31 return -1; 32 } 33 void create(mgraph &g)//創建圖的鄰接矩陣存儲 34 { 35 int i,j,k; 36 elem u,v; 37 printf("請輸入有向圖的頂點數:"); 38 scanf("%d",&g.n); 39 printf("請輸入有向圖的弧數:"); 40 scanf("%d",&g.e); 41 fflush(stdin);//清空緩存中的數據 42 printf("請輸入字符型頂點數據,如ABCD:"); 43 for(j=0;j<g.n;j++) 44 scanf("%c",&g.vexes[j]);//建立頂點表 45 fflush(stdin); 46 printf("請輸入弧的信息,格式:弧尾,弧頭\n"); 47 for(k=0;k<g.e;k++) 48 { 49 scanf("%c,%c",&u,&v); 50 i=locate(g,u); 51 j=locate(g,v); 52 g.edges[i][j]=1; 53 fflush(stdin); 54 } 55 } 56 void printg(mgraph g)//輸出有向圖的鄰接矩陣 57 { 58 int i,j; 59 printf("輸入圖的鄰接矩陣存儲信息:\n"); 60 printf("頂點數據:\n"); 61 for(i=0;i<g.n;i++) 62 printf("%d:%c\n",i,g.vexes[i]); 63 printf("鄰接矩陣數據:\n"); 64 for(i=0;i<g.n;i++) 65 { 66 for(j=0;j<g.n;j++) 67 { 68 printf("%3d",g.edges[i][j]); 69 } 70 printf("\n"); 71 } 72 } 73 void degree(mgraph g)//輸出頂點的度 74 { 75 int i,j,in,out; 76 for(i=0;i<g.n;i++) 77 { 78 in=0; 79 out=0; 80 for(j=0;j<g.n;j++) 81 { 82 if(g.edges[i][j]!=0) 83 out++; 84 if(g.edges[j][i]!=0) 85 in++; 86 } 87 printf("頂點%c的出度為%d---入度為%d---度為%d\n",g.vexes[i],out,in,in+out); 88 } 89 } 90 int firstadjvex(mgraph g,int v)//頂點v的第一個鄰接頂點 91 { 92 for(int i=0;i<g.n;i++) 93 { 94 if(g.edges[v][i]==1) 95 return i; 96 } 97 return -1; 98 } 99 int nextadjvex(mgraph g,int v,int w)//頂點v的相對於w的下一個鄰接頂點 100 { 101 for(int i=w+1;i<g.n;i++) 102 { 103 if(g.edges[v][i]==1) 104 return i; 105 } 106 return -1; 107 } 108 void dfs(mgraph g,int v)//遍歷一個連通分量 109 { 110 int w; 111 visited[v]=true; 112 printf("%c ",g.vexes[v]); 113 for(w=firstadjvex(g,v);w>=0;w=nextadjvex(g,v,w)) 114 { 115 if(!visited[w]) 116 dfs(g,w); 117 } 118 } 119 void dfstraverse(mgraph g)//深度優先遍歷 120 { 121 int v; 122 for(v=0;v<g.n;v++) 123 visited[v]=false;//標志訪問數組初始化 124 for(v=0;v<g.n;v++) 125 { 126 if(!visited[v]) 127 dfs(g,v); 128 } 129 } 130 void bfs(mgraph g)//廣度優先遍歷 131 { 132 int u=0,v=0,w=0; 133 queue q; 134 for(v=0;v<g.n;v++) 135 visited[v]=false; 136 initqueue(q); 137 for(v=0;v<g.n;v++) 138 { 139 if(!visited[v]) 140 { 141 visited[v]=true; 142 printf("%c ",g.vexes[v]); 143 enqueue(q,v); 144 while(!queueempty(q)) 145 { 146 dequeue(q,u); 147 for(w=firstadjvex(g,u);w>=0;w=nextadjvex(g,u,w)) 148 { 149 if(!visited[w]) 150 { 151 visited[w]=true; 152 printf("%c ",g.vexes[w]); 153 enqueue(q,w); 154 } 155 } 156 } 157 } 158 } 159 destroyqueue(q); 160 }
1 typedef struct 2 { 3 elemtype *base;//動態分配存儲空間 4 int front;//頭指針,若隊列不空指向隊列頭元素 5 int rear;//尾指針,若隊列不空指向隊列尾元素的下一個位置 6 }queue; 7 void initqueue(queue &q)//初始化隊列 8 { 9 q.base=new elemtype[max];//分配存儲空間 10 if(!q.base) 11 { 12 printf("隊列分配失敗\n"); 13 exit(-2); 14 } 15 else 16 q.front=q.rear=0; 17 } 18 int queueempty(queue q)//判斷隊列是否為空 19 { 20 if(q.front==q.rear) 21 return 1; 22 else 23 return 0; 24 } 25 void enqueue(queue &q,elemtype e)//入隊列操作 26 { 27 if((q.rear+1)%max==q.front) 28 { 29 printf("隊滿,無法插入新元素!\n"); 30 exit(-2); 31 } 32 else 33 { 34 q.base[q.rear]=e; 35 q.rear=(q.rear+1)%max; 36 } 37 } 38 void dequeue(queue &q,elemtype e)//出隊列操作 39 { 40 if(q.front==q.rear)//判斷隊列是否為空 41 { 42 printf("空隊列,無法刪除頭元素!\n"); 43 exit(-2); 44 } 45 else 46 { 47 e=q.base[q.front]; 48 q.front=(q.front+1)%max; 49 } 50 } 51 void destroyqueue(queue &q)//銷毀隊列 52 { 53 free(q.base); 54 q.base=NULL; 55 q.front=0; 56 q.rear=0; 57 printf("\n"); 58 }
運行結果如下: