-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_function2.c
executable file
·59 lines (48 loc) · 1.28 KB
/
stack_function2.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
52
53
54
55
56
57
58
59
#include "monty.h"
/**
* _rotl - Rotate the stack to the left.
* @stack: Pointer to the stack
* @line_number: Line number where the opcode occurs
*/
void _rotl(stack_t **stack, unsigned int line_number)
{
stack_t *runner = *stack;
int aux1 = 0;
if (!line_number || !stack || !*stack || !(*stack)->next)
return;
/* Save the value of the top element */
aux1 = runner->n;
/*Move each element to the left, starting from the second element */
while (runner->next)
{
runner = runner->next;
runner->prev->n = runner->n;
}
/* Set the value of the last element to the saved top value */
runner->n = aux1;
}
/**
* _rotr - Rotate the stack to the right.
* @stack: Pointer to the stack
* @line_number: Line number where the opcode occurs
*/
void _rotr(stack_t **stack, unsigned int line_number)
{
stack_t *runner = *stack;
int aux1 = 0;
if (!line_number || !stack || !*stack || !(*stack)->next)
return;
/* Find the last element */
while (runner->next)
runner = runner->next;
/* Save the value of the last element */
aux1 = runner->n;
/* Move each element to the right, starting from the last element */
while (runner->prev)
{
runner = runner->prev;
runner->next->n = runner->n;
}
/* Set the value of the first element to the saved last value */
runner->n = aux1;
}