-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertex.c
172 lines (145 loc) · 3.63 KB
/
vertex.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* VERTEX class - written by John C. Lusth
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "vertex.h"
#include "integer.h"
#define DEBUG 0
#if DEBUG
#define dprintf printf
#else
#define dprintf(...)
#endif
struct vertex
{
int number; //vertex identifier
DLL *neighbors; //the adjacency list
DLL *weights; //the adjacency list
int key; //reserved for graph algorithms
int flag; //reserved for graph algorithms
DLL *successors; //reserved for graph algorithms
VERTEX *pred; //reserved for graph algorithms
void *owner; //reserved for graph algorithms
};
/***** public methods *******************************************************/
/*** constructors/desctructors ***/
VERTEX *
newVERTEX(int n)
{
VERTEX *v = malloc(sizeof(VERTEX));
assert(v != 0);
v->number = n;
v->key = -1;
v->flag = 0;
v->pred = 0;
v->owner = 0;
v->neighbors = newDLL(displayVERTEX,0);
v->weights = newDLL(displayINTEGER,freeINTEGER);
v->successors = newDLL(displayVERTEX,0);
return v;
}
void
freeVERTEX(void *w)
{
VERTEX *v = w;
freeDLL(v->neighbors);
freeDLL(v->weights);
free(v);
}
/*** accessors *******************/
int getVERTEXnumber(VERTEX *v) { return v->number; }
int getVERTEXflag(VERTEX *v) { return v->flag; }
void *getVERTEXowner(VERTEX *v) { return v->owner; }
VERTEX *getVERTEXpred(VERTEX *v) { return v->pred; }
int getVERTEXkey(VERTEX *v) { return v->key; }
DLL *getVERTEXneighbors(VERTEX *v) { return v->neighbors; }
DLL *getVERTEXweights(VERTEX *v) { return v->weights; }
DLL *getVERTEXsuccessors(VERTEX *v) { return v->successors; }
/*** mutators ********************/
void *
setVERTEXowner(VERTEX *v,void *o)
{
void *temp = v->owner;
v->owner = o;
return temp;
}
int
setVERTEXflag(VERTEX *v,int f)
{
int temp = v->flag;
v->flag = f;
return temp;
}
VERTEX *
setVERTEXpred(VERTEX *v,VERTEX *p)
{
VERTEX *temp = v->pred;
v->pred = p;
return temp;
}
int
setVERTEXkey(VERTEX *v,int k)
{
int temp = v->key;
v->key = k;
return temp;
}
void
insertVERTEXneighbor(VERTEX *v,VERTEX *w)
{
insertDLL(v->neighbors,sizeDLL(v->neighbors),w);
}
void
insertVERTEXweight(VERTEX *v,int i)
{
insertDLL(v->weights,sizeDLL(v->weights),newINTEGER(i));
}
void
insertVERTEXsuccessor(VERTEX *v,VERTEX *w)
{
insertDLL(v->successors,sizeDLL(v->successors),w);
}
/*** informational ***************/
int
compareVERTEX(void *a,void *b)
{
VERTEX *x = a;
VERTEX *y = b;
dprintf("comparing vertex %d with vertex %d\n",x->number,y->number);
// printf("comparing vertex %d with vertex %d\n",x->number,y->number);
if (x->key == -1 && y->key != -1) {
// printf("both keys are -1\n");
return 1;
}
else if (x->key != -1 && y->key == -1) {
// printf("second one is -1\n");
return -1;
}
int r = x->key - y->key;
// printf("left - right: %d\n", r);
if (r == 0) {
// printf("r = 0, returning %d\n", x->number - y->number);
return x->number - y->number;
}
else
return r;
}
/*** visualizers *****************/
void
displayVERTEX(void *w,FILE *fp)
{
VERTEX *v = w;
fprintf(fp,"%d",v->number);
}
void
displayVERTEXdebug(void *v,FILE *fp)
{
VERTEX *a = v;
fprintf(fp,"%d:",a->number);
displayDLL(a->neighbors,fp);
displayDLL(a->weights,fp);
displayDLL(a->successors,fp);
}