-
Notifications
You must be signed in to change notification settings - Fork 0
/
NOTES.txt
368 lines (321 loc) · 8.11 KB
/
NOTES.txt
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
push_swap Log
/*------------------BINARY_SEARCH----------------*/
int get_index(int number, int *n, int size, int len)
/*Binary search
int len very stupid idea
Not working at all times*/
{
i = size / 2;
// if (i <= 0)
// return (0);
if (i >= len)
return (len - 1);
if (number != n[i])
{
if (number == n[i + 1])
return (i + 1);
else if (number == n[i - 1])
return (i - 1);
else if (number < n[i])
return (get_index(number, n, i, len));
else
return (get_index(number, n, size + (i + 1), len));
}
return (i);
}
/*-------------NOT_WORKING_PROPERLY_THOUGH-------------*/
if (a->size > SHORT_LST && a.neg) <----- Attributing indexes only in case there's negatives ........
Not to execute if no negatives on list; CREATE NEG FLAG ON T_STACK
/*-------USEFUL_FOR_VISUALIZING-----------*/
#include <stdio.h>
printf("stack a:\n");
printlst(&a, 0);
printlst(&a, 'i');
void printlst(t_stack *s, char arg)
{
t_dlist *tmp;
if (arg == 'r')
{
tmp = s->last;
printf("REVERSE\n");
}
else
tmp = s->head;
printf("--------\n");
if (!tmp)
{
printf("Empty!\n");
return ;
}
while (tmp)
{
if (arg == 'i')
printf("(index)%d\n", tmp->index);
else
printf("%d\n", tmp->n);
if (arg == 'r')
tmp = tmp->prev;
else
tmp = tmp->next;
}
printf("--------\n");
}
/*------------------------------------------------*/
pa = push(b, a) pb = push(a, b)
push -> if no src abort <------------------------------
rotate -> if no src abort <------------------------------
/*DEREFERENCE elements using *(int *)a.head->content */
*(int *)ft_lstlast(a->head)->content WORKS!!
Doubly Linked Lists w/ int n -> need remake lstlast lstnew lstadd_front lstclear lstdelone
--------------------------------------------------------------------------------
SEE 2nd_impl/ for implementation w/ doubly-linked-list and double-stack-struct (w/ int as value)
set doubly linked lists on libft.h and push_swap.h without thinking...still
using libft functions as well........................
lstiter might actually be useful
--------------------------------------------------------------------------------------------
ARG=`ruby -e "puts (0..99).to_a.shuffle.join(' ')"`
----------------->(need create an sh tester script)<-----------------------
ARRAY IMPLEMENTATION OF STACKS:
typedef struct s_stack
{
int stack[CAPACITY];
int top;
} t_stack;
LINKED LIST IMPLEMENTATION OF STACK:
typedef struct s_stack
{
t_list *head;
ssize_t size;
} t_stack;
GETTING ARGUMENTS INTO A LINKED LIST USING LIBFT(original):
int main(int argc, char **argv)
{
int n;
t_list *a;
t_list *new;
int i;
a = ft_lstnew(argv[1]);
i = 1;
while (++i < argc)
ft_lstadd_back(&a, ft_lstnew(argv[i]));
new = a;
while (new->next) //prints list contents
{
n = ft_atoi((char *)new->content); //casts to str and converts to int
ft_putnbr_fd(n, 1);
ft_putchar_fd('\n', 1);
new = new->next;
}
n = ft_atoi((char *)new->content);
ft_putnbr_fd(n, 1);
ft_putchar_fd('\n', 1);
ft_putnbr_fd(ft_lstsize(a), 1);
// ft_lstclear(&a, (free)(void *)); <----------- Not sure yet how to do this
return (0);
}
^ Maybe not the best idea (?)^
POP:
t_list pop(t_stack *s)
/*
* Deletes 1st node on stack pointed to by s->head
* Returns same node*/
{
t_list *node;
node = &(s->head); /*node points to 1st element of stack*/
node->next = NULL; /*isolates element*/
s->head = s->head->next; /*stack head points to next element*/
s->size--; /*size "decrement"*/
return (*node); /*return element pointed to by "node"*/
}
PUSH:
void push(t_list *node, t_stack *s)
/*
*Add node to list pointed by stack's head
increment stack size
does not pop from previous t_list, ...*/
{
ft_lstadd_back(&(s->head), ft_lstnew(node));
s->size++;
}
/*------UNUSED FUNCTIONS BECAUSE I RAN OUT OF TIME--------*/
#include "inc/push_swap.h"
static int ft_rotate(t_stack *a, int rot, char *dir)
/*rotates a rot times in dir direction
does nothing and returns 1 if rot is -1
only working for stack a atm; not very versatile*/
{
if (rot == -1)
return (1);
while (rot--)
exec(dir, a, 0);
return (0);
}
static int find_closest(t_stack *a, int i)
/*finds looked for element closest to extremes(top/bottom) and brings it to top
returns -1 if none found*/
{
t_stack top;
t_stack bottom;
if (!a->size)
return (0);
top.head = a->head;
top.size = 0;
bottom.size = 0;
while (top.head && top.size++ < (a->size / 2))
{
if (!((top.head->n >> i) & 1))
break ;
top.head = top.head->next;
}
bottom.head = lstlast(top.head);
while (bottom.head && bottom.size <= (a->size / 2))
{
bottom.size++;
if (!((bottom.head->n >> i) & 1) && bottom.size < top.size)
return (ft_rotate(a, bottom.size, "rra"));
bottom.head = bottom.head->prev;
}
if (top.size > (a->size / 2) && ((top.head->n >> i) & 1))
top.size = 0;
/*if tmp.size > i: tmp.size = i*/
// printf("\ntop = %d\tbottom = %d\t", top.size, bottom.size);
return (ft_rotate(a, --top.size, "ra"));
/*return (ft_rotate(a, top, bottom));*/
}
/* ^^^ ------------------------------------------------------------------------------ ^^^
maybe pass a copy of a->head to save a line (t_dlist a, int condition)
maybe return absolute index number instead of relative to top/bottom (0 - a.size)
... or call a rotating function instead
...or pass char rot == "ra" or "raa" depending on value returned
^^^ ------------------------------------------------------------------------------ ^^^ */
t_dlist pop(t_stack *s)
/*
* Deletes 1st node on stack pointed to by s->head
* Returns same node*/
{
t_dlist *node;
node = s->head; /*node points to 1st element of stack*/
node->next = NULL; /*isolates element*/
s->head = s->head->next; /*stack head points to next element*/
s->size--; /*size "decrement"*/
return (*node); /*return element pointed to by "node"*/
}
static int chunk_sort(t_stack *s, int *i, int *j, int min, int max)
/*NOT WORKING*/
{
int tmp;
while ((*i)++ != (int)(s->size / 2) && s->head)
{
if (s->head->n >= min && s->head->n < max)
break ;
s->head = s->head->next;
}
tmp = *i;
while (tmp++ != s->size)
s->head = s->head->next;
tmp = s->size / 2;
*j = 0;
while (s->head)
{
if (s->head->n >= min && s->head->n < max)
*j = tmp;
s->head = s->head->next;
tmp++;
}
if (*i == (int)(s->size / 2) && *j == 0)
return(0);
else
return (1);
}
void quick_sort(t_stack *a, t_stack *b)
/*NOT WORKING*/
{
int first;
int second;
int range;
int i;
i = 0;
range = a->size / 5;
while (i++ < 5)
{
while (chunk_sort(a, &first, &second, range - (range * i), range * i))
{
if (first <= second)
{
while (first--)
exec("ra", a, 0);
}
else
{
while (second--)
exec("rra", a, 0);
}
exec("pb", a, b);
}
}
printf("stack b:\n");
printlst(b, 0);
printf("stack b:\n");
free(b);
}
static t_dlist *get_middle(t_stack a)
/*finds midpoint in stack if odd or end of upper half if even
not really useful atm*/
{
t_dlist *tmp;
int i;
i = 0;
tmp = a.head;
while (i++ < (a.size / 2))
tmp = tmp->next;
return (tmp);
}
static void merge(int *a, int l, int h, int m) //no actual need for m here
{
int left[(m - l)]; //not permitted
int right[(h - m)]; //not permitted
t_indexes id;
id.llen = m - l;
id.rlen = h - m;
copy(a, left, right, &id);
while (id.l < id.llen && id.r < id.rlen)
{
if (left[id.l] < right[id.r])
a[l++] = left[id.l++];
else
a[l++] = right[id.r++];
}
while (id.l < id.llen)
a[l++] = left[id.l++];
while (id.r < id.rlen)
a[l++] = right[id.r++];
}
void merge_sort(int *a, int l, int h) //Not sure copy would work with recursion;
{
int m;
m = h / 2; // middle
if (h > 1) //base case
{
merge_sort(a, l, m); //DIVIDE LEFT SIDE
merge_sort(a + m, m, h - 1); //DIVIDE RIGHT SIDE
merge(a, l, h, m); //pass tmp[] instead of m maybe
}
// return (copy); //MERGE BOTH SIDES (malloc and return results or return a)
}
/*Dealing with $ARG (because zsh)*/
if (argc == 2)
{
a->size = 0;
arg = ft_split(argv[1], ' ');
if (!arg)
display_err();
while (arg[a->size])
{
n[a->size] = _atoi(arg[a->size]);
free(arg[a->size]);
a->size++;
}
// ft_free_arr_str(arg);
free(arg);
}
else