Skip to content

Commit

Permalink
Fix some bugs in q_free
Browse files Browse the repository at this point in the history
malloc return NULL with string value
q_sort function still with some bug may be in mergeTwoSingleList
make test score 70, still not completely pass
  • Loading branch information
Dayy0v0 committed Jan 5, 2024
1 parent 5efdaf5 commit f73dda0
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ bool q_insert_head(struct list_head *head, char *s)
if (!elem)
return false;
elem->value = (char *) malloc(strlen(s) + 1);
if (!elem->value) {
free(elem);
return false;
}
strncpy(elem->value, s, strlen(s) + 1);
list_add(&elem->list, head);
return true;
Expand All @@ -59,6 +63,10 @@ bool q_insert_tail(struct list_head *head, char *s)
if (!elem)
return false;
elem->value = (char *) malloc(strlen(s) + 1);
if (!elem->value) {
free(elem);
return false;
}
strncpy(elem->value, s, strlen(s) + 1);
list_add_tail(&elem->list, head);
return true;
Expand Down Expand Up @@ -348,7 +356,7 @@ int q_merge(struct list_head *head, bool descend)
queue_contex_t *queue1 = list_entry(curr, queue_contex_t, chain);
queue_contex_t *queue2 = list_entry(head->next, queue_contex_t, chain);
result += queue1->size;
if (!queue2->q && !list_empty(queue2->q)) {
if (queue2->q && (!list_empty(queue2->q))) {
queue1->q->prev->next = NULL;
queue2->q->prev->next = NULL;
queue2->size = 0;
Expand Down

0 comments on commit f73dda0

Please sign in to comment.