lem_in/srcs/lm_graph_utils.c
2019-04-01 11:48:33 +02:00

68 lines
2.4 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_adjnode *lm_new_node(int dest)
{
t_adjnode *new;
if (!(new = (t_adjnode *)malloc(sizeof(t_adjnode))))
return (NULL);
new->dest = dest;
new->next = NULL;
return (new);
}
t_graph *lm_creategraph(int v)
{
t_graph *new_graph;
int index;
index = -1;
if (!(new_graph = (t_graph *)malloc(sizeof(t_graph))))
return (NULL);
new_graph->vert = v;
if (!(new_graph->array = (t_adjlist *)malloc(sizeof(t_adjlist) * v)))
return (NULL);
while (++index < v)
new_graph->array[index].head = NULL;
return (new_graph);
}
void lm_ext_conn(t_graph *graph, t_lmdata *data,
char *raw, t_syntax *synt)
{
char **tab;
if (synt->gr_status == 5)
{
graph = lm_creategraph(data->nb_nodes);
synt->gr_status = 1;
}
tab = ft_strsplit(raw, '-');
if (tab != NULL)
lm_add_edge(graph, lm_find_index(data, tab[0]), lm_find_index(data, tab[1]));
}
//function to add the edges and create a proper adjacency list
void lm_add_edge(t_graph *graph, int src, int dest)
{
t_adjnode *newnode;
newnode = lm_new_node(dest);
newnode->next = graph->array[src].head;
graph->array[src].head = newnode;
newnode = lm_new_node(src);
newnode->next = graph->array[dest].head;
graph->array[dest].head = newnode;
}