feat: ✨ dynamic queue
fix: 🐛 memory math
This commit is contained in:
parent
5b6cb7af0b
commit
409644cff5
3 changed files with 118 additions and 6 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue