From 95c060a7c9732a77741f12e4fdad405413c06af0 Mon Sep 17 00:00:00 2001 From: Aaron Liao Date: Sun, 17 Mar 2024 19:03:05 +0800 Subject: [PATCH] Implement q_reverse function Implement q_reverse() to reverse all elements in the queue. --- queue.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/queue.c b/queue.c index 723364bf9..be07211e7 100644 --- a/queue.c +++ b/queue.c @@ -144,7 +144,19 @@ void q_swap(struct list_head *head) } /* Reverse elements in queue */ -void q_reverse(struct list_head *head) {} +void q_reverse(struct list_head *head) +{ + if (!head || list_empty(head)) + return; + + struct list_head *curr = head, *nxt = NULL; + while (nxt != head) { + nxt = curr->next; + curr->next = curr->prev; + curr->prev = nxt; + curr = nxt; + } +} /* Reverse the nodes of the list k at a time */ void q_reverseK(struct list_head *head, int k)