Thread: C simple stack implementation code, crashed

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    6

    C simple stack implementation code, crashed

    Hello i am newbie in the forum, i have been trying recently to implement a stack behaviour, but my program return werid values, It compiles without warnings but i have been unavilabe to find the error, here is my code.

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    typedef struct tNodo{
        int valor;
        struct tNodo *next;
    }Nodo;
    
    
    typedef struct Tstack{
        int size;
        Nodo* top;
    }Stack;
    
    
    Stack *getNewStack(){
        Stack *s= (Stack*)malloc(sizeof(Stack));
        s->size = 0;
        s->top = NULL;
        return s;
    }
    void push(Stack *s, int value)
    {
        if(s)
        {
            Nodo* nodo = (Nodo*)malloc(sizeof(Nodo));
            nodo->valor = value;
            nodo->next = s->top;
            s->top = nodo;
            s->size++;
        }
        else
        {
            perror("Strack NULL\n");
        }
    }
    
    
    Nodo *pop(Stack *s)
    {
        if(s)
        {
            if(s->size)
            {
                Nodo *aux = (Nodo*)malloc(sizeof(Nodo));
                aux->next = s->top;
                s->top = s->top->next;
                s->size--;
                return(aux);
            }
        }
        perror("Void Stack\n");
        return NULL;
    }
    
    
    int main()
    {
        Stack *s = getNewStack();
        push(s,45);
        push(s,65);
        push(s,95);
        Nodo * cuales = (Nodo*)malloc(sizeof(Nodo));
        cuales = pop(s);
        printf("%d\n", cuales->valor);
        return 0;
    }
    Last edited by aurquiel; 07-02-2018 at 08:10 AM.

  2. #2
    Registered User
    Join Date
    Jul 2018
    Posts
    6
    SOLVED i missed i line code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Implementation No Errors
    By fredlo2010 in forum C Programming
    Replies: 2
    Last Post: 04-08-2016, 07:59 PM
  2. stack implementation using arrays
    By Mishu Singh in forum C Programming
    Replies: 1
    Last Post: 05-31-2014, 12:36 AM
  3. error free code but program crashed ???
    By aykuluk in forum C Programming
    Replies: 7
    Last Post: 08-14-2011, 08:01 AM
  4. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 09:32 AM
  5. code review request: stack implementation
    By cs32 in forum C Programming
    Replies: 6
    Last Post: 02-24-2008, 02:26 PM

Tags for this Thread