Browse Source

Ajout de la pile

master
Bastien 7 years ago
committed by Squiz
parent
commit
b5c0515e13
2 changed files with 40 additions and 0 deletions
  1. +28
    -0
      stack.c
  2. +12
    -0
      stack.h

+ 28
- 0
stack.c View File

@@ -0,0 +1,28 @@
#include "stack.h"
#define STACK_TYPE size_t
#ifdef STACK_TYPE
int stack_push(struct stack_cell_t** l, STACK_TYPE elt) {
if(!*l || (*l)->sommet <= 0 ) {
struct stack_cell_t* tmp = *l;
(*l) = (struct stack_cell_t*) malloc(sizeof(struct stack_cell_t));
if(!*l)
return 0;
(*l)->next=tmp;
(*l)->sommet=STACK_TAB_SIZE;
}
(*l)->tab[--((*l)->sommet)]=elt;
return 1;
}

STACK_TYPE* stack_pop(struct stack_cell_t** l) {
if(!*l)
return NULL;
STACK_TYPE* tmp = & ((*l)->tab[((*l)->sommet)++]);
if((*l)->sommet >= STACK_TAB_SIZE) {
struct stack_cell_t* tmp = *l;
(*l) = (*l)->next;
free(tmp);
}
return tmp;
}
#endif

+ 12
- 0
stack.h View File

@@ -0,0 +1,12 @@
#include <stdlib.h>
#define STACK_TYPE size_t
#ifdef STACK_TYPE
#define STACK_TAB_SIZE 10
struct stack_cell_t {
struct stack_cell_t* next;
STACK_TYPE tab[STACK_TAB_SIZE];
size_t sommet;
};
int stack_push(struct stack_cell_t** l, STACK_TYPE elt);
STACK_TYPE* stack_pop(struct stack_cell_t** l);
#endif

Loading…
Cancel
Save