C functions to delete edges and nodes from a Graph

0 votes
asked May 17 by (130 points)

Hi guys, I would like help with writing two functions: one to delete an edge from a Graph and one to delete a node from a Graph. The implementation I am currently using is the following: 

struct Graph{

  int V;

  struct AdjList* array;


 // A structure to represent an adjacency list node 

struct AdjListNode 

    int dest; 

    struct AdjListNode* next; 


// A utility function to create a new adjacency list node 

struct AdjListNode* newAdjListNode(int dest) 

    struct AdjListNode* newNode = 

     (struct AdjListNode*) malloc(sizeof(struct AdjListNode)); 

    newNode->dest = dest; 

    newNode->next = NULL; 

    return newNode; 


// A structure to represent an adjacency list 

struct AdjList 

    struct AdjListNode *head;  


struct Graph* createGraph(int V) 

    struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph)); 

    graph->V = V; 


    // Create an array of adjacency lists.  Size of array will be V 

    graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList)); 


    // Initialize each adjacency list as empty by making head as NULL 

    for (int i = 0; i < V; ++i) 

        graph->array[i].head = NULL; 


    return graph; 

// Adds an edge to an undirected graph 

void addEdge(struct Graph* graph, int src, int dest) 

    // Add an edge from src to dest.  A new node is  

    // added to the adjacency list of src.  The node 

    // is added at the begining 

    struct AdjListNode* newNode = newAdjListNode(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 = newAdjListNode(src); 

    newNode->next = graph->array[dest].head; 

    graph->array[dest].head = newNode; 

Thank you in advance!

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.