Runtime Error Message: Line 52: member access within null pointer of type 'UndirectedGraphNode *[101]'


  • 0
    M

    Can someone help me with whats wrong here?

    /**
     * #define NEIGHBORS_MAX_SIZE 100
     * struct UndirectedGraphNode {
     *     int label;
     *     struct UndirectedGraphNode *neighbors[NEIGHBORS_MAX_SIZE];
     *     int neighborsCount;
     * };
     */
    
    struct UndirectedGraphNode *map[NEIGHBORS_MAX_SIZE + 1];
    
    struct UndirectedGraphNode *newNode(int label, int neighborsCount)
    {
        struct UndirectedGraphNode *copy = calloc(1, sizeof(struct UndirectedGraphNode));
        
        copy->label = label;
        copy->neighborsCount = neighborsCount;
        
        map[label] = copy;
        return copy;
    }
    
    void dfsUtil(struct UndirectedGraphNode *src)
    {
        int i = 0;
        struct UndirectedGraphNode *srcCopy = NULL;
        
        if(!src) return;
        
        srcCopy = map[src->label];
        
        if(!srcCopy)
        {
            srcCopy = newNode(src->label, src->neighborsCount);
        }
        
        for(i = 0; i<NEIGHBORS_MAX_SIZE; i++)
        {       
            if(src->neighbors[i])
            {
                dfsUtil(src->neighbors[i]);
                srcCopy->neighbors[i] = map[src->neighbors[i]->label];
            }
        }
        return;
    }
    
    struct UndirectedGraphNode *cloneGraph(struct UndirectedGraphNode *graph)
    {
        memset(map, 0, sizeof(map));
        dfsUtil(graph);
        if(graph->label < NEIGHBORS_MAX_SIZE ) return map[graph->label];
        return NULL;
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.