Skip to content

Commit

Permalink
Implement q_reverse function
Browse files Browse the repository at this point in the history
Utilize the list_for_each function to reverse the entire queue.
  • Loading branch information
aa860630 committed Mar 1, 2024
1 parent 39ea8d0 commit 72bd7b4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,20 @@ 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)
{
struct list_head *h = head->prev;
struct list_head *cur = head;
struct list_head *t = NULL;
list_for_each (t, head) {
cur->next = cur->prev;
cur->next->prev = cur;
h = cur;
cur = t;
}
cur->next = h;
h->prev = cur;
}

/* Reverse the nodes of the list k at a time */
void q_reverseK(struct list_head *head, int k)
Expand Down

0 comments on commit 72bd7b4

Please sign in to comment.