From d69e38b759b5530ae1a96880e1d512be55ad9fe3 Mon Sep 17 00:00:00 2001 From: Aaron Liao Date: Sat, 16 Mar 2024 15:48:52 +0800 Subject: [PATCH] Implement q_size function Implement q_size() to get the size of the queue. --- queue.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/queue.c b/queue.c index ce85f7d16..eac0cdce6 100644 --- a/queue.c +++ b/queue.c @@ -47,7 +47,14 @@ element_t *q_remove_tail(struct list_head *head, char *sp, size_t bufsize) /* Return number of elements in queue */ int q_size(struct list_head *head) { - return -1; + if (!head) + return 0; + + int size = 0; + struct list_head *curr; + list_for_each (curr, head) + size++; + return size; } /* Delete the middle node in queue */