C语言建立有向图的邻接表及其遍历操作
1
2 #include"stdio.h"
3 #include"stdlib.h"
4
5 typedef char elemtype;
6 #define maxsize 10
7 #define queuesize 100
8
9 typedef struct edgenode
10 {
11 int adjvex;
12 struct edgenode *next;
13 int weight;
14 }edgenode;
15
16 typedef struct vexnode
17 {
18 elemtype data;
19 edgenode *firstedge;
20 }vexnode;
21
22 typedef struct{
23 vexnode vexlist[maxsize];
24 int n,e;
25 }graph;
26
27 int locatevex(graph g,elemtype v)
28 {
29 int i;
30 for(i=0;i<g.n;i++)
31 if(g.vexlist[i].data==v)return i;
32 return -1;
33 }
34
35 void print(graph g)
36 {
37 int i;
38 edgenode *p;
39 printf("图的邻接表表示:");
40 for(i=0;i<g.n;i++){
41 printf("\n%4c",g.vexlist[i].data);
42 p=g.vexlist[i].firstedge;
43 while(p!=NULL){
44 printf("-->%d",p->adjvex);p=p->next;
45 }
46 }
47 printf("\n");
48 }
49 void creategraph(graph *g){
50 int i,j,k;
51 elemtype v1,v2;
52 edgenode *s;
53 printf("请输入图的顶点数及边数\n");
54 printf("顶点数 n=");scanf("%d",&g->n);
55 printf("边 数 e=");scanf("%d",&g->e);
56 printf("请输入图的顶点信息:\n");getchar();
57 for(i=0;i<=g->n;i++){
58 scanf("%c",&g->vexlist[i].data);
59 g->vexlist[i].firstedge=NULL;
60 }
61 printf("请输入图的边的信息:\n");
62 for(k=0;k<g->e;k++)
63 {
64 printf("请输入第%d条边的两个端点下标:",k+1);
65 scanf("%c%c",&v1,&v2);getchar();
66 i=locatevex(*g,v1);
67 j=locatevex(*g,v2);
68 if(i>=0&&j>=0){
69
70
79
80 s=(edgenode *)malloc(sizeof(edgenode));
81 s->adjvex=j;
82 s->next=g->vexlist[i].firstedge;
83 g->vexlist[i].firstedge=s;
84 }
85 }
86 }
87 int visited[maxsize];
88
89 void DFS(graph g,int i)
90 {
91 edgenode *p;
92 printf("%3c",g.vexlist[i].data);
93 visited[i]=1;
94 p=g.vexlist[i].firstedge;
95 while(p!=NULL) {
96 if(visited[p->adjvex]==0)
97 DFS(g,p->adjvex);
98 p=p->next;
99 }
100 }
101 void DFStraverse(graph g)
102 {
103 int v;
104 for (v=0; v<g.n;v++)visited[v]=0;
105 for (v=0; v<g.n;v++)
106
107 if (!visited[v])DFS(g,v);
108 }
109
110 typedef struct cirqueue
111 {
112 elemtype *data;
113 int front;
114 int rear;
115 }cirqueue;
116
117 int initqueue(cirqueue *q)
118 {
119 q->data=(elemtype *)malloc(queuesize*sizeof(cirqueue));
120 if (q->data==NULL)return 0;
121 q->front=q->rear=0;
122 return 1;
123 }
124
125 int enqueue (cirqueue *q,elemtype e)
126 {
127 if ((q->rear+1) % queuesize == q->front)
128 return 0;
129 q->data[q->rear] = e;
130 q->rear = (q->rear+1) % queuesize;
131 return 1;
132 }
133
134 int dequeue (cirqueue *q,int *e)
135 {
136 if (q->front == q->rear) return 0;
137 *e = q->data[q->front];
138 q->front = (q->front+1) %queuesize;
139 return 1;
140 }
141
142 int getfront(cirqueue q,elemtype *e)
143 {
144 if (q.front == q.rear) return 0;
145 *e=q.data[q.front];
146 return 1;
147 }
148
149 int queueempty(cirqueue q)
150 {
151 if(q.front == q.rear)return 1;
152 else return 0;
153 }
154
155 int queuelength(cirqueue q)
156 {
157 return (q.rear-q.front+queuesize) % queuesize;
158 }
159
160 int BFSvisited[maxsize];
161 cirqueue q;
162
163 void BFS(graph g,int k){
164 int i;
165 edgenode *p;
166 initqueue(&q);
167 printf("%3c",g.vexlist[k].data);
168 visited[k]=1;
169 enqueue(&q,k);
170 while(queueempty(q)==0) {
171 dequeue(&q,&i);
172 p=g.vexlist[i].firstedge;
173 while(p!=NULL){
174 if(visited[p->adjvex]==0){
175
176 printf("%3c",g.vexlist[p->adjvex].data);
177 visited[p->adjvex]=1;
178 enqueue(&q,p->adjvex);
179 }
180 p=p->next;
181 }
182 }
183 }
184 void BFStraverse(graph g)
185 {
186 int v;
187 for (v=0; v<g.n;v++)
188 visited[v]=0;
189 for (v=0; v<g.n;v++)
190 if (!visited[v])BFS(g,v);
191
192 }
193 int main()
194 {
195 graph g;
196 creategraph(&g);
197 print(g);
198 printf("\n");
199 printf("图的深度优先遍历序列:\n");
200 DFStraverse(g);
201 printf("\n");
202 printf("图的广度优先遍历序列:\n");
203 BFStraverse(g);
204 printf("\n");
205 return 0;
206 }
程序运行截图:
