/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* lm_graph_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: mndhlovu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/29 07:17:06 by mndhlovu #+# #+# */ /* Updated: 2019/03/29 07:17:26 by mndhlovu ### ########.fr */ /* */ /* ************************************************************************** */ #include "lem_in.h" t_ind *lm_new_adjnode(int dest) { t_ind *new; if (!(new = (t_ind *)ft_memalloc(sizeof(t_ind)))) return (NULL); new->index = dest; new->weight = 1; new->next = NULL; return (new); } 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 *)ft_memalloc(sizeof(t_graph)))) return (NULL); newgraph->v = v; //create an array of adjacency list. size of array will be v if (!(newgraph->array = (t_lm_adj *)ft_memalloc(sizeof(t_lm_adj) * v))) return (NULL); while (++index < v) newgraph->array[ index].head = NULL; return (newgraph);; } int lm_adj_parser(t_graph *graph, t_holder *holder) { t_temp *data; if (holder != NULL) { data = holder->data; if (data != NULL) { while (data) { lm_add_edge(graph, data->src_ind, data->dest_ind); data = data->next; } return (1); } } return (0); } void lm_add_edge(t_graph *graph, int src, int dest) { t_ind *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_adjnode(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_adjnode(src); newnode->next = graph->array[dest].head; graph->array[dest].head = newnode; }