-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.c
59 lines (53 loc) · 966 Bytes
/
graph.c
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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
struct newtype{
int ver;
int w;
struct newtype *next;
};
typedef struct newtype node;
node *graph[100005];
node* insertEdge(node *head, int ver, int w){
node *temp = head;
node *temp1 = (node *)malloc(sizeof(node));
temp1 -> ver = ver;
temp1 -> w = w;
temp1 -> next = NULL;
if(temp == NULL){
head = temp1;
}
else{
temp1 -> next = temp;
head = temp1;
}
return head;
}
void initGraph(int size){
int i;
for(i = 0; i < size; i++){
graph[i] = NULL;
}
}
void printVertex(int ver){
node *temp = graph[ver];
while(temp != NULL){
printf("%d %d\n", temp -> ver, temp -> w);
temp = temp -> next;
}
}
int main(){
initGraph(11);
int i;
for(i = 1; i < 5; i++){
graph[i] = insertEdge(graph[i], i + 1, 3);
}
for(i = 1; i < 10; i++){
graph[i] = insertEdge(graph[i], i + 2, 5);
}
for(i = 0; i <= 10; i++){
printf("\nVERTEX --> %d\n", i);
printVertex(i);
}
}