-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjLinkedList.c
More file actions
67 lines (61 loc) · 1.52 KB
/
AdjLinkedList.c
File metadata and controls
67 lines (61 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Created by ys on 2019/4/21.
//
#include "AdjLinkedList.h"
#include "../tools.h"
#define DATASTRUCT_ADJLINKEDLIST_C
#ifndef DATASTRUCT_ADJLINKEDLIST_C
#define DATASTRUCT_ADJLINKEDLIST_C
void CreateALGraph(ALGraph *G)
{
FILE *fp;
char *fname = "../data/graph.txt";
fp = fopen(fname,"r");
char ch;
int n, e;
fscanf(fp,"%d %d\n", &n, &e);
G->n = n;
G->e = e;
char vexdata = -1;
for (int i = 0; i < G->n; ++i) {
fscanf(fp,"%c\n", &vexdata);
G->adjlist[i].vexdata = vexdata;
G->adjlist[i].firstarc = NULL;
printf("%c\n",G->adjlist[i].vexdata);
}
EdgeNode *E;
char head, end;
int weight;
for (int j = 0; j < G->e; ++j) {
fscanf(fp,"(%c, %c, %d)\n",&head, &end, &weight);
E = (EdgeNode *)malloc(sizeof(EdgeNode));
E->adjvex = end - 'a';
E->nextarc = G->adjlist[head-'a'].firstarc;
E->info = weight;
G->adjlist[head - 'a'].firstarc = E;
printf("end %d\n", E->adjvex);
}
EdgeNode *p;
for (int i = 0; i < G->n; ++i) {
p=G->adjlist[i].firstarc;
while (p!=NULL)
{
printf("%c weitght:%d-> %c",G->adjlist[i].vexdata, p->info, G->adjlist[p->adjvex].vexdata);
p=p->nextarc;
printf("\n");
}
printf("\n");
}
}
void test()
{
ALGraph *G = (ALGraph *)malloc(sizeof(ALGraph));
if(G == NULL)
{
printf("创建图的时候,申请空间失败\n");
exit(-1);
}
CreateALGraph(G);
free(G);
}
#endif