ai safeguards
This commit is contained in:
1 parent
5968efc884
commit
0a7e4ac069
3 files changed
+131
-76
No files matched your search
+46
-51
@@ -3,25 +3,38 @@
|
||||
#include <stdlib.h>
|
||||
#include "arraylist.h"
|
||||
int CAPACITY = 24, ADDITIONAL = 16; // id rather just <<=1
|
||||
|
||||
#define INITIAL_CAPACITY 24
|
||||
#define RESIZE_FACTOR 2
|
||||
// stack list
|
||||
void ldalistinit(ldaArrayList *l)
|
||||
{
|
||||
l->size = 0;
|
||||
l->capacity = CAPACITY;
|
||||
l->capacity = INITIAL_CAPACITY;
|
||||
l->list = (int *)calloc(l->capacity, sizeof(int));
|
||||
if (!l->list)
|
||||
{
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
int ldalistlen(ldaArrayList *l)
|
||||
int ldalistlen(const ldaArrayList *l)
|
||||
{
|
||||
return l->size;
|
||||
}
|
||||
|
||||
int ldalistMakeRoom(ldaArrayList *l)
|
||||
{
|
||||
if (l->size >= l->capacity)
|
||||
{
|
||||
l->capacity += ADDITIONAL;
|
||||
l->list = (int *)realloc(l->list, l->capacity * sizeof(int));
|
||||
l->capacity *= RESIZE_FACTOR;
|
||||
int *new_list = (int *)realloc(l->list, l->capacity * sizeof(int));
|
||||
if (!new_list)
|
||||
{
|
||||
fprintf(stderr, "Memory reallocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
l->list = new_list;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
@@ -30,36 +43,29 @@ int ldalistMakeRoom(ldaArrayList *l)
|
||||
int ldalistpush(ldaArrayList *l, int value)
|
||||
{
|
||||
// raw dogging
|
||||
ldalistMakeRoom(l);
|
||||
ldalistMakeRoom(l); // ignoring this?
|
||||
// write first then increase
|
||||
l->list[l->size++] = value;
|
||||
return 0;
|
||||
}
|
||||
int ldalistadd(ldaArrayList *l, int at, int value)
|
||||
{
|
||||
ldalistMakeRoom(l);
|
||||
int size = ldalistlen(l);
|
||||
if (at > size)
|
||||
if (at < 0 || at > l->size)
|
||||
return -1;
|
||||
else if (at == size)
|
||||
|
||||
ldalistMakeRoom(l);
|
||||
for (int i = l->size; i > at; i--)
|
||||
{
|
||||
return ldalistpush(l, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = size; i > at; i--)
|
||||
{
|
||||
l->list[i] = l->list[i - 1];
|
||||
};
|
||||
l->list[at] = value;
|
||||
l->size += 1;
|
||||
return 0;
|
||||
}
|
||||
l->list[i] = l->list[i - 1];
|
||||
};
|
||||
l->list[at] = value;
|
||||
l->size++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ldalistpop(ldaArrayList *l, int *put_here)
|
||||
{
|
||||
if (ldalistlen(l) <= 0) // how would u like
|
||||
if (l->size <= 0) // how would u like
|
||||
return -1;
|
||||
*put_here = l->list[--l->size];
|
||||
// decrease to then take out
|
||||
@@ -68,47 +74,36 @@ int ldalistpop(ldaArrayList *l, int *put_here)
|
||||
|
||||
int ldalistremove(ldaArrayList *l, int at, int *put_here)
|
||||
{
|
||||
if (ldalistlen(l) <= 0)
|
||||
if (at < 0 || at >= l->size)
|
||||
return -1;
|
||||
|
||||
int size = ldalistlen(l);
|
||||
if (at > size)
|
||||
return -1;
|
||||
else if (at == size)
|
||||
*put_here = l->list[at];
|
||||
for (int i = at; i < l->size - 1; i++)
|
||||
{
|
||||
return ldalistpop(l, put_here);
|
||||
}
|
||||
else
|
||||
{
|
||||
*put_here = l->list[at];
|
||||
for (int i = at; i++ > size;)
|
||||
{
|
||||
l->list[i] = l->list[i - 1];
|
||||
};
|
||||
return 0;
|
||||
l->list[i] = l->list[i + 1]; // drag down one
|
||||
}
|
||||
l->size--;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ldalistlookup(ldaArrayList *l, int at, int *copy_here)
|
||||
int ldalistlookup(const ldaArrayList *l, int at, int *copy_here)
|
||||
{
|
||||
if (at > ldalistlen(l))
|
||||
if (at < 0 || at >= l->size)
|
||||
return -1;
|
||||
*copy_here = l->list[at];
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ldalistprint(ldaArrayList *l)
|
||||
void ldalistprint(const ldaArrayList *l)
|
||||
{
|
||||
printf("Array with size %d (capacity %d): ", l->size, l->capacity);
|
||||
printf("[");
|
||||
for (int i = 0, cur;
|
||||
ldalistlookup(l, i, &cur) == 0 &&
|
||||
printf("%d", cur) != 0 &&
|
||||
(++i < ldalistlen(l));
|
||||
printf(","))
|
||||
;
|
||||
|
||||
printf("]");
|
||||
printf("Array with size %d (capacity %d): [", l->size, l->capacity);
|
||||
for (int i = 0; i < l->size; i++)
|
||||
{
|
||||
printf("%d", l->list[i]);
|
||||
if (i < l->size - 1) // way easier for the eyes
|
||||
printf(", ");
|
||||
}
|
||||
printf("]\n");
|
||||
};
|
||||
|
||||
// int main(int argc, char const *argv[])
|
||||
|
||||
+46
-9
@@ -1,5 +1,7 @@
|
||||
#include "linkedlist.h"
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
// im not good at c!!!!! i do not use the Potential it has
|
||||
|
||||
// BE INCREDIBLY FR WITH NODEPTR
|
||||
@@ -11,12 +13,20 @@
|
||||
|
||||
dllda *initlda()
|
||||
{
|
||||
return (dllda *)calloc(1, sizeof(dllda));
|
||||
dllda *list = (dllda *)calloc(1, sizeof(dllda));
|
||||
if (!list) {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
nodeld *nodeinit(uintptr_t ptr)
|
||||
{
|
||||
nodeld *new = (nodeld *)calloc(1, sizeof(nodeld));
|
||||
// printf("%p ", new);
|
||||
if (!new) {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
new->ptr = ptr;
|
||||
return new;
|
||||
}
|
||||
@@ -175,9 +185,9 @@ void dllda_push(dllda *c, uintptr_t ptr)
|
||||
c->size++;
|
||||
}
|
||||
|
||||
int dllda_len(dllda *c) { return c->size; }
|
||||
int dllda_len(const dllda *c) { return c->size; }
|
||||
|
||||
nodeld *adll_find(dllda *l, int i)
|
||||
nodeld *adll_find(const dllda *l, int i)
|
||||
{
|
||||
int len = dllda_len(l);
|
||||
if (i < 0 || i >= len)
|
||||
@@ -211,7 +221,7 @@ int dllda_rem(dllda *l, int pos, uintptr_t *puthere)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dllda_query(dllda *l, int i, uintptr_t *copy_here)
|
||||
int dllda_query(const dllda *l, int i, uintptr_t *copy_here)
|
||||
{
|
||||
nodeld *g = adll_find(l, i);
|
||||
if (g == NULL)
|
||||
@@ -300,11 +310,11 @@ int dllda_insert(dllda *l, int pos, uintptr_t ptr)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int dllda_peek(dllda *l, uintptr_t *copy_here)
|
||||
int dllda_peek(const 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)
|
||||
int dllda_peekleft(const dllda *l, uintptr_t *copy_here)
|
||||
{
|
||||
return dllda_query(l, 0, copy_here);
|
||||
}
|
||||
@@ -347,7 +357,7 @@ void traverse(dllda *l)
|
||||
}
|
||||
|
||||
// return sizes
|
||||
int dllda_to_list(dllda *l, uintptr_t *arr)
|
||||
int dllda_to_list(const dllda *l, uintptr_t *arr)
|
||||
{
|
||||
int len = dllda_len(l);
|
||||
nodeld *node = l->start;
|
||||
@@ -383,7 +393,34 @@ int dllda_from_list(dllda *l, uintptr_t *arr, int size)
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
{
|
||||
|
||||
dllda *nlist = initlda();
|
||||
|
||||
// Test empty list
|
||||
uintptr_t value;
|
||||
assert(dllda_pop(nlist, &value) == -1); // Should return -1
|
||||
|
||||
// Test one element
|
||||
int item = 42;
|
||||
dllda_push(nlist, (uintptr_t)&item);
|
||||
assert(*(int *)nlist->start->ptr == 42); // Should print 42
|
||||
assert(dllda_pop(nlist, &value) == 0); // Should succeed
|
||||
assert(*(int *)value == 42); // Should print 42
|
||||
assert(dllda_len(nlist) == 0); // Should print 0
|
||||
|
||||
// Test multiple elements
|
||||
int item11 = 1, item22 = 2, item33 = 3;
|
||||
dllda_push(nlist, (uintptr_t)&item11);
|
||||
dllda_push(nlist, (uintptr_t)&item22);
|
||||
dllda_pushleft(nlist, (uintptr_t)&item33);
|
||||
printItOut(nlist); // Should print [3, 1, 2]
|
||||
assert(dllda_len(nlist) == 3);
|
||||
|
||||
return 0;
|
||||
|
||||
// wow
|
||||
|
||||
dllda *new = initlda();
|
||||
int dsa = 17;
|
||||
dllda_push(new, (uintptr_t)&dsa);
|
||||
|
||||
+39
-16
@@ -1,45 +1,68 @@
|
||||
#ifndef LDA_LINKEDLIST_H
|
||||
#define LDA_LINKEDLIST_H
|
||||
// free with: the good of the c
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Node structure for the doubly linked list
|
||||
typedef struct Node
|
||||
{
|
||||
uintptr_t ptr; // remember to cast!
|
||||
uintptr_t ptr; // Generic pointer or integer value
|
||||
struct Node *prev;
|
||||
struct Node *next;
|
||||
} nodeld;
|
||||
|
||||
// Doubly linked list structure
|
||||
typedef struct DoublyLinkedList
|
||||
{
|
||||
int size;
|
||||
nodeld *start;
|
||||
nodeld *end;
|
||||
int size; // Number of elements in the list
|
||||
nodeld *start; // Pointer to the first node
|
||||
nodeld *end; // Pointer to the last node
|
||||
} dllda;
|
||||
|
||||
// Initializes a new doubly linked list
|
||||
dllda *initlda();
|
||||
|
||||
// Initializes a new node with the given value
|
||||
nodeld *nodeinit(uintptr_t ptr);
|
||||
|
||||
// Fixes the structure of the list (e.g., after modifications)
|
||||
int structure_fixups(dllda *l);
|
||||
|
||||
// Adds a value to the front of the list
|
||||
void dllda_pushleft(dllda *c, uintptr_t ptr);
|
||||
|
||||
// Adds a value to the end of the list
|
||||
void dllda_push(dllda *c, uintptr_t ptr);
|
||||
|
||||
// Inserts a value at the specified position
|
||||
int dllda_insert(dllda *l, int pos, uintptr_t ptr);
|
||||
|
||||
// semi hidden node method
|
||||
nodeld *adll_find(dllda *l, int i);
|
||||
int dllda_len(dllda *c);
|
||||
int dllda_query(dllda *l, int i, uintptr_t *copy_here);
|
||||
// Finds the node at the specified index
|
||||
nodeld *adll_find(const dllda *l, int i);
|
||||
|
||||
int dllda_peekleft(dllda *l, uintptr_t *copy_here);
|
||||
int dllda_peek(dllda *l, uintptr_t *copy_here);
|
||||
// Returns the length of the list
|
||||
int dllda_len(const dllda *c);
|
||||
|
||||
// int adll_insert(nodeld *tobeplaced, uintptr_t ptr);
|
||||
// int adll_remove(dllda *whatif, nodeld *node);
|
||||
// Queries the value at the specified index
|
||||
int dllda_query(const dllda *l, int i, uintptr_t *copy_here);
|
||||
|
||||
// Peeks at the first value without removing it
|
||||
int dllda_peekleft(const dllda *l, uintptr_t *copy_here);
|
||||
|
||||
// Peeks at the last value without removing it
|
||||
int dllda_peek(const dllda *l, uintptr_t *copy_here);
|
||||
|
||||
// Removes the value at the specified position
|
||||
int dllda_rem(dllda *l, int pos, uintptr_t *puthere);
|
||||
|
||||
// Removes the first value from the list
|
||||
int dllda_popleft(dllda *l, uintptr_t *puthere);
|
||||
|
||||
// Removes the last value from the list
|
||||
int dllda_pop(dllda *l, uintptr_t *puthere);
|
||||
|
||||
// void traverse(dllda *l);
|
||||
// void printItOut(dllda *new);
|
||||
// Converts the list to an array
|
||||
int dllda_to_list(const dllda *l, uintptr_t *arr);
|
||||
|
||||
int dllda_to_list(dllda *l, uintptr_t *arr);
|
||||
#endif
|
||||
Reference in new issue
Block a user