Thread: Help with Linked Stack

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    2

    Question Help with Linked Stack

    Hey guys, so I'm new at programming and I'm having some troubles with my code.

    My code is suppose to order the elements of a stack during the insertion -push(x), that way the biggest value should be at the top of the stack at the end.

    For example, the input should be:

    6 1 9 4 3 2 10 22

    and the output should be:

    22 10 9 6 4 3 2 1

    Also, for the input I must read each line using the function fgets and to get each number of the line I must use strtok. The conversion of each number obtained with strtok to its integer can be done with the atoi function.

    So my code is looking like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct node {
        int info;
        struct node *ant;
    }node;
    
    typedef struct Pilha{
        node *topo;
    }Pilha;
    
    Pilha *P;
    
    void cria(Pilha *P){
        P->topo = NULL;
    }
    
    void push(Pilha *P, int x){
        node *p;
        node *aux;
        node *proximo;
    
        proximo = NULL;
        aux = NULL;
    
        p =(node*)malloc(sizeof(node));
    
        if(p == NULL){
            printf("Erro de alocacao!");
            exit(1);
            }
        else{
            p->info = x;
    
            if(P->topo == NULL){
                p->ant = NULL;
                P->topo = p;
           }
            else{
                aux = P->topo;
    
                while(aux != NULL && aux->info > x ){
                    proximo = aux;
                    aux = aux->ant;
                }
    
                proximo->ant = p;
                p->ant = aux;
    
            }
        proximo = NULL;
        aux = NULL;
        }
    }
    
    void converte(char string[], Pilha *P){
        char *pch;
        int x;
    
        pch = strtok(string," ");
        while (pch != NULL){
    
            x = atoi(pch);
            push(P, x);
            pch = strtok (NULL, " ");
        }
    }
    
    void imprime(Pilha *P){
        node *p;
    
        if(P->topo == NULL){
            printf("A Pilha esta vazia!");
            exit(1);
        }
        else{
            p = P->topo;
    
            while(p!=NULL){
                printf("%d ", p->info);
                p = p->ant;
            }
        }
    }
    
    void esvazia(Pilha *P){
        node *p, *aux;
    
        p = P->topo;
    
    
        while(p != NULL){
            aux = p;
            p = p->ant;
            free(aux);
        }
        free(P);
    }
    
    int main(){
    
        char string[6000];
    
        Pilha *P = (Pilha*) malloc (sizeof(Pilha));
        cria(P);
    
        printf("Informe os valores da pilha: ");
        fgets(string, 6000, stdin);
        converte(string, P);
    
        imprime(P);
        esvazia(P);
    
        return 0;
    
    }
    The problem is, when I run the code the terminal stop working and I can't determine what's wrong.
    If you guys could lead me the way and show where I'm missing it would be great!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Now is a good time to learn about debuggers.
    Code:
    $ gcc -g -Wall -Wextra bar.c
    $ gdb -q ./a.out 
    Reading symbols from /home/sc/Documents/a.out...done.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    Informe os valores da pilha: 1 2 3 4 5 6
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000400822 in push (P=0x602010, x=2) at bar.c:49
    49                  proximo->ant = p;
    (gdb) bt
    #0  0x0000000000400822 in push (P=0x602010, x=2) at bar.c:49
    #1  0x000000000040088e in converte (string=0x7fffffffc870 "1", P=0x602010) at bar.c:66
    #2  0x00000000004009ee in main () at bar.c:111
    (gdb) list
    44                  while(aux != NULL && aux->info > x ){
    45                      proximo = aux;
    46                      aux = aux->ant;
    47                  }
    48
    49                  proximo->ant = p;
    50                  p->ant = aux;
    51
    52              }
    53          proximo = NULL;
    (gdb) print proximo 
    $1 = (node *) 0x0
    You're dereferencing NULL at line 49.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    2
    Okay, I can see that. But how can I fix the problem?? I've tried a few things but I keep getting the same error...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But how can I fix the problem??
    You run the code in the debugger.

    Say you put a breakpoint at the start of push(), then you single-step the code to watch how it works line by line.

    You think about what should be happening next, then execute a line of code.

    When expectation differs from reality, you've found a bug.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Linked List query
    By Josh Heathcote in forum C Programming
    Replies: 1
    Last Post: 03-10-2014, 11:31 PM
  2. A Linked Stack in C
    By skytreader in forum C Programming
    Replies: 10
    Last Post: 12-20-2009, 10:32 AM
  3. stack using linked list problem
    By effa in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2009, 12:10 PM
  4. STACK/FIFO as a linked list
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-24-2009, 02:04 PM
  5. Stack in linked list
    By mag_chan in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 05:31 AM

Tags for this Thread