Skip to content
Snippets Groups Projects
Commit 287ed7af authored by orestis.malaspin's avatar orestis.malaspin
Browse files

Merge branch '5-add-push-function' into 'main'

add function stack_push

See merge request !7
parents 42523a25 4cdb99ea
No related branches found
No related tags found
1 merge request!7add function stack_push
#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;
} }
#ifndef _STACK_H_ #ifndef _STACK_H_
#define _STACK_H_ #define _STACK_H_
typedef struct _stack { typedef struct _stack {
int *data; int *data;
int capacity; int capacity;
int top; int top;
} stack; } stack;
void stack_push(stack *s, int val);
void stack_init(stack *stack); void stack_init(stack *stack);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment