資料結構:圖(鄰接表儲存 c++實現)
阿新 • • 發佈:2019-01-03
#include <iostream> #include <string> #include <queue> using namespace std; #define MAXVEX 10 #define INFINITY 0XFFFFFFFF #define SUCCESS 1 #define UNSUCCESS 0 typedef int Status; bool visited[MAXVEX]; //全域性陣列,記錄結點是否已補訪問 typedef int EdgeWeight; typedef struct EdgeNode { int adjvex; //鄰接點 EdgeWeight weight; //權值 struct EdgeNode* next; //指向下一條邊 }EdgeNode; typedef string VertexType; //頂點型別 typedef struct { VertexType data; EdgeNode* pFirstEdge; //指示第一條邊 }VertexNode; typedef VertexNode AdjList[MAXVEX];//鄰接表 typedef struct { AdjList adjList; //鄰接表 int iVexNum; //頂點個數 int iEdgeNum; //邊數 }AdjListGraph; //由頂點值得到頂點索引 int GetIndexByVertexVal( const AdjListGraph& G, VertexType val ) { for ( int i = 0; i < G.iVexNum; ++i ) { if ( val == G.adjList[i].data ) return i; } return -1; } //建立有向圖 Status CreateAdjListGraph( AdjListGraph& G ) { cout << "輸入頂點個數以及邊數:"; cin >> G.iVexNum >> G.iEdgeNum; cout << "請輸入" << G.iVexNum << "個頂點:"; for ( int i = 0; i < G.iVexNum; ++i ) { cin >> G.adjList[i].data; G.adjList[i].pFirstEdge = NULL; } cout << "請輸入由兩點構成的邊(" << G.iEdgeNum << "條):"; for ( int i = 0; i < G.iEdgeNum; ++i ) { VertexType first; VertexType second; cin >> first >> second; int m = GetIndexByVertexVal( G, first ); int n = GetIndexByVertexVal( G, second ); if ( m == -1 || n == -1 ) return UNSUCCESS; EdgeNode* pEdgeNode = new EdgeNode; pEdgeNode->adjvex = n; pEdgeNode->weight = 0; //權值暫時不用 //表頭插入法 pEdgeNode->next = G.adjList[m].pFirstEdge; G.adjList[m].pFirstEdge = pEdgeNode; } return SUCCESS; } //銷燬圖 void DestroyGraph( AdjListGraph& G ) { for ( int i = 0; i < G.iVexNum; ++i ) { EdgeNode* pEdge = G.adjList[i].pFirstEdge; while( pEdge ) { EdgeNode* q = pEdge; pEdge = pEdge->next; delete q; } G.adjList[i].pFirstEdge = NULL; } G.iVexNum = 0; G.iEdgeNum = 0; } //得到頂點的度 int GetVertexDegree( const AdjListGraph& G, VertexType val ) { int m = GetIndexByVertexVal( G, val );//得到頂點的下標 int iCount = 0; //頂點的度 for ( int i = 0; i < G.iVexNum; ++i ) { if ( i == m ) { EdgeNode* pEdgeOut = G.adjList[i].pFirstEdge; while ( pEdgeOut ) { ++iCount;//累加出度 pEdgeOut = pEdgeOut->next; } } else { EdgeNode* pEdgeIn = G.adjList[i].pFirstEdge; while ( pEdgeIn ) { if ( pEdgeIn->adjvex == m ) ++iCount; //累加入度 pEdgeIn = pEdgeIn->next; } } } return iCount; } //深度優先遍歷圖 void DFS( const AdjListGraph& G, int i ) { cout << G.adjList[i].data << " "; visited[i] = true; EdgeNode* pEdge = G.adjList[i].pFirstEdge; while( pEdge ) { int j = pEdge->adjvex; if ( !visited[j] ) { DFS( G, j); } pEdge = pEdge->next; } } void DFSTraverse( const AdjListGraph& G ) { for ( int i = 0; i < G.iVexNum; ++i ) { visited[i] = false; } for ( int i = 0; i < G.iVexNum; ++i ) { if ( !visited[i] ) DFS( G, i ); } } //廣度優先遍歷 void BFSTraverse( const AdjListGraph& G ) { for ( int i = 0; i < G.iVexNum; ++i ) { visited[i] = false; } queue<int> Q; for ( int i = 0; i < G.iVexNum; ++i ) { if ( !visited[i] ) { cout << G.adjList[i].data << " "; visited[i] = true; Q.push( i ); while( !Q.empty() ) { int iVex = Q.front(); Q.pop(); EdgeNode* pEdge = G.adjList[iVex].pFirstEdge; while ( pEdge ) { if ( !visited[pEdge->adjvex] ) { cout << G.adjList[pEdge->adjvex].data << " "; visited[pEdge->adjvex] = true; Q.push( pEdge->adjvex ); } pEdge = pEdge->next; } } } } } int main() { //建立有向圖 AdjListGraph G; CreateAdjListGraph( G ); //深度優先遍歷圖 DFSTraverse( G ); cout << endl << endl; //廣度優先遍歷圖 BFSTraverse( G ); cout << endl << endl; //結點的度 cout << "輸入求度的結點:"; VertexType v; cin >> v; cout << "度為:" << GetVertexDegree( G, v ) << endl; //銷燬有向圖 DestroyGraph( G ); return 0; }