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
No related branches found
No related tags found
1 merge request!16Resolve "Add pop function"
This commit is part of merge request !16. Comments created here will be created in the context of that merge request.
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){
s->top = -1;
s->capacity = DEFAULT_CAPACITY;
s->data = malloc(sizeof(int) * DEFAULT_CAPACITY);
}
bool stack_is_empty(stack s){
return s.top == -1;
}
#define DEFAULT_CAPACITY 4
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);
}
typedef struct _stack {
int *data;
int capacity;
int top;
} stack;
// depile
void stack_pop(stack *s, int *value){
if (stack_is_empty(*s)){
return;
}
assert(s->top>=0 && "Stcak is empty\n");
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;
}
\ 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