Thread: Stack ADT

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    8

    Stack ADT

    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

    Code:
    #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;
    }
    STACK INTERFACE

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pablo9891
    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).
    Use English. It is not that English is more professional, but that you are likely to have a wider reach when your code is readable by people who know (at least some) English.

    Quote Originally Posted by pablo9891
    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.
    The typical approach is to store a pointer to void*. The user is thus entirely responsible for interpreting the data that the pointer points to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    The typical approach is to store a pointer to void*. The user is thus entirely responsible for interpreting the data that the pointer points to
    I was reading this article Void pointers that talks about void pointers and in the example it separates the implementation of a function for the char data type, for the integer, etc.

    What you are proposing is that i should make a more generic code in which for example in the function

    Code:
    //POP AN ELEMENT FROM THE STACK
    int pop_pila(pila *mi_pila)
    I should do something like this?

    Code:
    //POP AN ELEMENT FROM THE STACK
    void* pop_pila(pila *mi_pila)
    Could you give me an example please?

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Quote Originally Posted by Salem View Post
    For no particular reason i didn't know this forum even exists and i wanted to start posting this question here. I still in DIC i really like that forum

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    I've just made a few modifications to my ADT in order to make it a bit more generic and i found this problem that i'm not sure what is wrong with it:

    Code:
    int pop_pila(pila *mi_pila,void *val) {    if(pila_vacia(mi_pila)) {
            return -1;
        }
        else {
            val = mi_pila->vec[mi_pila->tope];
            printf("POP: %c \n",*(char*)val);//this work's fine
            mi_pila->tope--;
            return 0;
        }
    }
    the problem is that when i try to show the value of the val argument outside the function it doesn't change, so i can't show what's inside the stack

    this is how the stack structure is build

    Code:
    typedef struct {        
            void **vec;
            int tope;
            int max_elementos;
        }pila;
    ans this is how i call the function

    Code:
    char letra;
    pop_pila(mi_pila,&letra)
    printf("%c \n",(char*)letra);//always show's the same value

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by pablo9891 View Post
    Code:
    char letra;
    pop_pila(mi_pila,&letra)
    printf("%c \n",(char*)letra);//always show's the same value
    Why do you cast "letra" to a pointer to char?

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack!
    By spank in forum C Programming
    Replies: 5
    Last Post: 03-27-2006, 02:06 AM
  2. Que/Stack
    By xddxogm3 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2003, 09:44 PM
  3. Stack
    By Max in forum C Programming
    Replies: 1
    Last Post: 11-04-2002, 12:40 PM
  4. Using a stack
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-09-2002, 10:44 PM
  5. stack
    By SilasP in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2002, 09:42 AM