/* forward declarations so self-referential structures are simpler */ typedef struct vertex vertex_t; typedef struct adj_vertex adj_vertex_t; typedef struct tour tour_t; struct vertex { char *name; adj_vertex_t *adj_list; vertex_t *next; }; struct adj_vertex { vertex_t *vertex; int edge_weight; adj_vertex_t *next; }; //doubly linked list with some metadata struct tour { vertex_t *node; int links; int curr; int length; tour_t *prev; tour_t *next; }; /* This is the one function you really should implement as part of your * graph data structure's public API. * * `add_edge` adds the specified edge to the graph passed in via the first * argument. If either of the edge's vertices are not already in the graph, * they are added before their adjacency lists are updated. If the graph * is currently empty (i.e., *vtxhead == NULL), a new graph is created, * and the caller's vtxhead pointer is modified. * * `vtxhead`: the pointer to the graph (more specifically, the head of the * list of vertex_t structures) * `v1_name`: the name of the first vertex of the edge to add * `v2_name`: the name of the second vertex of the edge to add * `weight` : the weight of the edge to add **/ //I decided to not have a pointer to a pointer to the first struct, but instead require a call to add_vert with the first name as the initilizer. vertex_t* add_edge (vertex_t *vtxhead, char *v1_name, char *v2_name, int weight); //adds a vertex and returns its address. Used to initialize list. vertex_t* add_vert(vertex_t *curr, char *name); //wrapper to handle linking. void add_link(vertex_t *v1, vertex_t *v2, int weight); //adds a flag so I can flip inputs and add links both ways. void add_link_help(vertex_t *v1, vertex_t *v2, int weight, int count); //helper adj_vertex_t* find_last(adj_vertex_t *curr); //helper vertex_t* find_vert(vertex_t *curr, char *name); //finds and prints a tour (if possible) and returns the head of the tour for freeing tour_t* tour(vertex_t* curr); //prints the tour iteratively, then prints the sum void print_tour(tour_t* head); //easy to reuse creation function tour_t* make_tour_node(vertex_t* curr, tour_t* prev); //weird mix of recursive and iterative, but it works int search(tour_t* head); //tells how many links search needs to iterate through int count_adj(adj_vertex_t * curr); //check that the thing we want to add to the tour isn't already in the tour int is_in_tour(tour_t* curr, vertex_t *key); //get the vertex we are going to add to the tour vertex_t* get_adj(int spot, adj_vertex_t * curr); //used to get the sum int get_weight(int spot, adj_vertex_t * curr); //recurse through the graph and /* FREE ,, ';; '' ____ || ; \ || \,---'-,-, || / ( o) || (o )__,--'-' \ || ,,,, ;'uuuuu'' ) ;; \ \ \ ) ) /\// '--' \'nnnnn' / \ \\ //'------' \ \\ // \ \ \\ // ) ) \\// | | \\ / | ALL THE THINGS */ void free_all(vertex_t *curr); void free_adj(adj_vertex_t *curr); void free_tour(tour_t * curr);