日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图的存储--十字链表

發(fā)布時間:2023/12/10 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图的存储--十字链表 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

十字鏈表常用于存儲有向圖(網(wǎng)),在鄰接矩陣當中,想要的到一個頂點的入度和出度,需要遍歷整個鄰接矩陣;在鄰接表當中,得到一個頂點的出度很簡單,而想要的到一個頂點的入度,卻需要遍歷整個鄰接表。

十字鏈表由此而來,在鄰接表的基礎上,不但有該頂點為弧尾組成的鏈表,還多增加了一條以該頂點為弧頭組成的鏈表。這樣,便能輕松得到該頂點的入度和出度。

?同時用于連接鏈表的弧結(jié)構(gòu)也發(fā)生變化:

十字鏈表存儲有向圖:

/*十字鏈表主要存儲有向圖 */ #define MAX_VERTEX_NUM 10 // 最大頂點數(shù) typedef char verType; // 頂點類型 typedef string InfoType; // 弧上信息類型 // 弧 struct ArcNode {int tailIndex, headIndex; // 弧尾、弧頭的下標ArcNode *tlink, *hlink; // 指向下一個同一弧尾、弧頭的弧InfoType* info; // 弧上信息 }; // 頂點 struct verNode {verType vertex; // 數(shù)組域ArcNode *firstIn, *firstOut; // 分別指向第一個以該頂點為弧頭、弧尾的弧 }; // 十字鏈表 struct OLGraph {verNode Vertex[MAX_VERTEX_NUM]; // 頂點數(shù)組int ver_num, arc_num; // 頂點數(shù)、弧數(shù) }; // 獲取頂點在數(shù)組的位置 int LocateVextex(OLGraph& graph, verType ver) {for (int i = 0; i < graph.ver_num; ++i)if (graph.Vertex[i].vertex == ver) return i;return -1; } // 創(chuàng)建有向圖 void CreatDG(OLGraph &graph) {cout << "輸入頂點數(shù)、邊數(shù)>>" << endl;cin >> graph.ver_num >> graph.arc_num;cout << "輸入頂點信息>>" << endl;for (int i = 0; i < graph.ver_num; ++i) {cin >> graph.Vertex[i].vertex;graph.Vertex[i].firstIn = NULL;graph.Vertex[i].firstOut = NULL;}verType v1, v2;int index1, index2;for (int i = 0; i < graph.arc_num; ++i) {cout << "輸入鄰接的頂點>>" << endl;cin >> v1 >> v2;index1 = LocateVextex(graph, v1);index2 = LocateVextex(graph, v2);if (index1 < 0 || index2 < 0) {cout << "鄰接頂點有誤" << endl;--i;continue;}ArcNode* arc = new ArcNode(); // 弧尾arc->headIndex = index2;arc->tailIndex = index1;arc->hlink = graph.Vertex[index2].firstIn;arc->tlink = graph.Vertex[index1].firstOut;graph.Vertex[index1].firstOut = arc;graph.Vertex[index2].firstIn = arc;cout << "弧上是否存儲信息" << endl;cout << "1、是 0、否" << endl;int Is_info;cin >> Is_info;if (Is_info) {cout << "輸入弧上信息" << endl;InfoType INFO;cin >> INFO;arc->info = new InfoType(INFO);}} }

?遍歷輸出頂點關(guān)系:

// 遍歷 void Traverse(OLGraph &graph) {for (int i = 0; i < graph.ver_num; ++i) {ArcNode* T = graph.Vertex[i].firstOut;while (T) {cout << "頂點 " << graph.Vertex[i].vertex << " ----> "<< graph.Vertex[T->headIndex].vertex;if (T->info) {cout << " 弧上信息:" << *(T->info);}cout << endl;T = T->tlink;}} }

總結(jié)

以上是生活随笔為你收集整理的图的存储--十字链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。