Skip to content

Commit

Permalink
Implement q_reverse function
Browse files Browse the repository at this point in the history
Implement q_reverse() to reverse all elements in the queue.
  • Loading branch information
aftuta85 committed Mar 17, 2024
1 parent 47e903e commit 95c060a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 95c060a

Please sign in to comment.