Skip to content
Snippets Groups Projects
Commit d93508e5 authored by ines.maya's avatar ines.maya Committed by orestis.malaspin
Browse files

Resolve "Add pop function that removes top of the stack and reallocates if top < capacity / 4"

parent e4bcf652
Branches
Tags
1 merge request!16Resolve "Add pop function"
*.o
main
\ No newline at end of file
......@@ -18,6 +18,19 @@ void stack_destroy(stack *s){
s->top = -1;
}
void stack_pop(stack *s, int *value){
if (stack_is_empty(*s)) {
return;
}
if (s->top == s->capacity/4){
s->capacity /= 2;
s->data = realloc(s->data, sizeof(int)*s->capacity);
}
*value = s->data[s->top];
s->top -= 1;
}
void stack_peek(stack s, int *value){
if (!stack_is_empty(s)) {
*value = s.data[s.top];
......@@ -51,4 +64,3 @@ int get_length(stack s)
{
return s.top + 1;
}
......@@ -11,6 +11,7 @@ void stack_init(stack *stack);
void stack_destroy(stack *s);
void stack_pop(stack *s, int *value);
void stack_peek(stack s, int *value);
void stack_clone(stack s, stack *clone);
int get_length(stack s);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment