-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_functions.c
More file actions
142 lines (131 loc) · 3.07 KB
/
push_functions.c
File metadata and controls
142 lines (131 loc) · 3.07 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* ************************************************************************** */
/* */
/* :::::::: */
/* push_functions.c :+: :+: */
/* +:+ */
/* By: dkramer <dkramer@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/12/14 16:30:01 by dkramer #+# #+# */
/* Updated: 2021/12/14 16:31:02 by dkramer ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft/ft_printf/libftprintf.h"
#include "libft/libft.h"
#include <stdlib.h>
#include "push_swap.h"
/*shift evrything one down the stack*/
int *moveback(int *stack, int len, t_str *str)
{
int i;
int saveint;
int save2;
int *save;
save = malloc(sizeof(int) * (len));
if (!save)
return (returnft(str));
i = 0;
while (i < (len - 1))
i++;
if (i > 0)
{
saveint = len - 1;
save2 = stack[saveint - 1];
while (i > 0)
{
save[i] = stack[i - 1];
i--;
}
save[saveint] = save2;
save[0] = 0;
}
free (stack);
return (save);
}
int *pushanonmes(int *stack1, int **stack2, t_str *str)
{
int *save;
save = 0;
if (!*stack2)
{
*stack2 = malloc(sizeof(int) * (str->lena));
if (!*stack2)
return (returnft(str));
}
if (str->lena > 1)
{
*stack2 = moveback(*stack2, str->lena, str);
if (!*stack2)
return (0);
}
*stack2[0] = stack1[0];
if (str->lenb >= 1)
{
save = malloc(sizeof(int) * (str->lenb));
if (!save)
return (returnft(str));
movefront(save, stack1, str->lenb);
}
free (stack1);
return (save);
}
int *pushbnonmes(int *stack1, int **stack2, t_str *str)
{
int *save;
save = 0;
if (!*stack2)
{
*stack2 = malloc(sizeof(int) * (str->lenb));
if (!*stack2)
return (returnft(str));
}
if (str->lenb > 1)
{
*stack2 = moveback(*stack2, str->lenb, str);
if (!*stack2)
return (0);
}
*stack2[0] = stack1[0];
if (str->lena >= 1)
{
save = malloc(sizeof(int) * (str->lena));
if (!save)
return (returnft(str));
movefront(save, stack1, str->lena);
}
free (stack1);
return (save);
}
/*take the first element at the top of b and put it at the top of a. Do
nothing if b is empty*/
int *pusha(int *stackb, int **stacka, t_str *str)
{
int *save;
save = 0;
if (stackb)
{
str->lenb--;
str->lena++;
save = pushanonmes(stackb, stacka, str);
if (str->error == 1)
return (0);
ft_printf("pa\n");
}
return (save);
}
/*take the first element at the top of a and put it at the top of b. Do
nothing if a is empty*/
int *pushb(int *stacka, int **stackb, t_str *str)
{
int *save;
save = 0;
if (stacka)
{
str->lena--;
str->lenb++;
save = pushbnonmes(stacka, stackb, str);
if (str->error == 1)
return (0);
ft_printf("pb\n");
}
return (save);
}