My C code ,always runtime error,is there anybody help me?


  • 0
    Y

    it alwasy get runtime error at input 4, [[3,0],[0,1]].but i try to print the results before returning .it's correct!!
    so i think the runtime error didn't take place in my code.someone can help me!?.. please.the system hint me
    Last executed input: 4, [[3,0],[0,1]]
    Stdout: return size:4 order: 2 1 0 3

    struct Edge{
        struct Edge* next;
        int dst;
    };
    
    struct Vertex{
        struct Edge* first;
    };
    
    struct Graph{
        struct Vertex* vertex;
        int num;
    };
    
    void createGraph(int** edgeInfo,struct Graph* graph,int edgeInfoLen,int numCourses){
        graph->num=numCourses;
        graph->vertex=(struct Vertex*)malloc(sizeof(struct Vertex)*(numCourses));
        int i,j;
        memset(graph->vertex,0,sizeof(graph->vertex)*numCourses);
        for(i=0;i<edgeInfoLen;i++){
                struct Vertex *vertex=graph->vertex+edgeInfo[i][1];
                if(!vertex->first){
                        vertex->first=(struct Edge*)malloc(sizeof(struct Edge));
                        vertex->first->dst=edgeInfo[i][0];
                        vertex->first->next=NULL;
                }
                else{
                        struct Edge* edge=vertex->first;
                        while(edge->next)
                                edge=edge->next;
                        edge->next=(struct Edge*)malloc(sizeof(struct Edge));
                        edge->next->dst=edgeInfo[i][0];
                        edge->next->next=NULL;
                }
        }
        
     }
    
     void topoLogical(struct Graph* graph,int* order,int* returnSize){
        int* degree = (int*)malloc(sizeof(int)*graph->num);
        memset(degree,0,sizeof(degree)*graph->num);
        int top=0;
        int* stack=(int*)malloc(sizeof(int)*graph->num);
        struct Edge* tmp;
        int i;
       // printf("order adressd");
        for(i=0;i<graph->num;i++){
                tmp = graph->vertex[i].first;
                while(tmp){
                        degree[tmp->dst]++;
                        tmp=tmp->next;
                }
        }
        int count=0;
        for(i=0;i<graph->num;i++)
                if(degree[i]==0)
                        stack[top++]=i;
        i=0;
        //printf("order adressd");
        while(top!=0){
                order[i]=stack[--top];
                struct Edge* var=graph->vertex[order[i]].first;
                i++;
                while(var){
                        degree[var->dst]--;
                        if(degree[var->dst]==0)
                                stack[top++]=var->dst;
                        var=var->next;
                }
        }
        //printf("order adressd");
        if(i<graph->num)
                *returnSize=0;
      }
    
    int* findOrder(int numCourses, int** prerequisites, int prerequisitesRowSize, int prerequisitesColSize,             int* returnSize) {
        int* order = (int*)malloc(sizeof(int)*numCourses);
        if(numCourses==0){
                *returnSize=0;
                return order;
        }      
        *returnSize=numCourses;
        if(prerequisitesRowSize==0){   
                for(int i=0;i<numCourses;i++){
                        order[i]=i;
                        //printf("%d",order[i]);
                }
                //printf("order adress:%d",order);
                return order;
        }
    
        struct Graph graph;
        createGraph(prerequisites,&graph,prerequisitesRowSize,numCourses);
    
        topoLogical(&graph,order,returnSize);
        printf("return size:%d\n",*returnSize);
        printf("order: ");
        for(int i=0;i<*returnSize;i++)
           printf("%d ",order[i]);
        return order;
     }

Log in to reply
 

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