-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.c
49 lines (43 loc) · 1.08 KB
/
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
/**
* @author Harsh Rawat, harsh-rawat, hrawat2
* @author Sidharth Gurbani, gurbani, gurbani
*/
#include <stdlib.h>
#include <string.h>
#include "graph.h"
vertex *createVertexNode();
/**
* This method is used to create a new vertex with the given data
* */
vertex *CreateVertex(void *data) {
vertex *newNode = createVertexNode();
newNode->data = data;
return newNode;
}
/**
* This method is used to add a edge from a given vertex to a target vertex
* */
void AddEdge(vertex *from, vertex *to) {
if (!Contains(from->edges, to)) {
AddNode(from->edges, to);
}
}
/**
* This method is used to get the data from the given vertex
* */
void *GetData(vertex *vertex) {
if (vertex == NULL) {
return NULL;
}
return vertex->data;
}
/**
* This is an internal method which is used to create a vertex node by allocating the required memory
* */
vertex *createVertexNode() {
vertex *newNode = malloc(sizeof(vertex));
ValidateMemoryAllocationError(newNode);
newNode->data = NULL;
newNode->edges = CreateLinkedList();
return newNode;
}