0

I need some help, I'm trying to create an adjacency list to represent a graph in C, I can add one edge to each node list, but I have no idea how to add more than one, I also dont know how to delete an edge , this is what I've got

edit 1 * I create the list inside the main function like an array: List *adjlist[Max]= {0};

typedef struct Node{
    int vertexNum;
    struct Node *next;
} Node;

typedef struct List{
    Node *head;
} List;

void Connect(List **adjlist, int v1, int v2){
    Node *dest, *tmp, *src;
    if(adjlist[v1]->head == NULL){
        src= (Node *)malloc(sizeof(Node));
        src->vertexNum = v2;
        src->next = NULL;
        adjlist[v1]->head=src;
    }

    dest = (Node *)malloc(sizeof(Node));
    dest->vertexNum = v2;
    dest->next=NULL;
    tmp=adjlist[v1]->head;

    while(tmp->next != NULL){
        tmp= tmp->next;
    }

        adjlist = tmp;
}
drpopp
  • 1
  • 1
  • 1
    Your list needs to hold "lists of nodes" instead of nodes. Then each can hold more than 1 entry. You could hard code an array of nodes to start with. – Michael Dorgan Apr 23 '20 at 23:02
  • 1
    Please _edit_ your question and post the rest of your code. We need to see how this function is called and how the lists are initialized. Try to do as a _single_ code block, that is downloadable, compilable, and runnable. Provide enough sample input to demonstrate the problem. Do you have a single list of all neighbor relationships? If so, I'd say `List **adjlist` (vs. `List *adjlist`) is suspect. – Craig Estey Apr 23 '20 at 23:02
  • `adjlist = tmp` doesn't seem to make much sense, given that `adjlist` is a `List**` and `tmp` is a `Node*`. Also, if the list is empty (i.e. head is `NULL`) you add the new `Node*` twice. – bitmask Apr 23 '20 at 23:43

0 Answers0