有向图十字链表创建与输出(一)

之前只有十字链表的创建,这篇包含创建和输出两个函数,用c++模板类实现。

  1 #include <iostream>
2
3 using namespace std;
4
5 template <class TElemType>
6 class Graph
7 {
8 public:
9 void CreateOlgraph();
10 void PrintOlgraph();
11 private:
12 struct ArcBox
13 {
14 int headvex,tailvex;
15 ArcBox *hlink,*tlink;
16 float weight;
17 };
18
19 template <class TElemType>
20 struct Vertex
21 {
22 TElemType data;
23 ArcBox *firstin,*firstout;
24 };
25
26 struct Olgraph
27 {
28 int vexnum,arcnum;
29 Vertex<TElemType> *vex;
30 };
31 Olgraph olgraph; //有向图的十字链表存储结构
32
33
34 };
35
36 template <class TElemType>
37 void Graph<TElemType>::CreateOlgraph() // 创建十字链表
38 {
39 int i,j,m,n;
40 float w;
41 TElemType v1,v2;
42 ArcBox *p;
43 cout << "输入有向图的顶点数和边数:" << endl;
44 cin >> olgraph.vexnum >> olgraph.arcnum;
45 olgraph.vex = (Vertex<TElemType> *)malloc(olgraph.vexnum * sizeof(Vertex<TElemType>));
46 cout << "输入顶点的信息:" << endl;
47 for(i = 0;i < olgraph.vexnum;i++)
48 {
49 cin >> olgraph.vex[i].data;
50 olgraph.vex[i].firstin = olgraph.vex[i].firstout = NULL;
51 }
52
53 cout << "输入各边的弧尾与弧头结点及有向边的权值:" << endl;
54
55 for(i = 0;i < olgraph.arcnum;i++)
56 {
57 cin >> v1 >> v2 >> w;
58 for(j = 0;j < olgraph.vexnum;j++)
59 {
60 if(v1 == olgraph.vex[j].data) m = j;
61 if(v2 == olgraph.vex[j].data) n = j;
62 }
63 p = (ArcBox *)malloc(sizeof(ArcBox));
64 p->headvex = n;p->tailvex = m;
65 p->weight = w;
66 p->hlink = olgraph.vex[n].firstin;olgraph.vex[n].firstin = p;
67 p->tlink = olgraph.vex[m].firstout;olgraph.vex[m].firstout = p;
68 }
69 } //end CreateOlgraph
70
71
72 template <class TElemType>
73 void Graph<TElemType>::PrintOlgraph() // 输出十字链表
74 {
75 int k;
76 ArcBox *p;
77 cout << " The Vertex Outdegree = > " << endl;
78 for(k = 0; k <olgraph.vexnum; k ++)
79 {
80 p = olgraph.vex[k].firstout;
81 cout << " Vertex:" << k+1 << "";
82 while(p != NULL)
83 {
84
85 cout << "<"<<(p->tailvex)+1 << "," << (p->headvex)+1<< ">" << " ";
86 p = p-> tlink;
87
88 }
89 cout << endl;
90
91 }
92
93
94
95 cout << " The Vertex Indegree = > " << endl;
96 for(k = 0; k <olgraph.vexnum; k ++)
97 {
98 p = olgraph.vex[k].firstin;
99 cout << " Vertex:" << k+1 << "";
100 while(p != NULL)
101 {
102
103 cout << "<"<<(p->tailvex)+1 << "," << (p->headvex)+1<< ">" << " ";
104 p = p-> hlink;
105
106 }
107 cout << endl;
108
109 }
110
111 }
112
113 int main()
114 {
115 /*Graph<int> gph;
116 gph.CreateDN();
117 int a,b;
118 cout << "输入终点和起点:";
119 cin >> a >> b;
120 gph.Shortestpath_DIJ(a,b);
121 gph.DestroyDN();
122 return 0;*/
123 Graph<int> gph;
124 gph.CreateOlgraph();
125 gph.PrintOlgraph();
126
127 return 0;
128 }



转载于:https://www.cnblogs.com/uniquews/archive/2012/02/23/2364627.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值