Skip to content

Commit a41ccb4

Browse files
committed
Fix q_size to return correct queue length
Previously, q_size always returned -1, which was incorrect. Now, it correctly iterates through the list and returns the actual length. Additionally, a null check for head was added to return 0. Change-Id: Ic9af6472a5516593a68a3a8e0d3f0746b8dc43f7
1 parent 2108bce commit a41ccb4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

queue.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ element_t *q_remove_tail(struct list_head *head, char *sp, size_t bufsize)
4646
/* Return number of elements in queue */
4747
int q_size(struct list_head *head)
4848
{
49-
return -1;
49+
if (!head)
50+
return 0;
51+
52+
int len = 0;
53+
struct list_head *li;
54+
55+
list_for_each (li, head)
56+
len++;
57+
return len;
5058
}
5159

5260
/* Delete the middle node in queue */

0 commit comments

Comments
 (0)