-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge.c
72 lines (59 loc) · 1.66 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
/**
* @file edge.c
* @brief Weighted graph edges
* @author Pascal Getreuer <getreuer@gmail.com>
*
* Copyright (c) 2010-2011, Pascal Getreuer
* All rights reserved.
*
* This program is free software: you can use, modify and/or
* redistribute it under the terms of the simplified BSD License. You
* should have received a copy of this license along this program. If
* not, see <http://www.opensource.org/licenses/bsd-license.html>.
*/
#include "basic.h"
#include "edge.h"
/** @brief An empty edge list */
edgelist NullEdgeList = {NULL, NULL, 0};
/**
* @brief Add an edge to an edgelist
* @param List the edgelist to add edge to
* @param x1, y1, x2, y2 edge endpoints
* @return 1 on success, 0 on failure (memory allocation failure)
*/
int AddEdge(edgelist *List, int x1, int y1, int x2, int y2)
{
edge *NewEdge;
if(!List || !(NewEdge = (edge *)Malloc(sizeof(edge))))
return 0;
NewEdge->x1 = x1;
NewEdge->y1 = y1;
NewEdge->x2 = x2;
NewEdge->y2 = y2;
NewEdge->Weight = 1;
NewEdge->NextEdge = NULL;
if(!List->Tail) /* Add edge to empty list */
List->Head = List->Tail = NewEdge;
else /* Append the edge to the end of the list */
{
List->Tail->NextEdge = NewEdge;
List->Tail = NewEdge;
}
(List->NumEdges)++;
return 1;
}
/**
* @brief Free edges within an edgelist
* @param List the edgelist
*/
void FreeEdgeList(edgelist *List)
{
edge *Edge, *NextEdge;
for(Edge = List->Head; Edge; Edge = NextEdge)
{
NextEdge = Edge->NextEdge;
Free(Edge);
}
List->Head = List->Tail = NULL;
List->NumEdges = 0;
}