noooo
This commit is contained in:
1 parent
045d7367a8
commit
8aa73bde5e
10 files changed
+649
-47
No files matched your search
+27
-18
@@ -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
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
// }
|
||||
@@ -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
|
||||
Reference in new issue
Block a user