Skip to content
Snippets Groups Projects
Select Git revision
  • 6119dc9c9e19edca3b9c697820897962154944f9
  • main default protected
  • 3-add-makefile-with-structure
  • 9-add-destroy-function-4
  • 4-add-create-init-function
  • 5-add-push-function-2
  • 9-add-destroy-function-3
  • 9-add-destroy-function-2
  • v0.1
9 results

stack.c

Blame
  • ines.maya's avatar
    ines.maya authored
    6119dc9c
    History
    stack.c 626 B
    #include "stack.h"
    
    void stack_init(stack *s){
        s->top = -1;
        s->capacity = DEFAULT_CAPACITY;
        s->data = malloc(sizeof(int) * DEFAULT_CAPACITY);
    }
    
    bool stack_is_empty(stack s){
    	return s.top == -1;
    }
    
    void stack_peek(stack s, int *value){
        if (!stack_is_empty(s)) {
            *value = s.data[s.top];
        }
    	printf("peek : value %d, top : %d\n", s.data[s.top],s.top);
    }
    
    // depile
    void stack_pop(stack *s, int *value){
    	if (stack_is_empty(*s)){
    		return;
    	}	
    
    	if (s->top == s->capacity/4){
    		s->capacity /= 2;
    	}
    	
    	*value = s->data[s->top];
    	printf("pop : %d, top : %d\n", s->data[s->top],s->top);
    	s->top -= 1;
    }