数据结构中的十字链表(2)
2008-04-12 14:24
120 查看
有向图也可以用十字链表表示:应为有向图由于不对称性,导致其矩阵表示里面会出现很多0,例如下面的有向图的矩阵表示:
矩阵raph
由于对于有向图的矩阵肯定是方阵,所以可以将行列链表的头节点合并;具体数据结构代码如下:
const int MAX_VERTEX_NUM=20;
typedef struct ArcBox
...{
int tailvex, headvex;
ArcBox *hlink, *tlink;
int info; //弧的相关信息,权值.
}ArcBox;
typedef struct VexNode
...{
int data;
ArcBox *firstIn, *firstOut; //指向该顶点的第一个入弧,出弧
}VexNode;
typedef struct
...{
VexNode xList[MAX_VERTEX_NUM];
int vexNum, arcNum;
}OLGraph; //十字链表:Orthogonal List;
int LocateVex(OLGraph& G, int& e)
...{
for (int i=0; i<G.vexNum; i++)
if(G.xList[i].data==e)
return i;
}
bool CreatDG(OLGraph& G)
...{
//采用十字链表存储表示,构造有向图.
cin>>G.vexNum>>G.arcNum; //顶点数,弧数;
for(int i=0; i<G.vexNum; i++)
...{
cin>>G.xList[i].data;
G.xList[i].firstIn=G.xList[i].firstOut=NULL;
}
for (int k=0; k<G.arcNum; k++)
...{
int v1, v2, value; //弧的起点,终点, 权值
cin>>v1>>v2>>value;
int i=LocateVex(G, v1); int j=LocateVex(G, v2);
ArcBox* ptem=new ArcBox;
ptem->tailvex=i; ptem->headvex=j; ptem->info=value;
ptem->hlink=G.xList[j].firstIn; ptem->tlink=G.xList[i].firstOut;
G.xList[j].firstIn=G.xList[i].firstOut=ptem;
}
return true;
}
//输出各行元素
void DisplayRow(OLGraph& dest)
...{
for (int i=0; i<dest.vexNum; i++)
...{
ArcBox *tem;
tem=dest.xList[i].firstIn;
while (tem)
...{
cout<<"( "<<tem->tailvex<<", ";
cout<<tem->headvex<<", ";
cout<<tem->info<<" )";
tem=tem->hlink;
}
cout<<endl;
}
}
/*测试输入:
4 7 //顶点数,行数
1 2 3 4 //顶点数据
1 2 10 //弧的信息:尾,头,权值
1 3 11
3 1 12
3 4 13
4 1 14
4 2 15
4 3 16*/
/*测试输出:
( 3, 0, 14 )( 2, 0, 12 )
( 3, 1, 15 )( 0, 1, 10 )
( 3, 2, 16 )( 0, 2, 11 )
( 2, 3, 13 )
*/
V1 | V2 | V3 | V4 | |
V1 | 0 | A | B | 0 |
V2 | 0 | 0 | 0 | 0 |
V3 | C | 0 | 0 | D |
V4 | E | F | G | 0 |
const int MAX_VERTEX_NUM=20;
typedef struct ArcBox
...{
int tailvex, headvex;
ArcBox *hlink, *tlink;
int info; //弧的相关信息,权值.
}ArcBox;
typedef struct VexNode
...{
int data;
ArcBox *firstIn, *firstOut; //指向该顶点的第一个入弧,出弧
}VexNode;
typedef struct
...{
VexNode xList[MAX_VERTEX_NUM];
int vexNum, arcNum;
}OLGraph; //十字链表:Orthogonal List;
int LocateVex(OLGraph& G, int& e)
...{
for (int i=0; i<G.vexNum; i++)
if(G.xList[i].data==e)
return i;
}
bool CreatDG(OLGraph& G)
...{
//采用十字链表存储表示,构造有向图.
cin>>G.vexNum>>G.arcNum; //顶点数,弧数;
for(int i=0; i<G.vexNum; i++)
...{
cin>>G.xList[i].data;
G.xList[i].firstIn=G.xList[i].firstOut=NULL;
}
for (int k=0; k<G.arcNum; k++)
...{
int v1, v2, value; //弧的起点,终点, 权值
cin>>v1>>v2>>value;
int i=LocateVex(G, v1); int j=LocateVex(G, v2);
ArcBox* ptem=new ArcBox;
ptem->tailvex=i; ptem->headvex=j; ptem->info=value;
ptem->hlink=G.xList[j].firstIn; ptem->tlink=G.xList[i].firstOut;
G.xList[j].firstIn=G.xList[i].firstOut=ptem;
}
return true;
}
//输出各行元素
void DisplayRow(OLGraph& dest)
...{
for (int i=0; i<dest.vexNum; i++)
...{
ArcBox *tem;
tem=dest.xList[i].firstIn;
while (tem)
...{
cout<<"( "<<tem->tailvex<<", ";
cout<<tem->headvex<<", ";
cout<<tem->info<<" )";
tem=tem->hlink;
}
cout<<endl;
}
}
/*测试输入:
4 7 //顶点数,行数
1 2 3 4 //顶点数据
1 2 10 //弧的信息:尾,头,权值
1 3 11
3 1 12
3 4 13
4 1 14
4 2 15
4 3 16*/
/*测试输出:
( 3, 0, 14 )( 2, 0, 12 )
( 3, 1, 15 )( 0, 1, 10 )
( 3, 2, 16 )( 0, 2, 11 )
( 2, 3, 13 )
*/
相关文章推荐
- 数据结构中的十字链表(1)
- 数据结构-图-十字链表的构造(1)
- 数据结构--十字链表(相加,创建)
- 数据结构--数组和广义表--创建以十字链表为存储结构的矩阵
- 数据结构:有向图的十字链表存储表示(c实现)
- 【算法与数据结构】图 -- 十字链表
- 数据结构学习笔记 --- 图(十字链表)
- 十字链表 稀疏矩阵的数据结构
- 5-4-十字链表(稀疏矩阵)-数组和广义表-第5章-《数据结构》课本源码-严蔚敏吴伟民版
- 数据结构例程——稀疏矩阵的十字链表表示
- 动手实现 数据结构 之 “十字链表”
- c语言版数据结构(奇迹冬瓜)-数组和广义表(十字链表存贮稀疏矩阵的行列值)
- 数据结构学习笔记 --- 图(十字链表)
- 【学习笔记----数据结构17-图的十字链表】
- 【数据结构】算法5.4 十字链表-创建
- c语言实现十字链表储存有向图(《数据结构》算法7.3)
- 数据结构之---C/C++实现稀疏矩阵的十字链表
- 数据结构9————马鞍点问题&三元组稀疏矩阵&十字链表稀疏链表的加减法
- 数据结构27:矩阵加法(基于十字链表)
- 【数据结构】算法7.3 图的存储结构-十字链表