Skip to content
Snippets Groups Projects
Commit 4f002953 authored by ines.maya's avatar ines.maya
Browse files

eneleve tous ceux qui est pas fonction pop

parent 6119dc9c
Branches
Tags
1 merge request!16Resolve "Add pop function"
CC=gcc
CFLAGS=-Wall -Wextra -pedantic -g -std=c11 -O3
CFLAGS_ASAN=-fsanitize=address -fno-omit-frame-pointer
LDFLAGS=-lm
LDFLAGS_ASAN=-fsanitize=address -fno-omit-frame-pointer
main: stack.o main.o
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(LDFLAGS_ASAN)
%.o: %.c
$(CC) $(CFLAGS) $(CFLAGS_ASAN) -c $^ -o $@
clean:
rm -f *.o main
#include "stack.h"
int main(){
stack s;
int value = 13;
stack_init(&s);
s.top += 1;
s.data[s.top] = value;
s.top += 1;
s.data[s.top] = value+1;
stack_pop(&s, &value);
free(s.data);
s.data = NULL;
s.capacity = -1;
s.top = -1;
}
\ No newline at end of file
#include "stack.h" #include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
void stack_init(stack *s){ #define DEFAULT_CAPACITY 4
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){ typedef struct _stack {
if (!stack_is_empty(s)) { int *data;
*value = s.data[s.top]; int capacity;
} int top;
printf("peek : value %d, top : %d\n", s.data[s.top],s.top); } stack;
}
// depile // depile
void stack_pop(stack *s, int *value){ void stack_pop(stack *s, int *value){
if (stack_is_empty(*s)){ assert(s->top>=0 && "Stcak is empty\n");
return;
}
if (s->top == s->capacity/4){ if (s->top == s->capacity/4){
s->capacity /= 2; s->capacity /= 2;
} }
*value = s->data[s->top]; *value = s->data[s->top];
printf("pop : %d, top : %d\n", s->data[s->top],s->top);
s->top -= 1; s->top -= 1;
} }
\ No newline at end of file
#ifndef _STACK_H_
#define _STACK_H_
#include <stdio.h>
#include <stdlib.h>
#include "stdbool.h"
#define DEFAULT_CAPACITY 4
typedef struct _stack {
int *data;
int capacity;
int top;
} stack;
void stack_init(stack *s);
void stack_peek(stack s, int *value);
void stack_pop(stack *s, int *value);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment