#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Problem with insert vertex to a graph. (corrected code format)

ChinaMember Posts: 1

I have a graph algorithm and want to use it to realize the shortest path problem, but I have trouble when I insert vertex to graph. The first vertex can be inserted successfully, but I seems that I can't insert more. I wrote some printf() function to see where did the problem happen, but I can't see it. Can anyone help me?
Here's the code:

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/ List element structure */
typedef struct ListElmt_
{
void *data;
struct ListElmt_ *next;
} ListElmt;

/* List structure */
typedef struct List_
{
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
ListElmt *tail;
} List;

/* Set structure */
typedef List Set;

/* Graph element structure */
{
void *vertex;

/* Graph structure */
typedef struct Graph_
{
int vcount;
int ecount;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
}Graph;

/* Define a structure for vertices in shortest-path problem .*/
typedef struct PathVertex_
{
void *data;
double weight;

VertexColor color;
double  d;

struct PathVertex_ *parent;
}PathVertex;

/ *Match function to initialize list, set and graph. */
int _match(const void *key1, const void *key2)
{
if(key1 == key2)
return 0;
else
return 1;
}

/* Destroy function to initialize list, set and graph. */
void _destroy(void *data)
{
free(data);
}

#define list_tail(list) ((list) -> tail)
#define list_data(element) ((element) -> data)
#define list_next(element) ((element) -> next)

/*list_init*/
void list_init(List *list,void (*destroy)(void *data)) {
list -> size = 0;
list -> destroy = destroy;
list -> tail = NULL;

return;
}

int list_ins_next(List *list, ListElmt *element, const void *data)
{
ListElmt      *new_element;

if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL)
return -1;

new_element -> data = (void *)data;
if(element == NULL) {
if (list_size(list) == 0)
list -> tail = new_element;

new_element -> next = list -> head;
}
else {

if (element -> next == NULL)
list -> tail = new_element;

new_element -> next = element -> next;
element -> next = new_element;
}

list -> size++;

return 0;
}

void set_init(Set *set, int (*match)(const void *key1, const void *key2), void (*destroy)(void *data))
{
list_init(set, destroy);
set -> match = match;
return;
}

//graph_init
void graph_init(Graph *graph, int (*match)(const void *key1, const void *key2), void(*destroy)(void *data))
{
graph -> vcount = 0;
graph -> ecount = 0;
graph -> match = match;
graph -> destroy = destroy;

return;
}

int graph_ins_vertex(Graph *graph, const void *data)
{
ListElmt *element;
int retval;

{
if(graph -> match(data, ((AdjList *)list_data(element)) -> vertex))
return 1;
}

return -1;

adjlist -> vertex = (void *)data;
{
return retval;
}

graph -> vcount++;
return 0;
}

int main()
{
Graph _graph;
Graph *graph = &_graph;
PathVertex pvertex1,
*pathvertex1 = &pvertex1;
char city[6] = {'a','b','c','d','e','f'};
int i = 0;

int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);

/*Match and destroy function. Use void pointer to realize.*/
match = &_match;
destroy = &_destroy;

/* Initialize graph. */
graph_init(graph, match, destroy);

/* Insert vertex to graph. */
for(i; i < 6; ++i)
{
pathvertex1 -> data = &_city[i];
graph_ins_vertex(graph, (const PathVertex *)pathvertex1);
}

return 0;
}
``````
Tagged: