-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate.c
47 lines (40 loc) · 951 Bytes
/
rotate.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
36
37
38
39
40
41
42
43
44
45
46
47
#include "monty.h"
/**
* rotl_op - brings the top of the stack to the bottom
* @list: Head of the stack/queue
* @line_num: Line number of the instruction
*/
void rotl_op(stack_t **list, unsigned int line_num)
{
stack_t *move = *list;
if ((*list) == NULL || (*list)->next == NULL || !line_num)
return;
while (move->next != NULL)
move = move->next;
move->next = (*list);
*list = (*list)->next;
(*list)->prev->next = NULL;
(*list)->prev->prev = move;
(*list)->prev = NULL;
}
/**
* rotr_op - brings the bottom of the stack to the top
* @list: Head of the stack/queue
* @line_num: Line number of the instruction
*/
void rotr_op(stack_t **list, unsigned int line_num)
{
stack_t *node = *list;
int node_n, prev_n;
if (node == NULL || node->next == NULL)
return;
node_n = prev_n = node->n;
while (node && line_num)
{
node_n = node->n;
node->n = prev_n;
node = node->next;
prev_n = node_n;
}
(*list)->n = node_n;
}