this is a big fix, not a small fix

This commit is contained in:
ldlda authored and lda committed 2024-12-05 17:40:47 +07:00
1 parent ad3ce0bd48
commit efe7783b78
2 files changed
+95 -16

No files matched your search

+70 -16
View File
@@ -52,9 +52,13 @@ int adll_remove(dllda *whatif, nodeld *node)
{ {
// start = right // start = right
whatif->start = node->next; whatif->start = node->next;
if (whatif->start == whatif->end)
whatif->end = NULL; // ass font
// left of right now is null // left of right now is null
whatif->start->prev = NULL; if (whatif->start != NULL)
// eq to line below whatif->start->prev = NULL;
} }
// no right! // no right!
else if (node->next == NULL || node == whatif->end) // b else if (node->next == NULL || node == whatif->end) // b
@@ -62,8 +66,8 @@ int adll_remove(dllda *whatif, nodeld *node)
// end is now left // end is now left
whatif->end = node->prev; whatif->end = node->prev;
// right of left now is null // right of left now is null
whatif->end->next = NULL; if (whatif->end != NULL)
// eq to line below too like whatif->end->next = NULL;
} }
whatif->size--; whatif->size--;
@@ -231,6 +235,11 @@ int dllda_popleft(dllda *l, uintptr_t *puthere)
}; };
*puthere = startMayBeNull->ptr; *puthere = startMayBeNull->ptr;
adll_remove(l, startMayBeNull); adll_remove(l, startMayBeNull);
if (dllda_len(l) == 1)
{
if (l->start == NULL)
;
};
return 0; return 0;
} }
@@ -304,10 +313,10 @@ void printItOut(dllda *new)
printf("size: %d [", sz); printf("size: %d [", sz);
uintptr_t *c = (uintptr_t *)calloc(sz, sizeof(uintptr_t)); uintptr_t *c = (uintptr_t *)calloc(sz, sizeof(uintptr_t));
for (int i = sz - dllda_to_list(new, c); for (int i = sz - dllda_to_list(new, c);
printf("(%d: %d)", i, *(int *)c[i]) && (i < sz); // how to get value
(++i < sz); // how to get value i++,
printf(", ")) (i < sz) && printf(", "))
; printf("(%d: %d)", i, *(int *)c[i]);
printf("]\n"); printf("]\n");
} }
void traverse(dllda *l) void traverse(dllda *l)
@@ -348,7 +357,30 @@ int dllda_to_list(dllda *l, uintptr_t *arr)
return len; return len;
} }
int _main(int argc, char const *argv[]) int dllda_into_list(dllda *l, uintptr_t *arr)
{
int len = dllda_len(l);
for (int i = 0; i < len; i++)
{
uintptr_t *t = &(arr[i]);
dllda_popleft(l, t);
// printf("%d ", *(int*) *t);
}
return len;
}
int dllda_from_list(dllda *l, uintptr_t *arr, int size)
{
for (int i = 0; i < size; i++)
// printf("%p %d ", arr[i], *(int *)arr[i]);
dllda_push(l, arr[i]);
// printf("%d ", *(int*) *t);
return 0;
}
int main(int argc, char const *argv[])
{ {
dllda *new = initlda(); dllda *new = initlda();
int dsa = 17; int dsa = 17;
@@ -378,18 +410,40 @@ int _main(int argc, char const *argv[])
dllda_insert(new, 0, (uintptr_t)&item6); dllda_insert(new, 0, (uintptr_t)&item6);
// printItOut(new); // printItOut(new);
// traverse(new); // traverse(new);
uintptr_t this; uintptr_t this1, this2, this3;
int code; int code;
code = dllda_rem(new, 3, &this); code = dllda_rem(new, 3, &this1);
printf(" %d ", *(int *)this); printf(" %d ", *(int *)this1);
// printItOut(new); // printItOut(new);
code = dllda_popleft(new, &this); code = dllda_popleft(new, &this2);
printf(" %d ", *(int *)this); printf(" %d ", *(int *)this2);
// printItOut(new); // printItOut(new);
code = dllda_pop(new, &this); code = dllda_pop(new, &this3);
printf(" %d ", *(int *)this); printf(" %d ", *(int *)this3);
// printItOut(new); // printItOut(new);
// traverse(new); // traverse(new);
printf("\n");
int item7 = 11;
dllda_insert(new, 2, (uintptr_t)&item7);
int item8 = 42;
dllda_insert(new, 1, (uintptr_t)&item8);
int item9 = 16;
dllda_insert(new, 3, (uintptr_t)&item9);
dllda_push(new, this1);
dllda_push(new, this2);
dllda_pushleft(new, this3);
printItOut(new);
printf("\n");
int len = dllda_len(new);
uintptr_t *list = calloc(len, sizeof(uintptr_t));
dllda_into_list(new, list);
for (int i = 0; i < len; i++)
printf("%d ", *(int *)list[i]);
printf("\n");
printItOut(new);
printf("a\n");
dllda_from_list(new, list, len);
printItOut(new);
return 0; return 0;
} }
+25
View File
@@ -0,0 +1,25 @@
// feed in a pointer to a function that takes 2 and returns (just like the )
#include <stdlib.h>
#include <stdio.h>
int cmpfunc(const void *a, const void *b)
{
const int *A = a, *B = b;
return (*A > *B) - (*A < *B);
}
int main(int argc, char const *argv[])
{
int a[13] = {1, 2, 3, 7, 3, 9, 11, 13, 42, -1, 4, 52, 10};
qsort(a, 13, sizeof(int), cmpfunc);
for (int i = 0; i < 13; i++)
printf("%d ", a[i]);
printf("\n");
int at = 5;
int *here = (int *)bsearch(&at, a, 13, sizeof(int), cmpfunc);
if (here == NULL)
printf("no %d ", at);
else
printf("is %d ", *here);
return 0;
}