Like Tree1Likes
  • 1 Post By AndiPersti

Stack ADT

This is a discussion on Stack ADT within the C Programming forums, part of the General Programming Boards category; Basically what i've been trying to do lately is to make a library of my own whith some ADT of ...

  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
    21,588
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    stahta01 likes this.

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, 01: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, 11:40 AM
  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, 08:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21