1.輸入指定的邊數和頂點數建立圖,並輸出深度優先遍歷和廣度優先遍歷的結果。
1)問題描述:在主程式中設計乙個簡單的選單,分別呼叫相應的函式功能:
1…圖的建立
2…深度優先遍歷圖
3…廣度優先遍歷圖
0…結束
2)實驗要求:在程式中定義下述函式,並實現要求的函式功能:
creategraph(): 按從鍵盤的資料建立圖
dfsgrahp():深度優先遍歷圖
bfsgrahp():廣度優先遍歷圖
圖的儲存可採用鄰接表或鄰接矩陣;
圖儲存資料型別定義 (鄰接表儲存)
# define max_vertex_num 8 //頂點最大個數
typedef struct arcnode
arcnode; //表結點
# define vertextype int //頂點元素型別
typedef struct vnode
vnode /*頭結點*/, adjlist[max_vertex_num];
typedef structalgraph;
4)注意問題:
注意理解各演算法實現時所採用的儲存結構。
注意區別正、逆鄰接。
#includeusing namespace std;
#include#include//c++的佇列庫函式
#include# define max_vertex_num 8 //頂點最大個數
typedef struct arcnodearcnode;
typedef struct vnodevnode, adjlist[max_vertex_num];
typedef structgraph;
void creategraph(graph &g)
for(i=1; i<=e; i++)
for(i=1;i<=n;i++);
arcnode* r;
r = (arcnode*)malloc(sizeof(arcnode));
r = g.vertex[i].firstarc;
while(r!=null)
sort(paixu,paixu+100);
r = g.vertex[i].firstarc;
count =100-count;
while(r!=null)
}} int flag[100] = ;
int con=1;
void dfs(graph g,int v)
p = p->nextarc; }}
int flag1[100] = ;
int con1 = 1;
void bfs(graph g,int v)
else
flag1[p->adjvex] = 1;
q.push(p->adjvex);
}p = p->nextarc;
} }}int main()arcnode; //表結點
#define vertextype int //頂點元素型別
typedef struct vnodevnode, adjlist[max_vertex_num];
typedef structgraph;
void creategraph(graph &g)
cout<<"請輸入各邊"g.vertex[d].indegree++;
s->nextarc = g.vertex[k].firstarc;
g.vertex[k].firstarc = s;
} for(i=1;i<=n;i++);
arcnode* r;
r = (arcnode*)malloc(sizeof(arcnode));
r = g.vertex[i].firstarc;
while(r!=null)
sort(paixu,paixu+100);
r = g.vertex[i].firstarc;
count =100-count;
while(r!=null)
}} int ts(graph g)
} cout} }}
int main()adjmatrix[500][500]; //矩陣
typedef struct graph;
int main()
} int m1 = m;
int a,b,c;
int p;
cout<<"請輸入邊資訊:"<>a>>b>>c;
g.arcs[a][b].quan = c;
} int q[1000];
for(i=0;i<=1000;i++)
int n1 = n-1;
int nowi = 1, nowq = 0,nowj = 1;
int z = 9999;
while(n1--)
} if(g.arcs[nowi][j].quan < min)
}} for(j=1;j<=n;j++)
} nowq = nowq + min;
q[nowj] = nowq;
int min1 = 99999;
for(i=1;i<=n;i++)
}nowq = min1;
if(nowi==n && nowq4.利用最小生成樹演算法解決通訊網的總造價最低問題
1)問題描述:若在n個城市之間建通訊網路,架設n-1條線路即可。如何以最低的經濟代價建設這個通訊網,是乙個網路的最小生成樹問題。
2)實驗要求:利用prim演算法求網的最小生成樹。
#includeusing namespace std;
typedef struct arcadjmatrix[500][500]; //矩陣
typedef struct graph;
int main()
} int m1 = m;
int a,b,c;
int p;
cout<<"請輸入各邊資訊"<>a>>b>>c;
g.arcs[a][b].quan = c;
g.arcs[b][a].quan = c;
} int u[1000] = ;
int v[1000] = ;
for(i=2;i<=n;i++)
u[1] = 1;
int n1 = n-1;
int nowi,nowj,sum=0;
while(n1--)
j++;} }
i++;
}g.arcs[nowi][nowj].quan = 99999;
g.arcs[nowj][nowi].quan = 99999;
sum += min;
u[nowj] = nowj;
v[nowj] = 0;
} cout<<"最小代價為:"
}
資料結構實驗報告 資料結構實驗報告
使用c語言中的陣列,實現線性表中的順序結構儲存的查詢 刪除操作。1 初始線性表通過陣列 迴圈 scanf語句實現輸入任意個整數。2 刪除操作的實現,任意輸入乙個要刪除的整數,找到這個元素,將此元素之後的所有元素逐個前移一位,實現刪除操作。3 要求以上2步操作可以重複執行。4 例如 刪除操作執行結果大...
資料結構實驗報告
1 掌握佇列儲存結構的表示和實現方法。2 掌握佇列的入隊和出隊等基本操作的演算法實現。3 了解佇列在解決實際問題中的簡單應用。1 建立順序迴圈佇列,並在順序迴圈佇列上實現入隊 出隊基本操作 驗證性內容 2 建立迴圈鏈佇列,並在迴圈鏈佇列上實現入隊 出隊基本操作 設計性內容 3 實現鍵盤輸入迴圈緩衝區...
資料結構實驗報告
1 理解二叉樹的型別定義與性質。2 掌握二叉樹的二叉鍊錶儲存結構的表示和實現方法。3 掌握二叉樹遍歷操作的演算法實現。4 熟悉二叉樹遍歷操作的應用。1 建立二叉樹的二叉鍊錶儲存結構。2 實現二叉樹的先序 中序和後序三種遍歷操作 驗證性內容 3 應用二叉樹的遍歷操作來實現判斷兩棵二叉樹是否相等的操作 ...