Skip to content

Commit 71928ec

Browse files
committed
Implement q_insert_head
Previously, q_insert_head() was a placeholder and did nothing. Now it correctly inserts a new element at the head of the queue, allocating memory and linking it properly.
1 parent dfb08dd commit 71928ec

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

queue.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ void q_free(struct list_head *head)
4040
/* Insert an element at head of queue */
4141
bool q_insert_head(struct list_head *head, char *s)
4242
{
43+
if (!head)
44+
return false;
45+
element_t *new_elem = malloc(sizeof(element_t));
46+
if (!new_elem)
47+
return false;
48+
new_elem->value = s;
49+
list_add(&new_elem->list, head);
4350
return true;
4451
}
4552

0 commit comments

Comments
 (0)