Basically what i've been trying to do lately is to make a library of my own whith some ADT of basic data structures. At the moment the only ADT i have is one for the stack data structure what i would like to know is if there is some suggestion you could give me in order to make my code a bit more "professional", eficciente and cleaner (the code is not complete yet).
Another thing i'd like to know is if there's any way to declare the type of an array dinamically, i mean, in my code i'm having a char array, what i would like to know is if that depending of the type the user wants my program would be able to define the data type of the array at execution time.
All the suggestions are accepted...
STACK IMPLEMENTATION
STACK INTERFACECode:#include<tda_pila.h> //CREATE THE STACK pila crear_pila(int max_elementos) { pila mi_pila; mi_pila.vec = (TIPODATO*)malloc(sizeof(TIPODATO)*max_elementos); mi_pila.tope = -1; mi_pila.max_elementos = max_elementos; return mi_pila; } //CHECK IF THE STACK IS FULL int pila_llena(pila *mi_pila) { if(mi_pila->tope == mi_pila->max_elementos - 1) { return -1; } else { return 0; } } //CHECK IF THE STACK IS EMPTY int pila_vacia(pila *mi_pila) { if(mi_pila->tope == MINELEMENTOS) { return -1; } else { return 0; } } //PUSH AN ELEMENT IN THE STACK int push_pila(pila *mi_pila,char val) { if(pila_llena(mi_pila)) { return -1; } else { mi_pila->tope = mi_pila->tope + 1; mi_pila->vec[mi_pila->tope] = val; return 0; } } //POP AN ELEMENT FROM THE STACK int pop_pila(pila *mi_pila) { char val; if(pila_vacia(mi_pila)) { return -1; } else { val = mi_pila->vec[mi_pila->tope]; mi_pila->tope--; return val; } } //DESTROY STACK void borrar_pila(pila *mi_pila) { if(mi_pila->vec == NULL) { return; } free(mi_pila->vec); mi_pila->vec = NULL; } //CHECK THE VALUE OF THE TOP OF THE STACK char obtener_tope(pila *mi_pila) { char letra; letra = pop_pila(mi_pila); push_pila(mi_pila,letra); return letra; }
Code:#ifndef TDA_PILA_H_INCLUDED #define TDA_PILA_H_INCLUDED #include<stdio.h> #include<stdlib.h> #include<string.h> #define TIPODATO char #define MINELEMENTOS -1 typedef struct { TIPODATO *vec; int tope; int max_elementos; }pila; pila crear_pila(int max_elementos); int pila_llena(pila *mi_pila); int pila_vacia(pila *mi_pila); int push_pila(pila *mi_pila,char val); int pop_pila(pila *mi_pila); void borrar_pila(pila *mi_pila); char obtener_tope(pila *mi_pila); #endif // TDA_PILA_H_INCLUDED