#include
#include
#include "f.h"
void prim(mgraph g,int v)
for (i=1; i//找出n-1個頂點
printf(" 邊(%d,%d)權為:%d\n",closest[k],k,min);
lowcost[k]=0; //標記k已經加入u
for (j=0; j//修改陣列lowcost和closest
if (g.edges[k][j]!=0 && g.edges[k][j]int main()
, ,,,
,};arraytomat(a[0], 6, g);
printf("最小生成樹構成:\n");
prim(g,0);
return
0;}
f.h』
#ifndef graph_h_included
#define graph_h_included
#define maxv 100 //最大頂點個數
#define inf 32767 //inf表示∞
typedef
int infotype;
//以下定義鄰接矩陣型別
typedef
struct
vertextype; //頂點型別
typedef
struct
//圖的定義
mgraph; //圖的鄰接矩陣型別
//以下定義鄰接表型別
typedef
struct anode //弧的結點結構型別
arcnode;
typedef
int vertex;
typedef
struct vnode //鄰接表頭結點的型別
vnode;
typedef vnode adjlist[maxv]; //adjlist是鄰接表型別
typedef
struct
algraph; //圖的鄰接表型別
//功能:由乙個反映圖中頂點鄰接關係的二維陣列,構造出用鄰接矩陣儲存的圖
//引數:arr - 陣列名,由於形式引數為二維陣列時必須給出每行的元素個數,在此將引數arr宣告為一維陣列名(指向int的指標)
// n - 矩陣的階數
// g - 要構造出來的鄰接矩陣資料結構
void arraytomat(int *arr, int n, mgraph &g); //用普通陣列構造圖的鄰接矩陣
void arraytolist(int *arr, int n, algraph *&); //用普通陣列構造圖的鄰接表
void mattolist(mgraph g,algraph *&g);//將鄰接矩陣g轉換成鄰接表g
void listtomat(algraph *g,mgraph &g);//將鄰接表g轉換成鄰接矩陣g
void dispmat(mgraph g);//輸出鄰接矩陣g
void dispadj(algraph *g);//輸出鄰接表g
#endif // graph_h_included
f.cpp
#include
#include
#include "f.h"
//功能:由乙個反映圖中頂點鄰接關係的二維陣列,構造出用鄰接矩陣儲存的圖
//引數:arr - 陣列名,由於形式引數為二維陣列時必須給出每行的元素個數,在此將引數arr宣告為一維陣列名(指向int的指標)
// n - 矩陣的階數
// g - 要構造出來的鄰接矩陣資料結構
void arraytomat(int *arr, int n, mgraph &g)
g.e=count;
}void arraytolist(int *arr, int n, algraph *&g)
g->e=count;
}void mattolist(mgraph g, algraph *&g)
//將鄰接矩陣g轉換成鄰接表g
g->n=g.n;
g->e=g.e;
}void listtomat(algraph *g,mgraph &g)
//將鄰接表g轉換成鄰接矩陣g
}}void dispmat(mgraph g)
//輸出鄰接矩陣g
}void dispadj(algraph *g)
//輸出鄰接表g
第十二周專案 1
分別定義teacher 教師 類和cadre 幹部 類,採用多重繼承方式由這兩個類派生出新類teacher cadre 教師兼幹部 要求 1 在兩個基類中都包含姓名 年齡 性別 位址 等資料成員。2 在teacher類中還包含資料成員title 職稱 在cadre類中還包含資料成員post 職務 在...
第十二周專案1
輸入描述 無 輸出描述 圖的基本運算 標頭檔案 ifndef graph h included define graph h included define maxv 100 最大頂點個數 define inf 32767 inf表示 typedef int infotype 以下定義鄰接矩陣型別 ...
第十二周專案2
j 輸入描述 無 輸出描述 描述的輸出內容 include include include graph.h 返回圖g中編號為v的頂點的出度 int outdegree algraph g,int v return n 輸出圖g中每個頂點的出度 void outds algraph g 輸出圖g中出度...