i007.cc

i007.cc

优先队列-降维打击

【数据结构】十字链表

原文地址

 

上篇文章讲解了图的邻接表的链式存储方式,这篇文章讲解图的十字链存储方式。

基本概念

十字链表是为了便于求得图中顶点的度(出度和入度)而提出来的。它是综合邻接表和逆邻接表形式的一种链式存储结构。

在十字链表存储结构中,有向图中的顶点的结构如下所示:

其中data表示顶点的具体数据信息,而firstIn则表示指向以该顶点为弧头的第一个弧节点。而firstOut则表示指向以该顶点为弧尾的第一个弧节点。为了表示有向图中所有的顶点,采用一个顶点数组存储每一个结点,如下图所示。

另外,在十字链表存储结构中,有向图中的每一条弧都有一个弧结点与之对应,具体的弧结点结构如下所示:

 

其中的tailVex表示该弧的弧尾顶点在顶点数组xList中的位置,headVex表示该弧的弧头顶点在顶点数组中的位置。hLink则表示指向弧头相同的下一条弧,tLink则表示指向弧尾相同的下一条弧。

从十字链表的数据结构来看,每一个顶点对应两个链表:以该顶点为弧尾的弧结点所组成的链表以及以该顶点为弧头的弧结点所组成的链表。

如下图所示的一个有向图:

其对应的顶点以及弧结点如下所示。拿结点A说明,该结点对应两个链表(绿色和黄色标记的)。绿色链表表示以结点A为弧头的弧组成的链表。黄色链表表示以结点A为弧尾的弧组成的链表。

 

基本操作

 

1、创建图的十字链表

  1. Status createDG(OLGraph &G, int vexNum, int arcNum){
  2. G.vexNum = vexNum;
  3. G.arcNum = arcNum;
  4. for(int i = 0; i<G.vexNum; i++){
  5. cin>>G.xList[i].data;
  6. G.xList[i].firstIn = NULL;
  7. G.xList[i].firstOut = NULL;
  8. }//初始化
  9. for(int i = 0; i<G.arcNum; i++){
  10. vexNode node1, node2;
  11. cout<<“please input two nodes of “<<i+1<<“-th arc”<<endl;
  12. cin>>node1.data>>node2.data;
  13. insertArc(G, node1, node2);
  14. }
  15. return 1;
  16. }

测试结果:

如下图所示首先输入四个顶点:A、B、C、D,然后输入7条弧。

既可以构建如下的有向图。

 

2、按照十字链表形式打印图

  1. Status printDG(OLGraph &G){
  2. for(int i = 0; i<G.vexNum; i++){
  3. arcBox *ptail = G.xList[i].firstOut;
  4. arcBox *phead = G.xList[i].firstIn;
  5. //打印以结点i为弧尾的链
  6. cout<<“以结点”<<i<<“为弧尾的链 “<<G.xList[i].data;
  7. while(ptail){
  8. cout<<“–>”<<“|”<<ptail->tailVex<<“|”<<ptail->headVex;
  9. ptail = ptail-> tLink;
  10. }
  11. cout<<“–>NULL”<<endl;
  12. //打印以结点i为弧头的链
  13. cout<<“以结点”<<i<<“为弧头的链 “<<G.xList[i].data;
  14. while(phead){
  15. cout<<“–>”<<“|”<<phead->tailVex<<“|”<<phead->headVex;
  16. phead = phead->hLink;
  17. }
  18. cout<<“–>NULL”<<endl;
  19. }
  20. return 1;
  21. }

测试结果:

每一个顶点对应两个链表(以该结点为弧头的链表和以该结点位弧尾的链表),分别将其打印出来。

3、向图中插入一个结点

  1. Status insertNode(OLGraph &G, vexNode node){
  2. G.xList[G.vexNum].data = node.data;
  3. G.xList[G.vexNum].firstIn = NULL;
  4. G.xList[G.vexNum].firstOut = NULL;
  5. G.vexNum = G.vexNum + 1;
  6. return 1;
  7. }

测试结果:

向原来的有向图插入一个新的结点E。新插入的节点没有与之前图中的顶点建立弧,因此其打印结果如下所示。

此时有向图变成了:

4、向图中结点之间插入一条弧

 

  1. void insertArcAction(OLGraph &G, int index1, int index2){
  2. arcBox* pArc = new arcBox[1];
  3. pArc->tailVex = index1;
  4. pArc->headVex = index2;
  5. pArc->info = NULL;
  6. arcBox *ptail = G.xList[index1].firstOut;
  7. arcBox *phead = G.xList[index2].firstIn;
  8. if(!ptail){pArc->tLink = NULL;}
  9. else{pArc->tLink = ptail;}
  10. if(!phead){pArc->hLink = NULL;}
  11. else{pArc->hLink = phead;}
  12. G.xList[index1].firstOut = pArc;//链头部插入弧结点
  13. G.xList[index2].firstIn = pArc;
  14. }
  15. Status insertArc(OLGraph &G, vexNode node1, vexNode node2){
  16. int index1 = locateVertex(G, node1);
  17. int index2 = locateVertex(G, node2);
  18. insertArcAction(G, index1, index2);
  19. return 1;
  20. }

 

测试结果:

插入一条结点B到结点C的弧。再次打印该有向图时,结果如下所示。

此时有向图变成了:

 

5、删除一条弧

  1. void deleteOutArcAction(OLGraph &G, int index1, int index2){
  2. arcBox *cur = G.xList[index1].firstOut;
  3. arcBox *pre = cur;
  4. int count = 0;
  5. if(!cur)
  6. return;
  7. else{
  8. while(cur){
  9. count++;
  10. if(cur->headVex == index2)
  11. break;
  12. pre = cur;
  13. cur = cur->tLink;
  14. }
  15. }
  16. if(!cur)
  17. return;//该结点没有对应的弧
  18. else if(count <=1)
  19. G.xList[index1].firstOut = pre->tLink;//删除第一个弧结点
  20. else
  21. pre->tLink = cur->tLink;//删除非第一个弧结点
  22. }
  23. void deleteInArcAction(OLGraph &G, int index1, int index2){
  24. arcBox *cur = G.xList[index2].firstIn;
  25. arcBox *pre = cur;
  26. int count = 0;
  27. if(!cur)
  28. return;
  29. else{
  30. while(cur){
  31. count++;
  32. if(cur->tailVex == index1)
  33. break;
  34. pre = cur;
  35. cur = cur->hLink;
  36. }
  37. }
  38. if(!cur)
  39. return;//该结点没有对应的弧
  40. else if(count <=1)
  41. G.xList[index2].firstIn = pre->hLink;
  42. else
  43. pre->hLink = cur->hLink;
  44. }
  45. Status deleteArc(OLGraph &G, vexNode node1, vexNode node2){
  46. //删除从结点1到结点2的弧(有方向)
  47. int index1 = locateVertex(G, node1);
  48. int index2 = locateVertex(G, node2);
  49. deleteOutArcAction(G, index1, index2);//删除两条链表里面的值
  50. deleteInArcAction(G, index1, index2);
  51. return 1;
  52. }

测试结果:

删除从结点A到结点B的弧。重新打印该有向图,结果如下所示:

此时有向图变成了:

 

6、删除一个顶点

  1. Status deleteNode(OLGraph &G, vexNode node){
  2. //删除结点后,该xList顶点数组中该结点后面的结点不前移,而只是将该被删除的结点的data设置成为一个较大的值
  3. int index = locateVertex(G, node);
  4. for(int i = 0; i<G.vexNum; i++){
  5. if(i == index)
  6. continue;
  7. else{
  8. deleteArc(G, G.xList[index], G.xList[i]);//删除以该结点为弧尾的弧
  9. deleteArc(G, G.xList[i], G.xList[index]);//删除以该结点为弧头的弧
  10. }
  11. }
  12. G.xList[index].data = ‘0’;//置’0’表示该结点被删除
  13. G.xList[index].firstIn = NULL;
  14. G.xList[index].firstOut = NULL;
  15. return 1;
  16. }

测试结果:

删除结点D,重新打印该有向图,其结果如下所示(只剩下3条弧,4个有效结点,结点D被删除后,该结点的内容变从‘D’变为‘0’):

测试有向图变成了:

7、全部代码

  1. // 十字链表.cpp : Defines the entry point for the console application.
  2. //
  3. #include “stdafx.h”
  4. #include <iostream>
  5. using namespace std;
  6. #define MAX_VERTEX_NUM 20
  7. typedef int Status;
  8. typedef int infoType;
  9. typedef char vertexType;
  10. typedef struct arcBox{
  11. int tailVex, headVex;
  12. struct arcBox *hLink, *tLink;
  13. infoType *info;
  14. }arcBox;//弧结点
  15. typedef struct vexNode{
  16. vertexType data;
  17. arcBox *firstIn, *firstOut;
  18. }vexNode;//顶点节点
  19. typedef struct{
  20. vexNode xList[MAX_VERTEX_NUM];
  21. int vexNum, arcNum;
  22. }OLGraph;//十字链
  23. int locateVertex(OLGraph &G, vexNode node){
  24. int index = -1;
  25. for(int i = 0; i<G.vexNum; i++){
  26. if(node.data == G.xList[i].data){
  27. index = i;
  28. break;
  29. }
  30. }
  31. return index;
  32. }
  33. void insertArcAction(OLGraph &G, int index1, int index2);
  34. Status insertArc(OLGraph &G, vexNode node1, vexNode node2);
  35. Status createDG(OLGraph &G, int vexNum, int arcNum){
  36. G.vexNum = vexNum;
  37. G.arcNum = arcNum;
  38. for(int i = 0; i<G.vexNum; i++){
  39. cin>>G.xList[i].data;
  40. G.xList[i].firstIn = NULL;
  41. G.xList[i].firstOut = NULL;
  42. }//初始化
  43. for(int i = 0; i<G.arcNum; i++){
  44. vexNode node1, node2;
  45. cout<<“please input two nodes of “<<i+1<<“-th arc”<<endl;
  46. cin>>node1.data>>node2.data;
  47. insertArc(G, node1, node2);
  48. }
  49. return 1;
  50. }
  51. Status printDG(OLGraph &G){
  52. for(int i = 0; i<G.vexNum; i++){
  53. arcBox *ptail = G.xList[i].firstOut;
  54. arcBox *phead = G.xList[i].firstIn;
  55. //打印以结点i为弧尾的链
  56. cout<<“以结点”<<i<<“为弧尾的链 “<<G.xList[i].data;
  57. while(ptail){
  58. cout<<“–>”<<“|”<<ptail->tailVex<<“|”<<ptail->headVex;
  59. ptail = ptail-> tLink;
  60. }
  61. cout<<“–>NULL”<<endl;
  62. //打印以结点i为弧头的链
  63. cout<<“以结点”<<i<<“为弧头的链 “<<G.xList[i].data;
  64. while(phead){
  65. cout<<“–>”<<“|”<<phead->tailVex<<“|”<<phead->headVex;
  66. phead = phead->hLink;
  67. }
  68. cout<<“–>NULL”<<endl;
  69. }
  70. return 1;
  71. }
  72. void insertArcAction(OLGraph &G, int index1, int index2){
  73. arcBox* pArc = new arcBox[1];
  74. pArc->tailVex = index1;
  75. pArc->headVex = index2;
  76. pArc->info = NULL;
  77. arcBox *ptail = G.xList[index1].firstOut;
  78. arcBox *phead = G.xList[index2].firstIn;
  79. if(!ptail){pArc->tLink = NULL;}
  80. else{pArc->tLink = ptail;}
  81. if(!phead){pArc->hLink = NULL;}
  82. else{pArc->hLink = phead;}
  83. G.xList[index1].firstOut = pArc;//链头部插入弧结点
  84. G.xList[index2].firstIn = pArc;
  85. }
  86. Status insertArc(OLGraph &G, vexNode node1, vexNode node2){
  87. int index1 = locateVertex(G, node1);
  88. int index2 = locateVertex(G, node2);
  89. insertArcAction(G, index1, index2);
  90. return 1;
  91. }
  92. Status insertNode(OLGraph &G, vexNode node){
  93. G.xList[G.vexNum].data = node.data;
  94. G.xList[G.vexNum].firstIn = NULL;
  95. G.xList[G.vexNum].firstOut = NULL;
  96. G.vexNum = G.vexNum + 1;
  97. return 1;
  98. }
  99. Status deleteArc(OLGraph &G, vexNode node1, vexNode node2);
  100. Status deleteNode(OLGraph &G, vexNode node){
  101. //删除结点后,该xList顶点数组中该结点后面的结点不前移,而只是将该被删除的结点的data设置成为一个较大的值
  102. int index = locateVertex(G, node);
  103. for(int i = 0; i<G.vexNum; i++){
  104. if(i == index)
  105. continue;
  106. else{
  107. deleteArc(G, G.xList[index], G.xList[i]);//删除以该结点为弧尾的弧
  108. deleteArc(G, G.xList[i], G.xList[index]);//删除以该结点为弧头的弧
  109. }
  110. }
  111. G.xList[index].data = ‘0’;//置’0’表示该结点被删除
  112. G.xList[index].firstIn = NULL;
  113. G.xList[index].firstOut = NULL;
  114. return 1;
  115. }
  116. void deleteOutArcAction(OLGraph &G, int index1, int index2){
  117. arcBox *cur = G.xList[index1].firstOut;
  118. arcBox *pre = cur;
  119. int count = 0;
  120. if(!cur)
  121. return;
  122. else{
  123. while(cur){
  124. count++;
  125. if(cur->headVex == index2)
  126. break;
  127. pre = cur;
  128. cur = cur->tLink;
  129. }
  130. }
  131. if(!cur)
  132. return;//该结点没有对应的弧
  133. else if(count <=1)
  134. G.xList[index1].firstOut = pre->tLink;//删除第一个弧结点
  135. else
  136. pre->tLink = cur->tLink;//删除非第一个弧结点
  137. }
  138. void deleteInArcAction(OLGraph &G, int index1, int index2){
  139. arcBox *cur = G.xList[index2].firstIn;
  140. arcBox *pre = cur;
  141. int count = 0;
  142. if(!cur)
  143. return;
  144. else{
  145. while(cur){
  146. count++;
  147. if(cur->tailVex == index1)
  148. break;
  149. pre = cur;
  150. cur = cur->hLink;
  151. }
  152. }
  153. if(!cur)
  154. return;//该结点没有对应的弧
  155. else if(count <=1)
  156. G.xList[index2].firstIn = pre->hLink;
  157. else
  158. pre->hLink = cur->hLink;
  159. }
  160. Status deleteArc(OLGraph &G, vexNode node1, vexNode node2){
  161. //删除从结点1到结点2的弧(有方向)
  162. int index1 = locateVertex(G, node1);
  163. int index2 = locateVertex(G, node2);
  164. deleteOutArcAction(G, index1, index2);//删除两条链表里面的值
  165. deleteInArcAction(G, index1, index2);
  166. return 1;
  167. }
  168. int _tmain(int argc, _TCHAR* argv[])
  169. {
  170. int vexNum = 4;
  171. int arcNum = 7;
  172. OLGraph G;
  173. cout<<“Try to create a Orthogonal List of a graph…”<<endl;
  174. createDG(G, vexNum, arcNum);
  175. cout<<“Try to print the Orthogonal List of the very graph…”<<endl;
  176. printDG(G);
  177. cout<<“Try to insert a node into the graph…”<<endl;
  178. vexNode node;
  179. cout<<” please input the data of the node to insert:”<<endl;
  180. cin>>node.data;
  181. insertNode(G, node);
  182. printDG(G);
  183. cout<<“Try to insert a arc into the graph…”<<endl;
  184. vexNode node1, node2;
  185. cout<<” please choose two node:”<<endl;
  186. cin>>node1.data>>node2.data;
  187. insertArc(G, node1, node2);
  188. printDG(G);
  189. cout<<“Try to delete the arc between two nodes…”<<endl;
  190. vexNode node3, node4;
  191. cout<<” please choose a arc with specifing two nodes:”<<endl;
  192. cin>>node3.data>>node4.data;
  193. deleteArc(G, node3, node4);
  194. printDG(G);
  195. cout<<“Try to delete a node of the graph…”<<endl;
  196. vexNode node5;
  197. cout<<” please choose a node:”<<endl;
  198. cin>>node5.data;
  199. deleteNode(G, node5);
  200. printDG(G);
  201. system(“pause”);
  202. return 0;
  203. }

发表回复