Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C, please help me fix these 2 issues with my code below //headerfile #ifndef MYGRAPH_H #define MYGRAPH_H #include #include #include typedef struct neighbor{ int

In C, please help me fix these 2 issues with my code below

image text in transcribed

//headerfile

#ifndef MYGRAPH_H #define MYGRAPH_H

#include #include #include

typedef struct neighbor{ int data; struct neighbor * next; }neighbor_t;

typedef struct node{ int data; struct node *next; struct neighbor * neighbor; }node_t;

typedef struct graph{ int numNodes; int numEdges; node_t* nodes; }graph_t;

graph_t* create_graph(){

graph_t* myGraph= (graph_t *)malloc(sizeof(graph_t)); myGraph->numNodes = 0; myGraph->numEdges = 0; myGraph->nodes = NULL; return myGraph; }

int graph_empty(graph_t* g){ if(g->numNodes == 0){ return 0; } return -1; }

int graph_add_node(graph_t* g, int item){ node_t* newNode = (node_t *) malloc(sizeof(node_t)); if(newNode == NULL) return -1;

newNode->data = item; newNode->neighbor = NULL; newNode->next = NULL;

g->numNodes++;

if(g->nodes == NULL) g->nodes = newNode; else { node_t *temp = g->nodes; while(temp->next != NULL) { temp = temp->next; } temp->next = newNode; } return 0; }

int graph_remove_node(graph_t* g, int item){ if(g->nodes == NULL) return -1;

node_t *temp = g->nodes; node_t *prev = NULL; while(temp != NULL && temp->data != item) { prev = temp; temp = temp->next; }

if(temp == NULL) return -1;

prev->next = temp->next; g->numNodes--;

free(temp);

return 0; }

int graph_add_edge(graph_t * g, int source, int destination){

if(g == NULL) return -1;

node_t *temp = g->nodes; while(temp != NULL && temp->data != source) { temp = temp->next; } if(temp == NULL) return -1;

neighbor_t *temp_n = temp->neighbor; neighbor_t* new_neighbor = (neighbor_t *) malloc(sizeof(neighbor_t));

if(new_neighbor == NULL) return -1;

new_neighbor->data = destination; new_neighbor->next = NULL;

if(temp_n == NULL) { temp_n = new_neighbor; } else { while(temp_n->next != NULL) { temp_n = temp_n->next; } temp_n->next = new_neighbor; } g->numEdges++;

return -1; }

int graph_remove_edge(graph_t * g, int source, int destination){ if(g == NULL) return -1;

node_t *temp = g->nodes; while(temp != NULL && temp->data != source) { temp = temp->next; } if(temp == NULL) return -1;

neighbor_t *temp_n = temp->neighbor; neighbor_t *prev_n = NULL;

if(temp_n == NULL) { return -1; }

while(temp_n != NULL && temp_n->data != destination) { prev_n = temp_n; temp_n = temp_n->next; }

prev_n->next = temp_n->next; free(temp_n); g->numEdges--; return -1; }

int contains_node( graph_t * g, int value){

if(g->nodes == NULL) return -1;

node_t *temp = g->nodes; while(temp->next != NULL) { if(temp->data == value) return 0; }

return -1; }

int contains_edge( graph_t * g, int source, int destination){ if(g == NULL) return -1;

node_t *temp = g->nodes; while(temp != NULL && temp->data != source) { temp = temp->next; } if(temp == NULL) return -1;

neighbor_t *temp_n = temp->neighbor;

if(temp_n == NULL) { return -1; }

while(temp_n != NULL && temp_n->data != destination) { temp_n = temp_n->next; }

if(temp_n == NULL) return -1;

return 0;

}

int * getNeighbors( graph_t * g, int value ){

if(g->nodes == NULL) return NULL; node_t *temp = g->nodes; while(temp != NULL && temp->data != value) { temp = temp->next; }

if(temp == NULL || temp->neighbor == NULL) return NULL;

neighbor_t *temp_n = temp->neighbor;

while(temp_n != NULL) { return &(temp_n->data); }

return NULL; }

int numNeighbors( graph_t * g, int value ){ if(g->nodes == NULL) return 0;

node_t *temp = g->nodes; while(temp != NULL && temp->data != value) { temp = temp->next; }

if(temp == NULL || temp->neighbor == NULL) return 0;

int neighbor_count = 0;

neighbor_t *temp_n = temp->neighbor;

while(temp_n != NULL) { neighbor_count++; temp_n = temp_n->next; } return neighbor_count; }

void graph_print(graph_t * g){ if(g == NULL) return;

node_t * temp = g->nodes;

while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } }

unsigned int graph_num_nodes(graph_t* g){ return g->numNodes; }

unsigned int graph_num_edges(graph_t* g){ return g->numEdges; }

void free_graph(graph_t* g){

if(g == NULL) return;

node_t*prev; node_t *temp = g->nodes;

while(temp != NULL) { prev = temp; temp = temp->next; free(prev); }

free(g); }

int is_reachable(graph_t * g, int source, int dest) {

node_t * temp = g->nodes;

bool isSourceFound = false;

while(temp != NULL)

{

if (temp->data == source && !isSourceFound) {

isSourceFound = true;

}

if (isSourceFound) {

if (temp->data == dest) {

return 0;

}

}

temp = temp->next;

}

return -1;

}

int has_cycle(graph_t * g) {

node_t * temp = g->nodes;

node_t * nextTemp = g->nodes->next->next;

while(temp != NULL && temp != g->nodes)

{

if (nextTemp == NULL) {

return -1;

}

if (temp->data == nextTemp->data) {

return 0;

break;

}

temp = temp->next;

nextTemp = nextTemp->next->next;

}

return -1;

}

void print_path(graph_t * g, int source, int dest) {

node_t * temp = g->nodes;

while(temp != NULL)

{

printf("%d ", temp->data);

temp = temp->next;

}

}

#endif

//Main.c file:

#include

#include

#include "my_graph.h"

int main(){ graph_t * testGraph = create_graph(); free_graph(testGraph); return 0; }

main.c: In function 'main' main.c:21:5: error: unknown type name 'graph t' graph-t * testGraph create-graph(); main.c:21:27: warning: initialization makes pointer from integer without a cast [enabled by default] graph t testGraphcreate_graph)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Carlos Coronel, Steven Morris

14th Edition

978-0357673034

More Books

Students also viewed these Databases questions

Question

3. Give examples of four fair disciplinary practices.

Answered: 1 week ago

Question

4. Explain how to use fair disciplinary practices.

Answered: 1 week ago