-
Notifications
You must be signed in to change notification settings - Fork 0
/
monty_funcs_4.c
80 lines (67 loc) · 2.09 KB
/
monty_funcs_4.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "monty.h"
void monty_rotl(stack_t **stack, unsigned int line_number);
void monty_rotr(stack_t **stack, unsigned int line_number);
void monty_stack(stack_t **stack, unsigned int line_number);
void monty_queue(stack_t **stack, unsigned int line_number);
/**
* monty_rotl - Rotates the top value of a stack_t linked list to the bottom.
* @stack: A pointer to the top mode node of a stack_t linked list.
* @line_number: The current working line number of a Monty bytecodes file.
*/
void monty_rotl(stack_t **stack, unsigned int line_number)
{
stack_t *top, *bottom;
if ((*stack)->next == NULL || (*stack)->next->next == NULL)
return;
top = (*stack)->next;
bottom = (*stack)->next;
while (bottom->next != NULL)
bottom = bottom->next;
top->next->prev = *stack;
(*stack)->next = top->next;
bottom->next = top;
top->next = NULL;
top->prev = bottom;
(void)line_number;
}
/**
* monty_rotr - Rotates the bottom value of a stack_t linked list to the top.
* @stack: A pointer to the top mode node of a stack_t linked list.
* @line_number: The current working line number of a Monty bytecodes file.
*/
void monty_rotr(stack_t **stack, unsigned int line_number)
{
stack_t *top, *bottom;
if ((*stack)->next == NULL || (*stack)->next->next == NULL)
return;
top = (*stack)->next;
bottom = (*stack)->next;
while (bottom->next != NULL)
bottom = bottom->next;
bottom->prev->next = NULL;
(*stack)->next = bottom;
bottom->prev = *stack;
bottom->next = top;
top->prev = bottom;
(void)line_number;
}
/**
* monty_stack - Converts a queue to a stack.
* @stack: A pointer to the top mode node of a stack_t linked list.
* @line_number: The current working line number of a Monty bytecodes file.
*/
void monty_stack(stack_t **stack, unsigned int line_number)
{
(*stack)->n = STACK;
(void)line_number;
}
/**
* monty_queue - Converts a stack to a queue.
* @stack: A pointer to the top mode node of a stack_t linked list.
* @line_number: The current working line number of a Monty bytecodes file.
*/
void monty_queue(stack_t **stack, unsigned int line_number)
{
(*stack)->n = QUEUE;
(void)line_number;
}