Skip to content
Snippets Groups Projects

add function stack_push

Merged richarda.tyarks requested to merge 5-add-push-function into main
2 unresolved threads
Files
2
+ 15
2
#include "stack.h"
Please register or sign in to reply
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#define DEFAULT_CAPACITY 4
@@ -17,6 +17,18 @@ void stack_destroy(stack *s) {
s->top = -1;
}
void stack_push(stack *s, int value)
{
if (s->top == s->capacity-1)
{
assert(s->data = realloc(s->capacity * 2 * sizeof(int)));
s->capacity=s->capacity * 2;
}
s->top++;
s->data[s->top] = value;
}
void stack_pop(stack *s, int *value) {
if (stack_is_empty(*s)) {
return;
@@ -62,3 +74,4 @@ void stack_clone(stack s, stack *clone) {
int get_length(stack s) {
return s.top + 1;
}
Loading