}
// GraphList.h(在此文的代碼基礎上修改 算法與數據結構基礎8:C++實現有向圖——鄰接表存儲)
#include#include #include #include using namespace std; // 邊 struct Edge{ int vName; int weight;// 權值 struct Edge* next; }; // 頂點(鏈表頭) struct Vertex{ int vName; int in;// 入度 int out; // 初度 struct Edge* next; }; // 有向圖 class GraphList { public: ~GraphList(); void createGraph(); void printGraph(); bool topsortInDegree(); bool topsortOutDegree(); private: // 1. 輸入定點數 void inputVertexCount(); // 2. 生成定點數組 void makeVertexArray(); // 3. 輸入邊數 void inputEdgeCount(); // 4. 輸入邊的起始點 void inputEdgeInfo(); // 5. 添加邊節點至對應的鏈表中 void addEdgeToList(int vFrom, int weight, int vTo); private: int m_vCount; int m_eCount; Vertex* m_vVertex; }; GraphList::~GraphList(){ for (int i = 0; i < m_vCount; ++i){ Edge* tmp = m_vVertex[i].next; Edge* edge = NULL; while(tmp){ edge = tmp; tmp = tmp->next; delete edge; edge = NULL; } } delete[] m_vVertex; } void GraphList::inputVertexCount() { cout << "please input count of vertex:"; cin >> m_vCount; } void GraphList::makeVertexArray() { m_vVertex = new Vertex[m_vCount]; // 初始化 for (int i = 0; i < m_vCount; ++i){ m_vVertex[i].vName = i; m_vVertex[i].next = NULL; m_vVertex[i].in = 0; m_vVertex[i].out = 0; } } void GraphList::inputEdgeCount() { cout << "please input count of edge:"; cin >> m_eCount; } void GraphList::inputEdgeInfo() { cout << "please input edge information:" << endl; for (int i = 0; i < m_eCount; ++i){ cout << "the edge " << i << ":" << endl; // 起點 int from = 0; cout << "From: "; cin >> from; // 權值 int weight = 0; cout << "Weight:"; cin >> weight; // 終點 int to = 0; cout << "To: "; cin >> to; cout << endl; addEdgeToList(from, weight, to); } } void GraphList::addEdgeToList(int vFrom, int weight, int vTo) { Edge* edge = new Edge(); edge->vName = vTo; edge->weight = weight; edge->next = NULL; Edge* tmp = m_vVertex[vFrom].next; if (tmp){ while(tmp->next){ tmp = tmp->next; } tmp->next = edge; }else{ m_vVertex[vFrom].next = edge; } ++m_vVertex[vTo].in; // 終點入度加1 ++m_vVertex[vFrom].out; // 起點初度加1 } void GraphList::printGraph() { for (int i = 0; i < m_vCount; ++i){ Edge* tmp = m_vVertex[i].next; cout << "list:" << m_vVertex[i].vName << "(in:" << m_vVertex[i].in << ")"<< "->"; while(tmp){ cout << "(weight:" << tmp->weight << ")"; cout << tmp->vName << "->"; tmp = tmp->next; } cout << "NULL" << endl; } } bool GraphList::topsortInDegree() { stack vertexStack; queue vertexQueue; int* degree = new int[m_vCount];// 聲明一個臨時變量,保存入度值,作操作,避免影響原始節點中的數據 // 1 統計入度為0的點 for(int i = 0; i < m_vCount; ++i){ degree[i] = m_vVertex[i].in; if(!degree[i]){ vertexStack.push(&m_vVertex[i]); } } int count = 0; while(!vertexStack.empty()){ // 保存入度為0的點 Vertex* tmp = vertexStack.top(); vertexStack.pop(); vertexQueue.push(tmp); ++count; // 2 從圖中刪除該結點以及它的所有出邊(即與之相鄰點入度減1) Edge* edge = tmp->next; while(edge){ Vertex* vertex = &m_vVertex[edge->vName]; --degree[edge->vName]; if (!degree[edge->vName]){ vertexStack.push(vertex); } edge = edge->next; } } // 判斷是否有環 if (count < m_vCount) { return false; } // 輸出排序結果 while(!vertexQueue.empty()){ Vertex* tmp = vertexQueue.front(); vertexQueue.pop(); cout << tmp->vName << " "; } cout << endl; delete[] degree; return true; } // ************************************************************************** // 流程控制 // ************************************************************************** void GraphList::createGraph() { inputVertexCount(); makeVertexArray(); inputEdgeCount(); inputEdgeInfo(); }
// main.cpp
// test for GraphList #include "GraphList.h" #includeint main() { GraphList graph; graph.createGraph(); graph.printGraph(); graph.topsort(); system("pause"); return 0; }
假如有圖如下:(就是用的前面兩節的圖)
結果: