#include <stdio.h>
/*
* Servicios de Pila:
*
* pilaAdm_t *stackCreate(int size, int sizeTipo);
*
* void stackDestroy(stack_t *pila);
*
* boolean push(pilaAdm_t *stack, void *elemento);
*
* boolean pop(pilaAdm_t *stack, void *elemento);
*
* stackEmpty(pila);
*
* stackFull(pila);
*
*/
#ifndef _stdio_h
#include <stdio.h>
#endif
#ifndef _stdlib_h
#include <stdlib.h>
#endif
#ifndef _string_h
#include <string.h>
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE !FALSE
#endif
#define stackEmpty(pila) pila->head == 0
#define stackFull(pila) pila->head == pila->size
typedef char boolean;
typedef struct{
void *elementos;
int size;
int head;
}pila_t;
typedef struct{
pila_t *pila;
int sizeTipo;
}pilaAdm_t;
pilaAdm_t *stackCreate(int size, int sizeTipo);
void stackDestroy(pilaAdm_t *pila);
boolean push(pilaAdm_t *stack, void *elemento);
boolean pop(pilaAdm_t *stack, void *elemento);
pilaAdm_t *stackCreate(int size, int sizeTipo){
pilaAdm_t
*stack
= (pilaAdm_t
*) malloc (sizeof(pilaAdm_t
));
stack
->pila
= (pila_t
*) malloc (sizeof(pila_t
)); stack
->pila
->elementos
= (void *) malloc (sizeTipo
* size
); stack->pila->size = size;
stack->pila->head = 0;
stack->sizeTipo = sizeTipo;
return stack;
}
void stackDestroy(pilaAdm_t *stack){
free(stack
->pila
->elementos
); }
boolean push(pilaAdm_t *stack, void *elemento){
if(!stack->pila || stackFull(stack->pila))
return FALSE; //si no hay pila o esta esta llena no puedo insertar
memcpy((char *)stack
->pila
->elementos
+ (stack
->sizeTipo
* stack
->pila
->head
++), elemento
,stack
->sizeTipo
);
return TRUE;
}
boolean pop(pilaAdm_t *stack, void *elemento){
if(!stack->pila || stackEmpty(stack->pila))
return FALSE; //Si no tengo pila, o si esta vacia no puedo hacer pop
stack->pila->head--;
//memcpy(elemento, (void *)(((char *)stack->pila->elementos) + (stack->sizeTipo * stack->pila->head)), stack->sizeTipo);
memcpy(elemento
, stack
->pila
->elementos
+ (stack
->sizeTipo
* stack
->pila
->head
), stack
->sizeTipo
);
return TRUE;
}
int main(void){
pilaAdm_t *stack;
int i,a, n = 5;
stack = stackCreate(n,sizeof(int));
for(i=0; i< 5; i++)
push(stack,(void *)&i);
for(i=0; i<5; i++){
pop(stack,(void *)&a);
}
return 0;
}