Skip to content

Commit

Permalink
Implement two types of q_remove functions
Browse files Browse the repository at this point in the history
Implement the following remove functions:
- q_remove_head(): Remove an element from the head of the queue
- q_remove_tail(): Remove an element from the tail of the queue
  • Loading branch information
aftuta85 committed Mar 16, 2024
1 parent f61bded commit 311e71a
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,36 @@ bool q_insert_tail(struct list_head *head, char *s)
/* Remove an element from head of queue */
element_t *q_remove_head(struct list_head *head, char *sp, size_t bufsize)
{
return NULL;
if (!head || list_empty(head))
return NULL;

element_t *target = list_first_entry(head, element_t, list);
if (sp) {
int orig_len = strlen(target->value);
int copy_len = (orig_len < bufsize - 1) ? orig_len : bufsize - 1;
strncpy(sp, target->value, copy_len);
sp[copy_len] = '\0';
}

list_del(&target->list);
return target;
}

/* Remove an element from tail of queue */
element_t *q_remove_tail(struct list_head *head, char *sp, size_t bufsize)
{
return NULL;
if (!head || list_empty(head))
return NULL;

element_t *target = list_last_entry(head, element_t, list);
if (sp) {
int orig_len = strlen(target->value);
int copy_len = (orig_len < bufsize - 1) ? orig_len : bufsize - 1;
strncpy(sp, target->value, copy_len);
sp[copy_len] = '\0';
}
list_del(&target->list);
return target;
}

/* Return number of elements in queue */
Expand Down

0 comments on commit 311e71a

Please sign in to comment.