Skip to content
Snippets Groups Projects

add function stack_push

Merged richarda.tyarks requested to merge 5-add-push-function into main
Files
2
+ 15
2
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
 
#include "stack.h"
 
#define DEFAULT_CAPACITY 4
#define DEFAULT_CAPACITY 4
@@ -17,6 +17,18 @@ void stack_destroy(stack *s) {
@@ -17,6 +17,18 @@ void stack_destroy(stack *s) {
s->top = -1;
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) {
void stack_pop(stack *s, int *value) {
if (stack_is_empty(*s)) {
if (stack_is_empty(*s)) {
return;
return;
@@ -62,3 +74,4 @@ void stack_clone(stack s, stack *clone) {
@@ -62,3 +74,4 @@ void stack_clone(stack s, stack *clone) {
int get_length(stack s) {
int get_length(stack s) {
return s.top + 1;
return s.top + 1;
}
}
 
Loading