資料結構:圖的實現--鄰接表,資料結構--鄰接表
圖的實現:鄰接表
當圖中的邊數較少時,用鄰接表來實現圖結構,則會浪費很多記憶體空間。因此,考慮另一種實現圖結構的方法:鄰接表。在鄰接表中主要有兩種節點結構體:
頂點節點
邊節點
直接看代碼
類定義
#include<iostream>#include<iomanip>using namespace std;//最大權值#define MAXWEIGHT 100//邊節點typedef struct edgenode_tag{int adjvex; //鄰接點int weight; //邊的權值struct edgenode_tag *next;}EdgeNode;//頂點節點typedef struct vertex_tag{int vertex; //頂點EdgeNode *next;}Vertex;class Graph{private://是否帶權bool isWeighted;//是否有向bool isDirected;//頂點數int numV;//邊數int numE;//鄰接表Vertex *adjList;public:/*構造方法numV是頂點數,isWeighted是否帶權值,isDirected是否有方向*/Graph(int numV, bool isWeighted = false, bool isDirected = false);//建圖void createGraph();//析構方法~Graph();//擷取頂點數int getVerNums(){return numV;}//擷取邊數int getEdgeNums(){return numE;}//插入邊void insertEdge(int tail, int head, int weight = 1);void insertedge(int tail, int head, int weight);//設定指定邊的權值void setEdgeWeight(int tail, int head, int weight);//列印鄰接表void printAdjacentList();//檢查輸入bool check(int tail, int head, int weight = 1);};
類實現
/*構造方法numV是頂點數,isWeighted是否帶權值,isDirected是否有方向*/Graph::Graph(int numV, bool isWeighted, bool isDirected){while (numV <= 0){cout << "輸入的頂點數不正確!,重新輸入 ";cin >> numV;}this->numV = numV;this->isWeighted = isWeighted;this->isDirected = isDirected;//邊數初始化為0numE = 0;adjList = new Vertex[numV]; //指標數組for (int i = 0; i < numV; i++){adjList[i].vertex = i;adjList[i].next = NULL;}}//建圖void Graph::createGraph(){//用一個新的變數表示邊數,numE的修改則留到insertedge()中int numEdge = 0;cout << "輸入邊數 ";while (cin >> numEdge && numEdge < 0)cout << "輸入有誤!,重新輸入 ";int i, j, w;if (!isWeighted) //無權圖{cout << "輸入每條邊的起點和終點:\n";for (int k = 0; k < numEdge; k++){cin >> i >> j;while (!check(i, j)){cout << "輸入的邊不對!重新輸入\n";cin >> i >> j;}insertEdge(i, j);}}else //有權圖{cout << "輸入每條邊的起點、終點和權值:\n";for (int k = 0; k < numEdge; k++){cin >> i >> j >> w;while (!check(i, j, w)){cout << "輸入的邊不對!重新輸入\n";cin >> i >> j >> w;}insertEdge(i, j, w);}}}//析構方法Graph::~Graph(){int i;EdgeNode *p, *q;for (i = 0; i < numV; i++){if (adjList[i].next){p = adjList[i].next;while (p){q = p->next;delete p;p = q;}}}delete[]adjList;}//設定指定邊的權值void Graph::setEdgeWeight(int tail, int head, int weight){if (!isWeighted) //無權圖{while (!check(tail, head)){cout << "輸入的邊不對!重新輸入\n";cin >> tail >> head;}insertEdge(tail, head);}else //有權圖{while (!check(tail, head, weight)){cout << "輸入的邊不對!重新輸入\n";cin >> tail >> head >> weight;}insertEdge(tail, head, weight);}}//插入邊void Graph::insertEdge(int vertex, int adjvex, int weight){insertedge(vertex, adjvex, weight);if (!isDirected) //無向圖insertedge(adjvex, vertex, weight);}void Graph::insertedge(int vertex, int adjvex, int weight){EdgeNode *p, *q, *r;p = q = r = NULL;if (adjList[vertex].next) //非第一個節點{p = adjList[vertex].next;//移動p到合適位置while (p && (p->adjvex < adjvex)){q = p;p = p->next;}if (p && (p->adjvex == adjvex)) //修改已有邊權值p->weight = weight;else{r = new EdgeNode;r->adjvex = adjvex;r->weight = weight;r->next = p;//當加入的新節點位於表的第一個位置if (adjList[vertex].next == p)adjList[vertex].next = r;elseq->next = r;numE++;}}else{p = new EdgeNode;p->adjvex = adjvex;p->weight = weight;p->next = NULL;adjList[vertex].next = p;numE++;}}//輸入檢查bool Graph::check(int tail, int head, int weight){if (tail >= 0 && tail < numV && head >= 0 && head < numV && weight > 0 && weight <= MAXWEIGHT)return true;elsereturn false;}//列印鄰接表void Graph::printAdjacentList(){int i;EdgeNode *edge = NULL;for (i = 0; i < numV; i++){edge = adjList[i].next;if (edge) //為什麼加一個if判斷?跟後面的換行有關。若某頂點無鄰接點(無邊),則會空出一行{while (edge){cout << "w(" << i << "," << edge->adjvex << ")=" << edge->weight << " ";edge = edge->next;}cout << endl;}}}主函數
int main(void){cout << "******使用鄰接表實現圖結構***by David***" << endl;bool isDirected, isWeighted;int numV;cout << "建圖" << endl;cout << "輸入頂點數 ";cin >> numV;cout << "邊是否帶權值,0(不帶) or 1(帶) ";cin >> isWeighted;cout << "是否是有向圖,0(無向) or 1(有向) ";cin >> isDirected;Graph graph(numV, isWeighted, isDirected);cout << "這是一個";isDirected ? cout << "有向、" : cout << "無向、";isWeighted ? cout << "有權圖" << endl : cout << "無權圖" << endl;graph.createGraph();cout << "列印鄰接表" << endl;graph.printAdjacentList();cout << endl;int tail, head, weight;cout << "修改指定邊的權值" << endl;if (isWeighted) //針對有權圖{cout << "輸入邊的起點、終點和權值 ";cin >> tail >> head >> weight;graph.setEdgeWeight(tail, head, weight);}else //針對無權圖{cout << "輸入邊的起點、終點 ";cin >> tail >> head;graph.setEdgeWeight(tail, head, 1);}cout << "修改成功!" << endl;cout << "列印鄰接矩陣" << endl;graph.printAdjacentList();system("pause");return 0;}運行
鄰接表
完整代碼下載:圖的實現:鄰接表
轉載請註明出處,本文地址:http://blog.csdn.net/zhangxiangdavaid/article/details/38323593
若有所協助,頂一個哦!
專欄目錄:資料結構與演算法目錄
資料結構:圖的鄰接表實現
發給你郵箱裡了,給分!!!!!
資料結構:圖的鄰接表實現
已發你郵箱