有向图邻接表存储于输出 [Jayme]

  1 #include <iostream>
2 using namespace std;
3
4
5
6
7 template <class TElemType>
8 class Graph
9 {
10 public:
11 void CreateAlgraph();
12 void PrintAlgraph();
13
14 private:
15
16 struct Arcnode
17 {
18 int adjvex;
19 Arcnode *nextarc;
20 float weight;
21 };
22
23 template <class TElemType>
24 struct Vexnode
25 {
26 TElemType data;
27 Arcnode *firarc;
28 };
29 struct ALgraph
30 {
31 int vexnum;
32 int arcnum;
33
34 Vexnode<TElemType> *vex;
35 };
36 ALgraph algraph; //邻接表存储
37
38 };
39
40
41 template <class TElemType>
42 void Graph<TElemType>::CreateAlgraph()
43 {
44 Arcnode *p,*q,*l,*s,*w,*r,*g,*f,*t;
45 int a,b,k,i;
46 cout << "请输入k的值" << endl;
47 cin >> k;
48 a = 3*k + 3;//顶点数
49 b = 6*k + 2;//边数
50 algraph.vexnum = a;//将输入的顶点数赋给图的顶点数
51 algraph.arcnum = b;//将输入的边数赋给图的边数
52 algraph.vex = (Vexnode<TElemType> *)malloc(algraph.vexnum * sizeof(Vexnode<TElemType>));
53
54
55
56
57
58 for(i = 0;i < algraph.vexnum;i++)
59 {
60 algraph.vex[i].data = i+1;//给每个顶点赋值
61 algraph.vex[i].firarc = NULL;
62
63 }
64
65 for(i =1;i <= k; i++)
66 {
67 p = (Arcnode *)malloc(sizeof(Arcnode));
68 p->adjvex = i;
69 p->nextarc = algraph.vex[0].firarc;
70 algraph.vex[0].firarc = p;//链表是从表头插入
71
72 }
73
74
75 for(i =1;i <= k; i++)
76 {
77 w = (Arcnode *)malloc(sizeof(Arcnode));
78 w->adjvex = k+1;
79 w->nextarc = algraph.vex[i].firarc;
80 algraph.vex[i].firarc = w;//链表是从表头插入
81
82 }
83
84 for(i =k +1;i <= 2*k; i++)
85 {
86 r = (Arcnode *)malloc(sizeof(Arcnode));
87 r->adjvex = 2*k+1;
88 r->nextarc = algraph.vex[i].firarc;
89 algraph.vex[i].firarc = r;//链表是从表头插入
90
91 }
92
93
94 for(i =2*k+2;i <= 3*k+1; i++)
95 {
96 g = (Arcnode *)malloc(sizeof(Arcnode));
97 g->adjvex = i;
98 g->nextarc = algraph.vex[2*k+1].firarc;
99 algraph.vex[2*k+1].firarc = g;//链表是从表头插入
100
101 }
102
103 for(i =2*k+2;i <= 3*k+1; i++)
104 {
105 f = (Arcnode *)malloc(sizeof(Arcnode));
106 f->adjvex = 3*k+2;
107 f->nextarc = algraph.vex[i].firarc;
108 algraph.vex[i].firarc = f;//链表是从表头插入
109
110 }
111
112 q = (Arcnode *)malloc(sizeof(Arcnode));
113 q->adjvex = 0;
114 q->nextarc = algraph.vex[2*k].firarc;
115 algraph.vex[2*k].firarc = q;
116
117 l = (Arcnode *)malloc(sizeof(Arcnode));
118 l->adjvex = k+1;
119 l->nextarc = algraph.vex[2*k+2].firarc;
120 algraph.vex[2*k+2].firarc = l;
121
122 s = (Arcnode *)malloc(sizeof(Arcnode));
123 s->adjvex = 2*k+1;
124 s->nextarc = algraph.vex[3*k+2].firarc;
125 algraph.vex[3*k+2].firarc = s;
126
127 for(i = k + 1;i <= 2*k-1; i++)
128 {
129 t = (Arcnode *)malloc(sizeof(Arcnode));
130 t->adjvex =i + 1;
131 t->nextarc = algraph.vex[i].firarc;
132 algraph.vex[i].firarc = t;
133
134 }
135
136 }
137
138 template <class TElemType>
139 void Graph<TElemType>::PrintAlgraph() // 输出链表
140 {
141 int k;
142 Arcnode *p;
143 cout << " The Vertex Outdegree = > " << endl;
144 for(k = 0; k <algraph.vexnum; k ++)
145 {
146 p = algraph.vex[k].firarc;
147 cout << " Vertex" << k+1 << ": ";
148 while(p != NULL)
149 {
150
151 cout << "<"<<k+1 << "," << (p->adjvex)+1<< ">" << " ";
152 p = p-> nextarc;
153
154 }
155 cout << endl;
156
157 }
158
159 }
160
161 int main()
162
163 {
164 Graph<int> gph;
165 gph.CreateAlgraph();
166 gph.PrintAlgraph();
167
168 return 0;
169 }
posted @ 2012-02-28 00:50  uniquews  阅读(699)  评论(0编辑  收藏  举报