feat: dynamic queue

fix: 🐛 memory math
This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-07-10 22:16:33 +03:00
parent 5b6cb7af0b
commit 409644cff5
3 changed files with 118 additions and 6 deletions

View file

@ -23,7 +23,7 @@ Stack new() {
}
int push(Stack* stack, int element) {
int* new_space_ptr = (int*)realloc(stack->elements, stack->size+sizeof(int));
int* new_space_ptr = (int*)realloc(stack->elements, (stack->size+1)*sizeof(int));
if (!new_space_ptr) {
return -1;
}
@ -38,10 +38,8 @@ int* pop(Stack* stack) {
return NULL;
}
int* element = &stack->elements[stack->top];
int* new_space_ptr = (int*)realloc(stack->elements, stack->size-sizeof(int));
if (new_space_ptr) {
return NULL;
}
int* new_space_ptr = (int*)realloc(stack->elements, (stack->size-1) * sizeof(int));
stack->size--;
stack->top--;
return element;