lem_in/srcs/lm_graph_utils.c
Mthandazo Ndhlovu 1abb3fc566 invalid maps
2019-04-02 15:09:43 +02:00

62 lines
2.0 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lm_graph_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/29 07:17:06 by mndhlovu #+# #+# */
/* Updated: 2019/03/29 07:17:26 by mndhlovu ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
t_adjlist *lm_new_node(int dest)
{
t_adjlist *newnode;
if (!(newnode = (t_adjlist *)malloc(sizeof(t_adjlist))))
return (NULL);
newnode->dest = dest;
newnode->next = NULL;
return (newnode);
}
t_graph *lm_creategraph(int v)
{
t_graph *newgraph;
int index;
index = -1;
if (!(newgraph = (t_graph *)malloc(sizeof(t_graph))))
return (NULL);
newgraph->v = v;
//create an array of adjacency list. size of array will be v
if (!(newgraph->array = (t_adj *)malloc(sizeof(t_adj) * v)))
return (NULL);
while (++index < v)
newgraph->array[
index].head = NULL;
return (newgraph);;
}
void lm_add_edge(t_graph *graph, int src, int dest)
{
t_adjlist *newnode;
/*
* Add an edge from src to dest. A new node is added to the adjacency list of
* src. The node is added at the beginning
*/
newnode = lm_new_node(dest);
newnode->next = graph->array[src].head;
graph->array[src].head = newnode;
/*
* Since graph is undirected, add an edge from dest to src also
*/
newnode = lm_new_node(src);
newnode->next = graph->array[dest].head;
graph->array[dest].head = newnode;
}