-
Notifications
You must be signed in to change notification settings - Fork 0
/
elementary_operations.c
90 lines (81 loc) · 2.64 KB
/
elementary_operations.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
81
82
83
84
85
86
87
88
89
90
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* elementary_operations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pgober <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 10:28:33 by pgober #+# #+# */
/* Updated: 2023/11/05 10:32:16 by pgober ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void sab(t_stack **head, char ab, bool print) // swap the first two elements at the top of stack a or b
{
t_stack *tmp;
if (!(*head && (*head)->next))
return ;
tmp = (*head)->data;
(*head)->data = ((*head)->next)->data;
((*head)->next)->data = tmp;
if (ab == 'a' && print == true)
write(1, "sa\n", 3);
else if (print == true)
write(1, "sb\n", 3);
}
void pab(t_stack **head_from, t_stack **head_to, char ab, bool print) // push to stack a or b
{
t_stack *new_node;
t_stack *new_head;
if (!(*head_from))
return ;
new_node = (t_stack *)malloc(sizeof(t_stack));
if (!new_node)
return ;
new_node->data = malloc(sizeof(int));
if (!(new_node->data))
return (free(new_node));
*((int *)(new_node->data)) = *((int *)((*head_from)->data));
new_node->next = *head_to;
*head_to = new_node;
new_head = (*head_from)->next;
free((*head_from)->data);
free(*head_from);
*head_from = new_head;
if (ab == 'a' && print == true)
write(1, "pa\n", 3);
else if (print == true)
write(1, "pb\n", 3);
}
void rab(t_stack **head, char ab, bool print) // rotate stack a or b
{
t_stack *new_head;
t_stack *old_head;
old_head = *head;
new_head = (*head)->next;
while ((*head)->next)
(*head) = (*head)->next;
(*head)->next = old_head;
old_head->next = NULL;
*head = new_head;
if (ab == 'a' && print == true)
write(1, "ra\n", 3);
else if (print == true)
write(1, "rb\n", 3);
}
void rrab(t_stack **head, char ab, bool print) // reverse rotate stack a or b
{
t_stack *old_head;
t_stack *tmp;
old_head = *head;
while (((*head)->next)->next)
(*head) = (*head)->next;
tmp = (*head)->next;
(*head)->next = NULL;
*head = tmp;
(*head)->next = old_head;
if (ab == 'a' && print == true)
write(1, "rra\n", 4);
else if (print == true)
write(1, "rrb\n", 4);
}