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;