-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_utils.c
122 lines (111 loc) · 2.7 KB
/
functions_utils.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* functions_part2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eablak <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/19 19:05:25 by eablak #+# #+# */
/* Updated: 2022/12/20 16:12:19 by eablak ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void ra(t_struct **a)
{
t_struct *begin;
t_struct *new_begin;
t_struct *new_keep;
t_struct *x;
x = malloc(sizeof(t_struct));
begin = *a;
new_begin = begin->next;
x->data = begin->data;
x->next = NULL;
begin->next = NULL;
new_keep = new_begin;
while (new_begin->next != NULL)
new_begin = new_begin->next;
new_begin->next = x;
free(begin);
(*a) = new_keep;
write(1, "ra\n", 3);
}
void rb(t_struct **b)
{
t_struct *begin;
t_struct *new_begin;
t_struct *new_keep;
t_struct *x;
x = malloc(sizeof(t_struct));
begin = *b;
new_begin = begin->next;
x->data = begin->data;
x->next = NULL;
begin->next = NULL;
free(begin);
new_keep = new_begin;
while (new_begin->next)
new_begin = new_begin->next;
new_begin->next = x;
(*b) = new_keep;
write(1, "rb\n", 3);
}
t_struct *sa(t_struct **a)
{
t_struct *begin;
int temp;
begin = (*a);
temp = (*a)->data;
(*a)->data = (*a)->next->data;
(*a)->next->data = temp;
write(1, "sa\n", 3);
return (begin);
}
void pa(t_holder *holder)
{
t_struct *a;
t_struct *b;
t_struct *keep_b;
t_struct *nnew;
nnew = malloc(sizeof(t_struct));
a = holder->a;
b = (t_struct *)holder->b;
if (b == NULL)
return ;
keep_b = b->next;
nnew->data = b->data;
b->next = NULL;
free(b);
holder->b = keep_b;
nnew->next = a;
holder->a = nnew;
holder->size_a++;
holder->size_b--;
write(1, "pa\n", 3);
}
void pb(t_holder *holder)
{
t_struct *keep_a;
t_struct *nnew;
nnew = malloc(sizeof(t_struct));
if (holder->a == NULL)
return ;
keep_a = holder->a->next;
nnew->data = holder->a->data;
nnew->next = NULL;
free(holder->a);
holder->a = keep_a;
if (holder->b == NULL)
holder->b = nnew;
else
{
nnew->next = holder->b;
holder->b = nnew;
}
holder->size_a--;
holder->size_b++;
write(1, "pb\n", 3);
}