Skip to content

Commit d6f9638

Browse files
committed
Implement q_size
Implement this function to return the size of a queue. Using list_for_each to enumerate the element in the queue, and count will be added with each element. Change-Id: Ia2fe2bb6a2241fa5523f83a78fbc072b7fb2781d
1 parent 5a1e92d commit d6f9638

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

queue.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ element_t *q_remove_tail(struct list_head *head, char *sp, size_t bufsize)
7878
/* Return number of elements in queue */
7979
int q_size(struct list_head *head)
8080
{
81-
return -1;
81+
if (!head)
82+
return 0;
83+
struct list_head *cur;
84+
int size = 0;
85+
list_for_each (cur, head)
86+
size++;
87+
return size;
8288
}
8389

8490
/* Delete the middle node in queue */

0 commit comments

Comments
 (0)