链表内的链表分段错误

时间:2017-09-21 04:08:43

标签: c linked-list



#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
struct smallnode{
    char name[4];
    struct smallnode *next;
};
struct bignode {
  int no;
  struct smallnode *element;
  struct smallnode *ehead;
  struct bignode *next;
};
    struct bignode *classes;
    struct bignode *head;

void add_class(int no){
    struct bignode *newclass = malloc(sizeof(struct bignode));
    newclass->no=no;
    newclass->element=NULL;newclass->ehead=NULL;
    if(head==NULL){classes = newclass;head = classes;head->next=NULL;}
    else{
        head->next=newclass;
        head = head->next;
        head->next=NULL;
    }
}

void add_student(int noc,char* name){
    struct bignode *pointer;
    pointer = classes;
    int i;
    // get the desired class
    for(i=2;i<=noc;i++){
        pointer=pointer->next;
    }
    //add student to class
    struct smallnode *newstud;
    newstud = malloc(sizeof(struct smallnode));
    strcpy(newstud->name,name);
    if(pointer->element==NULL){
        pointer->element=newstud;
        pointer->ehead = pointer->element;
        pointer->ehead->next=NULL;
    }
    else{
        pointer->ehead->next=newstud;
        pointer->ehead = pointer->ehead->next;
        pointer->ehead->next=NULL;
    }
}

void resetdata(){
    struct bignode *classpointer;
    struct bignode *t;
    struct smallnode *studentpointer;
    struct smallnode *c;
    classpointer = classes;
    while(classpointer!=NULL){
        t=classpointer;
        studentpointer=classpointer->element;
        while(studentpointer!=NULL){
            c = studentpointer;
            studentpointer= studentpointer->next;
            free(c);
        }
        classpointer=classpointer->next;
        free(t);
    }
    classes = NULL;
    head = NULL;
}

int main()
{
    FILE *fp,*op;
    int sclasw=0;int adstud=0; int noc=1;int cp=0; char c; char sname[4]=""; int sc=0;
    int cases;

    classes = NULL;
    head = NULL;
    fp = stdin;
    op = stdout;
    if (fp == NULL){
        printf("null");
    }
    cases =fgetc(fp)-'0';
    fgetc(fp);
    //reading
    int i;
for(i=1;i<=cases;i++){
    c = fgetc(fp);
    while(c!='\n'){
        c = fgetc(fp);
        if(c==':'){sclasw=1;}
        else if (c=='('&&sclasw==0){adstud=1;}
        else if (c=='('&&sclasw==1){}
        else if (c==')'&&sclasw==1){sclasw=0;add_class(noc);}
        else if (c==')'&&adstud==1){adstud=0;add_student(cp,sname);sc=0;}
        else if (sclasw==1&&c==','){
            add_class(noc);
            noc++;
        }
        else if (c==','&&adstud==1){
            add_student(cp,sname);
            sc=0;
        }
        else if (c==','&&sclasw==0&&adstud==0){
            cp++;sc=0;
        }
        else if(adstud==1){
            sname[sc]=c;
            sc++;
        }else if(sclasw==1){/*printf("-");*/}
    }
    //end reading
    //coloring algo
    struct bignode *classpointer;
    struct bignode *classpointer2;
    struct smallnode *studentpointer;
    struct smallnode *studentpointer2;
    int *color, totalcolor;
    color=malloc((noc+1)*sizeof(int));
    color[1]=1; totalcolor = 1;
    int col,oldcol;
    //breaker
    int b1=0,b2=0,b3=0;
    classpointer = classes->next;
    while(classpointer!=NULL){
        for(col=1;col<=totalcolor;){
            color[classpointer->no]=col;
            oldcol=col;
            studentpointer = classpointer->element;
            while (studentpointer!=NULL){//for the nth element
                classpointer2 = classes;
                while(classpointer2!=classpointer){//check the class
                    if (color[classpointer2->no]==col){
                        studentpointer2 = classpointer2->element;
                        while (studentpointer2!=NULL){// for an element
                            if(strcmp(studentpointer->name,studentpointer2->name)==0){//that has the same student as  the nth element
                                col++;//if theres the same student next color
                                color[classpointer->no]=col;
                                if(col>totalcolor){//if there is no next color create one
                                    totalcolor++;b1=1;
                                }
                                b2=1;
                                b3=1;
                                break;
                            }
                            studentpointer2 = studentpointer2->next;
                        }
                    }
                    classpointer2=classpointer2->next;
                    if(b3==1){b3=0;break;}
                }
                studentpointer = studentpointer->next;
                if(b2==1){b2=0;break;}
            }
            if (color[classpointer->no]==oldcol){break;}
            if(b1==1){b1=0;break;}
        }
        classpointer=classpointer->next;
    }
    //end algo
    fprintf(op,"Case %i: (",i);
    classpointer=classes;
    while(classpointer!=NULL){
        fprintf(op,"C%02i:%i",classpointer->no,color[classpointer->no]);
        classpointer=classpointer->next;
        if(classpointer!=NULL)fprintf(op,", ");
    }
    fprintf(op,")\n");
    resetdata();
    sclasw = 0; adstud = 0; noc=1;cp=0;sc = 0;
}
    return 0;
    fclose(fp);
    fclose(op);
}
&#13;
&#13;
&#13;

// EDIT
输入:

1
Case 1: (C01,C02,C03,C04,C05,C06,C07,C08,C09,C10,C11),(RDA,ALB,NAC,EDF,BMG,VLJ,IVL,LGM,EGN,KSO,EST,VIV),(MCA,EDF,SLF,ADG,BCG,AAI,RRK,LGM,RLM,JGP,LRQ,WAR,KLS,DDY),(ABC,BBD,GCF,ADG,AKL,BCL,MIN,JGP,RSQ,DBU,IEY,RAW,ESZ),(ANA,JCA,CAB,NAC,GCF,GLH,VLJ,LLM,MAN,PEP,PQQ,ERR,SET,MAV,REW),(BBC,EDD,HSE,EST,ELG,ISH,JEI,EMJ,RRK,TPL,RER,EPS,AVU,CDW,ELY),(ALA,MCA,ABB,BCF,GLH,AKL,HGN,RON,JGP,ALQ,EPR,ABT,KEV,YEZ),(CDC,ISH,ABI,DHJ,ESM,FBM,RMN,PEP,VIR,JLS,LOT,MAV,TEX),(AAA,HLA,BBD,WRE,ECG,HLH,DHJ,RON,TSO,PQQ,MBT,REW,BAX,TRY,BDZ),(MCA,JCA,BCF,EGG,AAI,XTK,WIL,CSM,HLO,RSP,APR,RER,JET,DBU),(RDA,BBB,CLC,ECG,MNH,EMJ,JOK,ARM,NFM,EGN,RCN,RSP,LEQ,YIR,AVU),(ADB,WDB,BKC,CLC,SDE,UKF,BMG,HRH,BTK,LGM,QJP,EPS,KLS,BST,YNZ)

输出(在代码块中工作正常):

Case 1: (C01:1, C02:2, C03:1, C04:2, C05:3, C06:3, C07:1, C08:4, C09:4, C10:2, C11:4)

PS。我已经编辑了我的源代码,这样就没有像阵列内存不足那样的小错误。

该程序使用2个数组进行图形着色。一个用于课程,另一个用于学生。较大的链表(即类的链表)在每个节点中都有一个较小的链表的元素;

Hackerrank的编译器说它有一个分段错误,但是当我在代码块中运行它时它工作正常。 我已经坚持了几天,我不知道为什么会发生这种情况。请帮帮我:(

0 个答案:

没有答案