-
Notifications
You must be signed in to change notification settings - Fork 0
/
_rotl.c
35 lines (31 loc) · 1.04 KB
/
_rotl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "monty.h"
/**
* _rotl - rotates the first element of the stack
* @stack: stack head
* @line_count: line count
*
* Return: void
* This task requires you to implement the "rotl" opcode
* in the Monty programming language. The "rotl" opcode rotates
* the stack to the top, where the top element becomes the last
* one, and the second top element becomes the first one. You need
* to write a function called "rotl" that takes a stack pointer and
* the line count as parameters. The function should rotate the
* stack according to the specified behavior.
*/
void _rotl(stack_t **stack, unsigned int line_count)
{
stack_t *left;
stack_t *right;
(void) line_count;
if (!stack || !*stack || !(*stack)->next)
return;
left = right = *stack;
while (right->next) /* move the right pointer to the last node */
right = right->next;
right->next = left; /* a circle infinite linked list loop */
left->prev = right;
*stack = left->next; /* so we cut the link between the 0 and 1 element */
(*stack)->prev->next = NULL;
(*stack)->prev = NULL;
}