最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)有向圖的鄰接表表示

 更新時(shí)間:2020年04月26日 11:45:01   作者:ChanJose  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)有向圖的鄰接表表示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C++有向圖的鄰接表表示,供大家參考,具體內(nèi)容如下

一、思路:

有向圖的插入有向邊、刪除邊、刪除頂點(diǎn)和無(wú)向圖的有區(qū)別。其他的和無(wú)向圖的類(lèi)似。

1.插入有向邊<e1, e2>

只需要插入<e1, e2>邊就行,不需要插入對(duì)稱(chēng)邊<e2, e1>

2.刪除邊<e1,e2>:

 只需要?jiǎng)h除<e1, e2>邊就行,不需要仔找對(duì)稱(chēng)邊<e2, e1>進(jìn)行刪除。

3.刪除頂點(diǎn)v:

首先,要在鄰接表中刪除以v為頭的邊<v, w>;

同時(shí),也要在鄰接表中刪除以v為尾的邊<k, v>, 不能通過(guò)對(duì)稱(chēng)邊來(lái)找,只能一個(gè)個(gè)頂點(diǎn)找,浪費(fèi)時(shí)間。

二、實(shí)現(xiàn)程序

1.DirectedGraph.h:有向圖

#ifndef DirectedGraph_h
#define DirectedGraph_h
#include <iostream>
using namespace std;
 
const int DefaultVertices = 30;
 
template <class T, class E>
struct Edge { // 邊結(jié)點(diǎn)的定義
 int dest; // 邊的另一頂點(diǎn)位置
 E cost; // 表上的權(quán)值
 Edge<T, E> *link; // 下一條邊鏈指針
};
 
template <class T, class E>
struct Vertex { // 頂點(diǎn)的定義
 T data; // 頂點(diǎn)的名字
 Edge<T, E> *adj; // 邊鏈表的頭指針
};
 
template <class T, class E>
class Graphlnk {
public:
 const E maxValue = 100000; // 代表無(wú)窮大的值(=∞)
 Graphlnk(int sz=DefaultVertices); // 構(gòu)造函數(shù)
 ~Graphlnk(); // 析構(gòu)函數(shù)
 void inputGraph(); // 建立鄰接表表示的圖
 void outputGraph(); // 輸出圖中的所有頂點(diǎn)和邊信息
 T getValue(int i); // 取位置為i的頂點(diǎn)中的值
 E getWeight(int v1, int v2); // 返回邊(v1, v2)上的權(quán)值
 bool insertVertex(const T& vertex); // 插入頂點(diǎn)
 bool insertEdge(int v1, int v2, E weight); // 插入邊
 bool removeVertex(int v); // 刪除頂點(diǎn)
 bool removeEdge(int v1, int v2); // 刪除邊
 int getFirstNeighbor(int v); // 取頂點(diǎn)v的第一個(gè)鄰接頂點(diǎn)
 int getNextNeighbor(int v,int w); // 取頂點(diǎn)v的鄰接頂點(diǎn)w的下一鄰接頂點(diǎn)
 int getVertexPos(const T vertex); // 給出頂點(diǎn)vertex在圖中的位置
 int numberOfVertices(); // 當(dāng)前頂點(diǎn)數(shù)
private:
 int maxVertices; // 圖中最大的頂點(diǎn)數(shù)
 int numEdges; // 當(dāng)前邊數(shù)
 int numVertices; // 當(dāng)前頂點(diǎn)數(shù)
 Vertex<T, E> * nodeTable; // 頂點(diǎn)表(各邊鏈表的頭結(jié)點(diǎn))
};
 
// 構(gòu)造函數(shù):建立一個(gè)空的鄰接表
template <class T, class E>
Graphlnk<T, E>::Graphlnk(int sz) {
 maxVertices = sz;
 numVertices = 0;
 numEdges = 0;
 nodeTable = new Vertex<T, E>[maxVertices]; // 創(chuàng)建頂點(diǎn)表數(shù)組
 if(nodeTable == NULL) {
  cerr << "存儲(chǔ)空間分配錯(cuò)誤!" << endl;
  exit(1);
 }
 for(int i = 0; i < maxVertices; i++)
  nodeTable[i].adj = NULL;
}
 
// 析構(gòu)函數(shù)
template <class T, class E>
Graphlnk<T, E>::~Graphlnk() {
 // 刪除各邊鏈表中的結(jié)點(diǎn)
 for(int i = 0; i < numVertices; i++) {
  Edge<T, E> *p = nodeTable[i].adj; // 找到其對(duì)應(yīng)鏈表的首結(jié)點(diǎn)
  while(p != NULL) { // 不斷地刪除第一個(gè)結(jié)點(diǎn)
   nodeTable[i].adj = p->link;
   delete p;
   p = nodeTable[i].adj;
  }
 }
 delete []nodeTable; // 刪除頂點(diǎn)表數(shù)組
}
 
// 建立鄰接表表示的圖
template <class T, class E>
void Graphlnk<T, E>::inputGraph() {
 int n, m; // 存儲(chǔ)頂點(diǎn)樹(shù)和邊數(shù)
 int i, j, k;
 T e1, e2; // 頂點(diǎn)
 E weight; // 邊的權(quán)值
 
 cout << "請(qǐng)輸入頂點(diǎn)數(shù)和邊數(shù):" << endl;
 cin >> n >> m;
 cout << "請(qǐng)輸入各頂點(diǎn):" << endl;
 for(i = 0; i < n; i++) {
  cin >> e1;
  insertVertex(e1); // 插入頂點(diǎn)
 }
 
 cout << "請(qǐng)輸入圖的各邊的信息:" << endl;
 i = 0;
 while(i < m) {
  cin >> e1 >> e2 >> weight;
  j = getVertexPos(e1);
  k = getVertexPos(e2);
  if(j == -1 || k == -1)
   cout << "邊兩端點(diǎn)信息有誤,請(qǐng)重新輸入!" << endl;
  else {
   insertEdge(j, k, weight); // 插入邊
   i++;
  }
 } // while
}
 
// 輸出有向圖中的所有頂點(diǎn)和邊信息
template <class T, class E>
void Graphlnk<T, E>::outputGraph() {
 int n, m, i;
 T e1, e2; // 頂點(diǎn)
 E weight; // 權(quán)值
 Edge<T, E> *p;
 
 n = numVertices;
 m = numEdges;
 cout << "圖中的頂點(diǎn)數(shù)為" << n << ",邊數(shù)為" << m << endl;
 for(i = 0; i < n; i++) {
  p = nodeTable[i].adj;
  while(p != NULL) {
   e1 = getValue(i); // 有向邊<i, p->dest>
   e2 = getValue(p->dest);
   weight = p->cost;
   cout << "<" << e1 << ", " << e2 << ", " << weight << ">" << endl;
   p = p->link; // 指向下一個(gè)鄰接頂點(diǎn)
  }
 }
}
 
// 取位置為i的頂點(diǎn)中的值
template <class T, class E>
T Graphlnk<T, E>::getValue(int i) {
 if(i >= 0 && i < numVertices)
  return nodeTable[i].data;
 return NULL;
}
 
// 返回邊(v1, v2)上的權(quán)值
template <class T, class E>
E Graphlnk<T, E>::getWeight(int v1, int v2) {
 if(v1 != -1 && v2 != -1) {
  Edge<T , E> *p = nodeTable[v1].adj; // v1的第一條關(guān)聯(lián)的邊
  while(p != NULL && p->dest != v2) { // 尋找鄰接頂點(diǎn)v2
   p = p->link;
  }
  if(p != NULL)
   return p->cost;
 }
 return maxValue; // 邊(v1, v2)不存在,就存放無(wú)窮大的值
}
 
// 插入頂點(diǎn)
template <class T, class E>
bool Graphlnk<T, E>::insertVertex(const T& vertex) {
 if(numVertices == maxVertices) // 頂點(diǎn)表滿(mǎn),不能插入
  return false;
 nodeTable[numVertices].data = vertex; // 插入在表的最后
 numVertices++;
 return true;
}
 
// 插入邊
template <class T, class E>
bool Graphlnk<T, E>::insertEdge(int v1, int v2, E weight) {
 if(v1 >= 0 && v1 < numVertices && v2 >= 0 && v2 < numVertices) {
  Edge<T, E> *p = nodeTable[v1].adj; // v1對(duì)應(yīng)的邊鏈表頭指針
  while(p != NULL && p->dest != v2) // 尋找鄰接頂點(diǎn)v2
   p = p->link;
  if(p != NULL) // 已存在該邊,不插入
   return false;
  p = new Edge<T, E>; // 創(chuàng)建新結(jié)點(diǎn)
  p->dest = v2;
  p->cost = weight;
  p->link = nodeTable[v1].adj; // 鏈入v1邊鏈表
  nodeTable[v1].adj = p;
  numEdges++;
  return true;
 }
 return false;
}
 
// 有向圖刪除頂點(diǎn)較麻煩
template <class T, class E>
bool Graphlnk<T, E>::removeVertex(int v) {
 if(numVertices == 1 || v < 0 || v > numVertices)
  return false; // 表空或頂點(diǎn)號(hào)超出范圍
 
 Edge<T, E> *p, *s;
 // 1.清除頂點(diǎn)v的邊鏈表結(jié)點(diǎn)w 邊<v,w>
 while(nodeTable[v].adj != NULL) {
  p = nodeTable[v].adj;
  nodeTable[v].adj = p->link;
  delete p;
  numEdges--; // 與頂點(diǎn)v相關(guān)聯(lián)的邊數(shù)減1
 } // while結(jié)束
 // 2.清除<w, v>,與v有關(guān)的邊
 for(int i = 0; i < numVertices; i++) {
  if(i != v) { // 不是當(dāng)前頂點(diǎn)v
   s = NULL;
   p = nodeTable[i].adj;
   while(p != NULL && p->dest != v) {// 在頂點(diǎn)i的鏈表中找v的頂點(diǎn)
    s = p;
    p = p->link; // 往后找
   }
   if(p != NULL) { // 找到了v的結(jié)點(diǎn)
    if(s == NULL) { // 說(shuō)明p是nodeTable[i].adj
     nodeTable[i].adj = p->link;
    } else {
     s->link = p->link; // 保存p的下一個(gè)頂點(diǎn)信息
    }
    delete p; // 刪除結(jié)點(diǎn)p
    numEdges--; // 與頂點(diǎn)v相關(guān)聯(lián)的邊數(shù)減1
   }
  }
 }
 numVertices--; // 圖的頂點(diǎn)個(gè)數(shù)減1
 nodeTable[v].data = nodeTable[numVertices].data; // 填補(bǔ),此時(shí)numVertices,比原來(lái)numVertices小1,所以,這里不需要numVertices-1
 nodeTable[v].adj = nodeTable[numVertices].adj;
 // 3.要將填補(bǔ)的頂點(diǎn)對(duì)應(yīng)的位置改寫(xiě)
 for(int i = 0; i < numVertices; i++) {
  p = nodeTable[i].adj;
  while(p != NULL && p->dest != numVertices) // 在頂點(diǎn)i的鏈表中找numVertices的頂點(diǎn)
   p = p->link; // 往后找
  if(p != NULL) // 找到了numVertices的結(jié)點(diǎn)
   p->dest = v; // 將鄰接頂點(diǎn)numVertices改成v
 }
 return true;
}
 
// 刪除邊
template <class T, class E>
bool Graphlnk<T, E>::removeEdge(int v1, int v2) {
 if(v1 != -1 && v2 != -1) {
  Edge<T, E> * p = nodeTable[v1].adj, *q = NULL;
  while(p != NULL && p->dest != v2) { // v1對(duì)應(yīng)邊鏈表中找被刪除邊
   q = p;
   p = p->link;
  }
  if(p != NULL) { // 找到被刪除邊結(jié)點(diǎn)
   if(q == NULL) // 刪除的結(jié)點(diǎn)是邊鏈表的首結(jié)點(diǎn)
    nodeTable[v1].adj = p->link;
   else
    q->link = p->link; // 不是,重新鏈接
   delete p;
   return true;
  }
 }
 return false; // 沒(méi)有找到結(jié)點(diǎn)
}
 
// 取頂點(diǎn)v的第一個(gè)鄰接頂點(diǎn)
template <class T, class E>
int Graphlnk<T, E>::getFirstNeighbor(int v) {
 if(v != -1) {
  Edge<T, E> *p = nodeTable[v].adj; // 對(duì)應(yīng)鏈表第一個(gè)邊結(jié)點(diǎn)
  if(p != NULL) // 存在,返回第一個(gè)鄰接頂點(diǎn)
   return p->dest;
 }
 return -1; // 第一個(gè)鄰接頂點(diǎn)不存在
}
 
// 取頂點(diǎn)v的鄰接頂點(diǎn)w的下一鄰接頂點(diǎn)
template <class T, class E>
int Graphlnk<T, E>::getNextNeighbor(int v,int w) {
 if(v != -1) {
  Edge<T, E> *p = nodeTable[v].adj; // 對(duì)應(yīng)鏈表第一個(gè)邊結(jié)點(diǎn)
  while(p != NULL && p->dest != w) // 尋找鄰接頂點(diǎn)w
   p = p->link;
  if(p != NULL && p->link != NULL)
   return p->link->dest; // 返回下一個(gè)鄰接頂點(diǎn)
 }
 return -1; // 下一個(gè)鄰接頂點(diǎn)不存在
}
 
// 給出頂點(diǎn)vertex在圖中的位置
template <class T, class E>
int Graphlnk<T, E>::getVertexPos(const T vertex) {
 for(int i = 0; i < numVertices; i++)
  if(nodeTable[i].data == vertex)
   return i;
 return -1;
}
 
// 當(dāng)前頂點(diǎn)數(shù)
template <class T, class E>
int Graphlnk<T, E>::numberOfVertices() {
 return numVertices;
}
 
#endif /* DirectedGraph_h */

2.main.cpp

/*
 測(cè)試數(shù)據(jù):
5 7
0 1 2 3 4
0 1 10
0 3 30
0 4 100
1 2 50
2 4 10
3 2 20
3 4 60
 */
 
#include "DirectedGraph.h"
 
int main(int argc, const char * argv[]) {
 Graphlnk<char, int> st; // 聲明對(duì)象
 bool finished = false;
 int choice;
 char e1, e2, next;
 int weight;
 
 while(!finished) {
  cout << "[1]創(chuàng)建基于鄰接表的有向圖" << endl;
  cout << "[2]輸出圖的所有頂點(diǎn)和邊信息" << endl;
  cout << "[3]取頂點(diǎn)v的第一個(gè)鄰接頂點(diǎn)" << endl;
  cout << "[4]取v的鄰接頂點(diǎn)w的下一個(gè)鄰接頂點(diǎn)" << endl;
  cout << "[5]插入頂點(diǎn)" << endl;
  cout << "[6]插入邊" << endl;
  cout << "[7]刪除頂點(diǎn)" << endl;
  cout << "[8]刪除邊" << endl;
  cout << "[9]退出" << endl;
  cout << "請(qǐng)輸入選擇[1-9]:";
  cin >> choice;
  switch(choice) {
   case 1:
    st.inputGraph();
    break;
   case 2:
    st.outputGraph();
    break;
   case 3:
    cout << "請(qǐng)輸入頂點(diǎn):";
    cin >> e1;
    e2 = st.getValue(st.getFirstNeighbor(st.getVertexPos(e1)));
    if(e2)
     cout << "頂點(diǎn)" << e1 << "的第一個(gè)鄰接頂點(diǎn)為:" << e2 << endl;
    else
     cout << "頂點(diǎn)" << e1 << "沒(méi)有鄰接頂點(diǎn)!" << endl;
    break;
   case 4:
    cout << "請(qǐng)輸入頂點(diǎn)v和鄰接頂點(diǎn)w:";
    cin >> e1 >> e2;
    next = st.getValue(st.getNextNeighbor(st.getVertexPos(e1), st.getVertexPos(e2)));
    if(next)
     cout << "頂點(diǎn)" << e1 << "的鄰接頂點(diǎn)" << e2 << "的下一個(gè)鄰接頂點(diǎn)為:" << next << endl;
    else
     cout << "頂點(diǎn)" << e1 << "的鄰接頂點(diǎn)" << e2 << "沒(méi)有下一個(gè)鄰接頂點(diǎn)!" << endl;
    break;
   case 5:
    cout << "請(qǐng)輸入要插入的頂點(diǎn):";
    cin >> e1;
    if(st.insertVertex(e1))
     cout << "插入成功!" << endl;
    else
     cout << "表已滿(mǎn),插入失敗!" << endl;
    break;
   case 6:
    cout << "請(qǐng)輸入要插入的邊的信息:" << endl;
    cin >> e1 >> e2 >> weight;
    st.insertEdge(st.getVertexPos(e1), st.getVertexPos(e2), weight);
    break;
   case 7:
    cout << "請(qǐng)輸入要?jiǎng)h除的頂點(diǎn):";
    cin >> e1;
    if(st.removeVertex(st.getVertexPos(e1)))
     cout << "頂點(diǎn)" << e1 << "已刪除!" << endl;
    else
     cout << "頂點(diǎn)" << e1 << "不在圖中!" << endl;
    break;
   case 8:
    cout << "請(qǐng)輸入要?jiǎng)h除的邊的兩個(gè)端點(diǎn):" << endl;
    cin >> e1 >> e2;
    st.removeEdge(st.getVertexPos(e1), st.getVertexPos(e2));
    break;
   case 9:
    finished = true;
    break;
   default:
    cout << "選擇輸入錯(cuò)誤,請(qǐng)重新輸入!" << endl;
  }
 }
 return 0;
}

測(cè)試結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++ OpenCV實(shí)戰(zhàn)之制作九宮格圖像

    C++ OpenCV實(shí)戰(zhàn)之制作九宮格圖像

    本文將為大家介紹如何使用OpenCV C++ 制作九宮格圖像,即將一張圖像均等分成九份,然后將這九個(gè)小塊按一定間隔拷貝到新畫(huà)布上就可以啦。感興趣的可以動(dòng)手試一試
    2022-01-01
  • C++中SetConsoleCursorPosition()移動(dòng)光標(biāo)函數(shù)的用法大全

    C++中SetConsoleCursorPosition()移動(dòng)光標(biāo)函數(shù)的用法大全

    這篇文章主要介紹了C++中SetConsoleCursorPosition()移動(dòng)光標(biāo)函數(shù)的用法大全,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • C++線程之thread詳解

    C++線程之thread詳解

    這篇文章主要為大家詳細(xì)介紹了C++線程中的thread,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • C++?qsort函數(shù)排序與冒泡模擬實(shí)現(xiàn)流程詳解

    C++?qsort函數(shù)排序與冒泡模擬實(shí)現(xiàn)流程詳解

    qsort是一個(gè)庫(kù)函數(shù),基于快速排序算法實(shí)現(xiàn)的一個(gè)排序的函數(shù),下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言qsort()函數(shù)使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • C++短路求值(邏輯與、邏輯或)實(shí)例

    C++短路求值(邏輯與、邏輯或)實(shí)例

    這篇文章主要介紹了C++短路求值(邏輯與、邏輯或)實(shí)例,以實(shí)例形式講述了邏輯或的短路與邏輯與的短路及相應(yīng)的應(yīng)用實(shí)例,需要的朋友可以參考下
    2014-10-10
  • 仿現(xiàn)代C++智能指針實(shí)現(xiàn)引用計(jì)數(shù)

    仿現(xiàn)代C++智能指針實(shí)現(xiàn)引用計(jì)數(shù)

    這篇文章主要為大家詳細(xì)介紹了如何仿現(xiàn)代C++智能指針實(shí)現(xiàn)引用計(jì)數(shù),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以了解下
    2024-03-03
  • C++實(shí)現(xiàn)LeetCode(65.驗(yàn)證數(shù)字)

    C++實(shí)現(xiàn)LeetCode(65.驗(yàn)證數(shù)字)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(65.驗(yàn)證數(shù)字),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C 語(yǔ)言條件運(yùn)算符詳細(xì)講解

    C 語(yǔ)言條件運(yùn)算符詳細(xì)講解

    本文主要介紹C語(yǔ)言中的條件運(yùn)算符,并提供示例代碼以便大家學(xué)習(xí)參考,希望能幫助學(xué)習(xí) C語(yǔ)言的同學(xué)
    2016-07-07
  • C++智能指針模板應(yīng)用詳細(xì)介紹

    C++智能指針模板應(yīng)用詳細(xì)介紹

    從比較簡(jiǎn)單的層面來(lái)看,智能指針是RAII(Resource Acquisition Is Initialization,資源獲取即初始化)機(jī)制對(duì)普通指針進(jìn)行的一層封裝。這樣使得智能指針的行為動(dòng)作像一個(gè)指針,本質(zhì)上卻是一個(gè)對(duì)象,這樣可以方便管理一個(gè)對(duì)象的生命周期
    2022-08-08
  • C++11 并發(fā)指南之std::mutex詳解

    C++11 并發(fā)指南之std::mutex詳解

    這篇文章主要介紹了C++11 并發(fā)指南之std::mutex詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02

最新評(píng)論

沧源| 榕江县| 黑龙江省| 洪雅县| 营山县| 郎溪县| 武定县| 胶南市| 聂荣县| 平乐县| 教育| 舒城县| 图木舒克市| 鄄城县| 永登县| 五台县| 阳泉市| 灵丘县| 讷河市| 甘孜| 古交市| 昌平区| 凌源市| 承德县| 万全县| 资阳市| 安仁县| 平遥县| 吴堡县| 衡南县| 兰西县| 天镇县| 乃东县| 津南区| 石阡县| 宁海县| 丰城市| 霍州市| 十堰市| 敖汉旗| 涟源市|