This commit is contained in:
ldlda committed 2024-10-22 05:07:50 +07:00
1 parent 045d7367a8
commit 8aa73bde5e
10 files changed
+649 -47

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
.vscode
exe
*.exe
drafts
+13 -3
View File
@@ -6,7 +6,7 @@ typedef struct
int imag;
} ldaComplex;
void print(ldaComplex* lda)
void print(ldaComplex *lda)
{
printf("%d + %di", lda->real, lda->imag);
}
@@ -30,8 +30,18 @@ int main(int argc, char const *argv[])
ldaComplex addRes = add(lda, lda2);
ldaComplex multiRes = multiply(lda, lda2);
printf("("); print(&lda); printf(") + ("); print(&lda2); printf(") = "); print(&addRes);
printf("\n("); print(&lda); printf(") * ("); print(&lda2); printf(") = "); print(&multiRes);
printf("(");
print(&lda);
printf(") + (");
print(&lda2);
printf(") = ");
print(&addRes);
printf("\n(");
print(&lda);
printf(") * (");
print(&lda2);
printf(") = ");
print(&multiRes);
return 0;
}
// ass
+7 -4
View File
@@ -1,10 +1,13 @@
void findMax(int *max, int a) {
if (a > *max) *max = a;
void findMax(int *max, int a)
{
if (a > *max)
*max = a;
};
#include <stdio.h>
int main(){
int c = 176;
int main()
{
int c = 176;
int f = 10;
findMax(&f, c);
printf("%d, %d", c, f);
+4 -4
View File
@@ -10,13 +10,13 @@ int log10intint(int n)
{
a *= 10;
if (n - a < 0)
{
return i;
}
}
};
int ten_to_the(int i)
{ if (i < 0) return 0;
{
if (i < 0)
return 0;
int ret = 1;
while (i-- > 0)
ret *= 10;
@@ -37,7 +37,7 @@ int special_swap(int number)
int main()
{
printf("%d\n", special_swap(142425));
return 0;
}
+109
View File
@@ -0,0 +1,109 @@
#include "../common/linkedlist.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h> // reimport!!! as
#include <stdbool.h>
typedef struct CarInfo
{
int capacity;
char *id; // i follow the thing
// (0, cap]
int reserved;
} CarInfo;
typedef struct Train
{
dllda Cars;
int passengers;
} Train;
// rust is perfect for this let me tell you
CarInfo *init(int cap, char *id)
{
CarInfo *c = (CarInfo *)calloc(1, sizeof(CarInfo));
if (cap > 0)
c->capacity = cap;
c->id = strdup(id);
return c;
}
int check(CarInfo *c)
{
if (c->reserved <= 0)
return -1;
else if (c->reserved > c->capacity)
return 1;
return 0;
}
int getPassengers(CarInfo *c) { return c->reserved; }
int getCapacity(CarInfo *c) { return c->capacity; }
int printTheCarThing(CarInfo *c)
{
return printf("CarInfo (id: %s): %d/%d", c->id, c->reserved, c->capacity);
}
void printhere(Train *train)
{
dllda *new = &train->Cars;
int sz = dllda_len(new);
printf("size: %d [", sz);
uintptr_t *c = (uintptr_t *) calloc(sz, sizeof(uintptr_t));
for (int i = sz - dllda_to_list(new, c);
printf("(%d: ", i) &&
printTheCarThing((CarInfo *)c[i]) &&
printf(")") &&
(++i < sz); // how to get value
printf(", "))
;
printf("]\n");
}
// HUGE
int addPassenger(Train *train, int p)
{ // +- supported
if (p == 0)
return 0; // hey what if we return the excess
train->passengers += p;
dllda *cars = &train->Cars;
int len = dllda_len(cars);
// so should this be a fifo or a what
int flag = 1;
int startPoint = 0, endIncl = len - 1;
if (p < 0)
{
flag = -1;
startPoint = endIncl, endIncl = 0;
} // its find all over again
// so if -4 is smaller than -2 and 4 is smaller than 23
for (int i = startPoint; flag * i <= flag * endIncl; i += flag)
{ CarInfo *curcar;
dllda_query(cars, i, (uintptr_t*) curcar);
if (true);
};
return 0;
}
int main(int argc, char const *argv[])
{
Train train;
train.passengers = 0;
dllda *Cars = &train.Cars;
Cars = initlda();
CarInfo *Car1 = init(55, "gay101");
dllda_push(Cars, (uintptr_t)Car1);
CarInfo *Car2 = init(73, "top3");
dllda_pushleft(Cars, (uintptr_t)Car2);
CarInfo *Car3 = init(25, "r1chk1dz");
dllda_pushleft(Cars, (uintptr_t)Car3);
CarInfo *Car4 = init(14, "prez");
dllda_insert(Cars, 2, (uintptr_t)Car4);
uintptr_t *array = (uintptr_t *)malloc(dllda_len(Cars) * sizeof(CarInfo));
dllda_to_list(Cars, array);
printhere(&train);
return 0;
}
+52 -16
View File
@@ -1,22 +1,58 @@
#include "../common/arraylist.h"
// haha i will have to compile manually EVERY TIME HAHAHAHAHAHAHAHA
#include <stdio.h>
ldaArrayList number_to_list(long long int e)
{
ldaArrayList c;
ldalistinit(&c);
if (e < 0)
{
ldalistpush(&c, -1);
e = -e;
}
else
ldalistpush(&c, 1);
for (; e > 0; e /= 10)
ldalistadd(&c, 1, (int) (e % 10));
// SHIT IN (array rotato (it means bad))
ldalistprint(&c);
return c;
}
void displayNumber(ldaArrayList *n)
{
for (int i = 0, cur; i < ldalistlen(n); i++)
{
if (ldalistlookup(n, i, &cur) != 0)
return;
if (cur == -1)
printf("-");
else
printf("%d", cur);
}
}
long long int list_to_number(ldaArrayList c)
{
// SHIT OUT
long long ret = 0;
int e;
for (long long int f = 1; ldalistpop(&c, &e) == 0; f *= 10)
{
if (e >= 0)
ret = ret + e * f;
else if (e == -1)
ret = -ret;
}
return ret;
}
int main(int argc, char const *argv[])
{
ldaArrayList lda;
ldalistinit(&lda);
ldalistpush(&lda, 1);
ldalistpush(&lda, 2);
ldalistpush(&lda, 5);
ldalistpush(&lda, 10);
ldalistpush(&lda, 20);
ldalistpush(&lda, 30);
ldalistadd(&lda, 4, 50); // correct af
ldalistprint(&lda);
int g;
ldalistpop(&lda, &g);
int f;
ldalistremove(&lda, 6, &f);
ldalistprint(&lda);
printf("%d %d",g, f);
ldaArrayList ef = number_to_list(-276482364786342);
displayNumber(&ef);
printf("\n");
printf(" %lld ", list_to_number(ef));
return 0;
}
+27 -18
View File
@@ -1,36 +1,43 @@
// for printf
#include <stdio.h>
#include <stdlib.h>
#include "arraylist.h"
int CAPACITY = 24, ADDITIONAL = 16; //id rather just <<=1
// stack list
void ldalistinit(ldaArrayList *l)
{
l->size = 0;
l->capacity = CAPACITY;
l->list = (int *)calloc(l->capacity, sizeof(int));
}
int ldalistlen(ldaArrayList *l)
{
return l->size;
}
int ldalistcheck(ldaArrayList *l)
int ldalistMakeRoom(ldaArrayList *l)
{
if (ldalistlen(l) >= 100)
return -1;
if (l->size >= l->capacity)
{
l->capacity += ADDITIONAL;
l->list = (int *)realloc(l->list, l->capacity * sizeof(int));
return 1;
}
return 0;
}
int ldalistpush(ldaArrayList *l, int value)
{
if (ldalistcheck(l) != 0)
return -1;
// raw dogging
ldalistMakeRoom(l);
// write first then increase
l->list[l->size++] = value;
return 0;
}
int ldalistadd(ldaArrayList *l, int at, int value)
{
if (ldalistcheck(l) != 0)
return -1;
ldalistMakeRoom(l);
int size = ldalistlen(l);
if (at > size)
return -1;
@@ -52,9 +59,10 @@ int ldalistadd(ldaArrayList *l, int at, int value)
int ldalistpop(ldaArrayList *l, int *put_here)
{
if (ldalistlen(l) <= 0)
if (ldalistlen(l) <= 0) // how would u like
return -1;
*put_here = l->list[l->size--];
*put_here = l->list[--l->size];
// decrease to then take out
return 0;
}
@@ -88,15 +96,18 @@ int ldalistlookup(ldaArrayList *l, int at, int *copy_here)
*copy_here = l->list[at];
return 0;
}
void ldalistprint(ldaArrayList *l)
{
printf("Array with size %d (capacity %d): ", l->size, l->capacity);
printf("[");
for (int i = 0,
cur,
_ = ldalistlookup(l, i, &cur) || printf("%d", cur);
++i < ldalistlen(l) && printf(", ");
_ = ldalistlookup(l, i, &cur) || printf("%d", cur))
for (int i = 0, cur;
ldalistlookup(l, i, &cur) == 0 &&
printf("%d", cur) != 0 &&
(++i < ldalistlen(l));
printf(","))
;
printf("]");
};
@@ -105,5 +116,3 @@ void ldalistprint(ldaArrayList *l)
// /* code */
// return 0;
// }
+3 -2
View File
@@ -1,13 +1,14 @@
// for printf
// stack list
#ifndef COMMON_LDAARRAYLIST_H
#define COMMON_LDAARRAYLIST_H
typedef struct LdaArrayList
{
int size;
int list[100];
int capacity; // size big then capacity big 2
int* list;
} ldaArrayList;
void ldalistinit(ldaArrayList *l);
+391
View File
@@ -0,0 +1,391 @@
#include "linkedlist.h"
#include <stdio.h>
// im not good at c!!!!! i do not use the Potential it has
// BE INCREDIBLY FR WITH NODEPTR
// am i doing it right
// removehidden
dllda *initlda()
{
return (dllda *)calloc(1, sizeof(dllda));
}
nodeld *nodeinit(uintptr_t ptr)
{
nodeld *new = (nodeld *)calloc(1, sizeof(nodeld));
// printf("%p ", new);
new->ptr = ptr;
return new;
}
// int dllda_init(dllda *l, uintptr_t firstValue)
// {
// l->size = 1;
// nodeld start;
// start.ptr = firstValue;
// l->start = &start;
// start.prev = NULL, start.next = NULL;
// printf("%p,%p,%p,%d,%p,%p", l, &start, start.ptr, *(int *)start.ptr, start.prev, start.next);
// return 0;
// }
// auto size--
int adll_remove(dllda *whatif, nodeld *node)
{
if (whatif->size <= 0)
{
whatif->size = 0;
return -1;
}
if (!(node->prev == NULL || node == whatif->start) &&
!(node->next == NULL || node == whatif->end)) // not a not b
{
// left of right now is left
node->next->prev = node->prev;
// right of left now is right
node->prev->next = node->next;
}
// no left.
else if (node->prev == NULL || node == whatif->start) // a
{
// start = right
whatif->start = node->next;
// left of right now is null
whatif->start->prev = NULL;
// eq to line below
}
// no right!
else if (node->next == NULL || node == whatif->end) // b
{
// end is now left
whatif->end = node->prev;
// right of left now is null
whatif->end->next = NULL;
// eq to line below too like
}
whatif->size--;
free(node);
// free(node);
return 0;
// how do i either know the position or know the
}
int structure_fixups(dllda *l)
{
nodeld *start = l->start, *end = l->end;
if (!(start || end))
{
printf("\nnothing\n");
l->size = 0;
return 0;
}
else if (start == NULL)
{
start = end;
int count = 1;
while (start->prev != NULL)
{
start = start->prev;
count++;
}
if (count > 1)
l->start = start;
l->size = count;
return 2;
}
else if (end == NULL)
{
end = start;
int count = 1;
while (end->next != NULL)
{
end = end->next;
count++;
}
if (count > 1)
l->end = end;
l->size = count;
return 1;
}
else
return 3;
{
int cfl = 1, cfr = 1;
while (end->prev || start->next)
{
if (end->prev && start->next == 0)
{
printf("%p, %p", end->prev, start->next);
printf("%d, %d", cfl, cfr);
};
{
end = end->prev;
cfr++;
}
{
start = start->next;
cfl++;
}
}
if (cfl == cfr)
l->size = cfl;
};
}
void dllda_pushleft(dllda *c, uintptr_t ptr)
{
nodeld *new = nodeinit(ptr);
structure_fixups(c);
if (c->start != NULL)
{
new->next = c->start;
new->next->prev = new;
}
c->start = new;
c->size++;
}
void dllda_push(dllda *c, uintptr_t ptr)
{
// printf("%p %p \n", &c, c);
nodeld *new = nodeinit(ptr);
// printf("%p ", new);
structure_fixups(c);
if (c->end || c->start)
{
new->prev = c->end ? c->end : c->start;
new->prev->next = new;
c->end = new;
}
else
c->start = new;
c->size++;
}
// TODO!
int dllda_len(dllda *c) { return c->size; }
nodeld *adll_find(dllda *l, int i)
{
int len = dllda_len(l);
if (i < 0 || i >= len)
return NULL;
int attack_from = 1;
nodeld *curnode = l->start;
int from = 0;
if (i > len / 2)
{
attack_from = -1;
curnode = l->end;
from = len - 1;
}
// start is 0 end is len l - 1 aka -1
for (int cur = from; cur != i; cur += attack_from)
{
if (attack_from == 1)
curnode = curnode->next;
else if (attack_from == -1)
curnode = curnode->prev;
}
return curnode;
}
int dllda_rem(dllda *l, int pos, uintptr_t *puthere)
{
nodeld *c = adll_find(l, pos);
*puthere = (uintptr_t)c->ptr;
adll_remove(l, c);
return 0;
}
int adll_findHandler(dllda *l, int i, nodeld *c)
{
c = adll_find(l, i);
if (c == NULL)
{ // start now is No Longer Null
if (dllda_len(l) == 0)
return -1;
else
return -2;
};
return 0;
}
int dllda_query(dllda *l, int i, uintptr_t *copy_here)
{
nodeld *g = NULL;
int c = adll_findHandler(l, i, g); // ma
if (c != 0)
return c;
*copy_here = g->ptr;
return 0;
}
int dllda_popleft(dllda *l, uintptr_t *puthere)
{
nodeld *startMayBeNull = adll_find(l, 0);
if (startMayBeNull == NULL)
{ // start now is No Longer Null
if (dllda_len(l) == 0)
return -1;
else
return -2;
};
*puthere = startMayBeNull->ptr;
adll_remove(l, startMayBeNull);
return 0;
}
int dllda_pop(dllda *l, uintptr_t *puthere)
{
nodeld *endMayBeNull = adll_find(l, dllda_len(l) - 1);
if (endMayBeNull == NULL)
{
if (dllda_len(l) == 0)
return -1;
else
return -2;
};
*puthere = endMayBeNull->ptr;
adll_remove(l, endMayBeNull);
return 0;
}
// no add to size, also bugs when edges (i mean this is a interceptor)
int adll_intercept(nodeld *tobeplaced, uintptr_t ptr)
{
nodeld *next = tobeplaced;
nodeld *prev = next->prev;
nodeld *new = nodeinit(ptr);
new->prev = prev;
new->next = next;
if (prev->next != next || next->prev != prev)
return -1;
prev->next = new;
next->prev = new;
return 0;
}
int dllda_insert(dllda *l, int pos, uintptr_t ptr)
{
int len = dllda_len(l);
if (0 < pos && pos < len)
{
nodeld *replaced = adll_find(l, pos);
if (replaced == NULL)
return -1;
if (adll_intercept(replaced, ptr) != 0)
return -1;
l->size++;
return 0;
}
else if (pos == len)
dllda_push(l, ptr);
else if (pos == 0)
dllda_pushleft(l, ptr);
else
return -1;
return 0;
}
int dllda_peek(dllda *l, uintptr_t *copy_here)
{
return dllda_query(l, dllda_len(l) - 1, copy_here);
}
int dllda_peekleft(dllda *l, uintptr_t *copy_here)
{
return dllda_query(l, 0, copy_here);
}
void printItOut(dllda *new)
{
int sz = dllda_len(new);
printf("size: %d [", sz);
uintptr_t *c = (uintptr_t *)calloc(sz, sizeof(uintptr_t));
for (int i = sz - dllda_to_list(new, c);
printf("(%d: %d)", i, *(int *)c[i]) &&
(++i < sz); // how to get value
printf(", "))
;
printf("]\n");
}
void traverse(dllda *l)
{
printf("\nthis is l with size %d, start being uint %p, and end we do not know %p\n", l->size, l->start, l->end);
printf("array repr: ");
printItOut(l);
int leng = dllda_len(l);
nodeld *someNode = l->start;
for (int i = 0; (i < leng); i++, // funnily
(i < leng) && (printf("somenode next now.\n") == 0))
{
printf("with find:\n");
nodeld *thisNode = adll_find(l, i);
printf("node %p at %d, ", thisNode, i);
printf("with ptr %p val %d, ", thisNode->ptr, *(int *)thisNode->ptr);
printf("prev %p and next %p\n", thisNode->prev, thisNode->next);
printf("with node traversal:\n");
printf("node? at %d, ", i);
printf("with ptr %p val %d, ", someNode->ptr, *(int *)someNode->ptr);
printf("prev %p and next %p\n", someNode->prev, someNode->next);
someNode = someNode->next;
}
printf("that is all.\n");
}
// return sizes
int dllda_to_list(dllda *l, uintptr_t *arr)
{
int len = dllda_len(l);
nodeld *node = l->start;
for (int i = 0; i < len; i++)
{
arr[i] = node->ptr;
node = node->next;
}
return len;
}
// int main(int argc, char const *argv[])
// {
// dllda *new = initlda();
// int dsa = 17;
// dllda_push(new, (uintptr_t)&dsa);
// // traverse(new);
// int item1 = 1;
// dllda_push(new, (uintptr_t)&item1);
// // traverse(new);
// int item2 = 4;
// dllda_push(new, (uintptr_t)&item2);
// // traverse(new);
// int item3 = 13;
// dllda_push(new, (uintptr_t)&item3);
// // traverse(new);
// int item4 = -2;
// dllda_pushleft(new, (uintptr_t)&item4);
// int item5 = 9;
// dllda_insert(new, 2, (uintptr_t)&item5);
// int item6 = 25;
// dllda_insert(new, 0, (uintptr_t)&item6);
// printItOut(new);
// traverse(new);
// uintptr_t this;
// int code;
// code = dllda_rem(new, 3, &this);
// printf(" %d ", *(int *)this);
// printItOut(new);
// code = dllda_popleft(new, &this);
// printf(" %d ", *(int *)this);
// printItOut(new);
// code = dllda_pop(new, &this);
// printf(" %d ", *(int *)this);
// printItOut(new);
// traverse(new);
// return 0;
// }
+42
View File
@@ -0,0 +1,42 @@
#ifndef LDA_LINKEDLIST_H
#define LDA_LINKEDLIST_H
// free with: the good of the c
#include <stdlib.h>
typedef struct Node
{
uintptr_t ptr; // remember to cast!
struct Node *prev;
struct Node *next;
} nodeld;
typedef struct DoublyLinkedList
{
int size;
nodeld *start;
nodeld *end;
} dllda;
dllda *initlda();
nodeld *nodeinit(uintptr_t ptr);
int structure_fixups(dllda *l);
void dllda_pushleft(dllda *c, uintptr_t ptr);
void dllda_push(dllda *c, uintptr_t ptr);
int dllda_insert(dllda *l, int pos, uintptr_t ptr);
// nodeld *adll_find(dllda *l, int i);
int dllda_len(dllda *c);
int dllda_query(dllda *l, int i, uintptr_t *copy_here);
int dllda_peekleft(dllda *l, uintptr_t *copy_here);
int dllda_peek(dllda *l, uintptr_t *copy_here);
// int adll_intercept(nodeld *tobeplaced, uintptr_t ptr);
// int adll_remove(dllda *whatif, nodeld *node);
int dllda_rem(dllda *l, int pos, uintptr_t *puthere);
int dllda_popleft(dllda *l, uintptr_t *puthere);
int dllda_pop(dllda *l, uintptr_t *puthere);
// void traverse(dllda *l);
// void printItOut(dllda *new);
int dllda_to_list(dllda *l, uintptr_t *arr);
#endif