Skip to content

Commit

Permalink
Implement two types of q_insert functions
Browse files Browse the repository at this point in the history
Implement the following insert functions:
- q_insert_head(): Insert an element at the head of the queue
- q_insert_tail(): Insert an element at the tail of the queue
  • Loading branch information
aftuta85 committed Mar 16, 2024
1 parent 61395de commit f61bded
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,40 @@ void q_free(struct list_head *head) {}
/* Insert an element at head of queue */
bool q_insert_head(struct list_head *head, char *s)
{
if (!head)
return false;

element_t *new_element = malloc(sizeof(element_t));
if (!new_element)
return false;

new_element->value = strdup(s);
if (!new_element->value) {
free(new_element);
return false;
}

list_add(&new_element->list, head);
return true;
}

/* Insert an element at tail of queue */
bool q_insert_tail(struct list_head *head, char *s)
{
if (!head)
return false;

element_t *new_element = malloc(sizeof(element_t));
if (!new_element)
return false;

new_element->value = strdup(s);
if (!new_element->value) {
free(new_element);
return false;
}

list_add_tail(&new_element->list, head);
return true;
}

Expand Down

0 comments on commit f61bded

Please sign in to comment.