0

So, I have this 2 structs and the one global variable

struct Tasks{

    int tid;                      
    int difficulty;               
    struct Tasks *next;           
};

struct Head_GL{

    int tasks_count[3];           
    struct Tasks *head;           
};

struct Head_GL *tasks_head;

and I have to create a linked list with acceding order by difficulty. How is it possible to make comparision and read the difficulty. I did this tasks_head->head->difficulty and gives me segmentation fault

8
  • You will need to allocate memory to them first. Commented Nov 9, 2020 at 14:15
  • tasks_head->head=(struct Tasks*)malloc(sizeof(struct Tasks)); like this? Commented Nov 9, 2020 at 14:17
  • You also need allocation for tasks_head. Commented Nov 9, 2020 at 14:18
  • so before i allocate tasks_head->head i have to allocate tasks_head tasks_head=(struct Head_GL*)malloc(sizeof(struct Head_GL)); Commented Nov 9, 2020 at 14:21
  • 1
    You can drop the casts, they are useless: (struct Tasks*)malloc(sizeof(struct Tasks)) -> malloc(sizeof(struct Tasks)) Commented Nov 9, 2020 at 14:25

2 Answers 2

1

You need to create each item:

tasks_head = calloc(1, sizeof(struct Head_GL));
tasks_head->head = calloc(1, sizeof(struct Tasks));

You can then populate and use them. You also need to remember to free these later.

Sign up to request clarification or add additional context in comments.

2 Comments

The structs (does he need typedefs around that too...)
They should be struct Head_GL and struct Tasks.
0

I allocate the memory but i have a problem to printf the results. Segmantation fault in printf in main (tasks_head->head->tid). Any help?

struct Tasks *new=(struct Tasks*)malloc(sizeof(struct Tasks));


tasks_head=(struct Head_GL*)malloc(sizeof(struct Head_GL));
tasks_head->head=(struct Tasks*)malloc(sizeof(struct Tasks));

tasks_head->tasks_count[0]=0;
tasks_head->tasks_count[1]=0;
tasks_head->tasks_count[2]=0;
tasks_head->head->difficulty=0;
tasks_head->head->tid=0;
tasks_head->head->next=NULL;

if(new==NULL)
    return 0;
new->tid = tid;
new->difficulty = difficulty;
new->next = NULL;

if(difficulty==1)
    tasks_head->tasks_count[0]++;
else if(difficulty==2)
    tasks_head->tasks_count[1]++;
else
    tasks_head->tasks_count[2]++;


if(tasks_head==NULL){
    tasks_head->head = new;
    return 1;
}
if( tasks_head->head->difficulty > difficulty){
    new->next = tasks_head->head;
    tasks_head->head= new;
    return 1;
}
else{
    prev = tasks_head->head;
    temp = tasks_head->head->next;
    while(temp != NULL && temp->difficulty < difficulty){
        prev = temp;
        temp = temp->next;
    }
    if(temp==NULL){
        prev->next = new;
        return 1;
    }
    else{
        new->next = temp;
        prev->next = new;
        return 1;
    }
}

}

int main(){

printf("hello1\n");

if(1==insert_task(1,1))
    printf("alo");

if(1==insert_task(4,1))
    printf("alo");

if(1==insert_task(3,2))
    printf("alo\n");


printf("%d\n",num);

for(int i=0; i<num; i++){
    printf("%d\n",tasks_head->head->tid);
    tasks_head->head=tasks_head->head->next;
}

return 0;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.