-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrot_opcodes.c
51 lines (45 loc) · 1.45 KB
/
rot_opcodes.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
48
49
50
51
#include "monty.h"
/**
* rotl - rotates the stack to the top
* @stack: a pointer to the stack or queue
* @line_number: the index of the current command
*
* Note: The top element of the stack becomes the last one, and the second top
* element of the stack becomes the first one. This function never fails
*/
void rotl(stack_t **stack, __attribute__((unused)) unsigned int line_number)
{
stack_t *tmp;
if (size(monty_list) < 2 || size(monty_list) == 0)
return; /* there's not enough nodes to rotate */
/* keep the address of the now old head node and move to next node */
tmp = *stack;
*stack = (*stack)->next;
(*stack)->prev = NULL;
monty_list.head = *stack;
/* adjust links to insert at the end of the list */
tmp->next = NULL;
monty_list.tail->next = tmp;
tmp->prev = monty_list.tail;
monty_list.tail = tmp;
}
/**
* rotr - rotates the stack to the bottom.
* @stack: a pointer to the stack or queue
* @line_number: the index of the current command
*
* Note: The last element of the stack becomes the top element of the stack.
* This function never fails
*/
void rotr(stack_t **stack, __attribute__((unused)) unsigned int line_number)
{
if (size(monty_list) < 2 || size(monty_list) == 0)
return;
monty_list.tail->next = monty_list.head;
monty_list.head->prev = monty_list.tail;
monty_list.head = monty_list.tail;
monty_list.tail = monty_list.tail->prev;
monty_list.tail->next = NULL;
monty_list.head->prev = NULL;
*stack = monty_list.head;
}