-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge.c
102 lines (81 loc) · 1.47 KB
/
edge.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* EDGE class - written by John C. Lusth
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "edge.h"
#define DEBUG 0
#if DEBUG
#define dprintf printf
#else
#define dprintf(...)
#endif
struct edge
{
int v1;
int v2;
int weight;
};
/*** constructors/desctructors ***/
EDGE
*newEDGE(int v1,int v2,int w)
{
EDGE *e = malloc(sizeof(EDGE));
e->v1 = v1;
e->v2 = v2;
e->weight = w;
return e;
}
void
freeEDGE(void *e)
{
free((EDGE *) e);
}
/*** accessors *******************/
int getEDGEv1(EDGE *e) { return e->v1; }
int getEDGEv2(EDGE *e) { return e->v2; }
int getEDGEweight(EDGE *e) { return e->weight; }
/*** mutators ********************/
int
setEDGEv1(EDGE *e,int v)
{
int temp = e->v1;
e->v1 = v;
return temp;
}
int
setEDGEv2(EDGE *e,int v)
{
int temp = e->v2;
e->v2 = v;
return temp;
}
int
setEDGEweight(EDGE *e,int w)
{
int temp = e->weight;
e->weight = w;
return temp;
}
/*** informational ***************/
int
compareEDGE(void *a,void *b)
{
EDGE *e = a;
EDGE *f = b;
dprintf("comparing edge (%d,%d,%d) with (%d,%d,%d)\n",
e->v1,e->v2,e->weight,f->v1,f->v2,f->weight);
int r1 = e->v1 - f->v1;
if (r1 == 0)
return e->v2 - f->v2;
else
return r1;
}
/*** visualizers *****************/
void
displayEDGE(void *a,FILE *fp)
{
EDGE *e = a;
fprintf(fp,"(%d,%d,%d)",e->v1,e->v2,e->weight);
}