-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram-3.19.c
More file actions
51 lines (43 loc) · 911 Bytes
/
program-3.19.c
File metadata and controls
51 lines (43 loc) · 911 Bytes
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
// Program 3.19
// Adjacency-lists graph representation
// ./a.out
// 0 6 0 1 0 2 0 5 4 7 0 7 2 7 1 7 3 4 3 5 4 5 4 6 $
#include <stdio.h>
#include <stdlib.h>
typedef struct node *link;
struct node {
int v;
link next;
};
link NEW(int v, link next) {
link x = malloc(sizeof *x);
x->v = v;
x->next = next;
return x;
}
#define V 10
void print_adj(link adj[]) {
for (int i = 0; i < V; i++) {
link x = adj[i];
if (!x)
continue;
printf("%d: ", i);
while (x) {
printf("%d ", x->v);
x = x->next;
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int i, j;
link adj[V];
for (i = 0; i < V; i++)
adj[i] = NULL;
while (scanf("%d %d", &i, &j) == 2) {
adj[j] = NEW(i, adj[j]);
adj[i] = NEW(j, adj[i]);
}
print_adj(adj);
return 0;
}