C Write a method Node NoderRotate which rightrotates a list

C++

Write a method Node *Node::rRotate() which right-rotates a list (the tail is moved to the head).

C++

Write a method Node *Node::rRotate() which right-rotates a list (the tail is moved to the head).


Write a method Node *Node::rRotate() which right-rotates a list (the tail is moved to the head).

Solution

void rotate_list_right(listnode** head, int k)
{
if( !head || !*head )
{
printf( \"\ rotate_list_right: empty list = so return \ \" );
return;
}
if( k < 1 )
{
printf( \"\ rotate_list_right:invalid input: k must be >= 1 \ \" );
return;
}

listnode* post = *head;
listnode* curr = *head;

/* move post by k nodes */
while(k--)
{
post = post->next;
if( !post ) /* k is bigger than length of the list */
{
printf( \"\ rotate_list_right:invalid input: k must be smaller than list size \ \" );
return;
}
}

/* move curr to kth-last node */
while(post->next)
{
curr = curr->next;
post = post->next;
}

/* currs\' next is new header */
listnode* tmp = *head;
*head = curr->next;
curr->next = 0;

//join post
post->next = tmp;
}

C++ Write a method Node *Node::rRotate() which right-rotates a list (the tail is moved to the head). C++ Write a method Node *Node::rRotate() which right-rotate

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site