Search code examples
c++stack

C++ Program execution halts with no error


I've started to experiment (no previous experience with the language) with c++, I'm using Dev C++ ver. 5.11. I'm trying to implement a stack, I don't want to use the ready Stack library because I want to practice. However with no errors the program halts after printing out the stack items and further statements aren't executed. What is going wrong?

#include <iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

struct node{
    int data;
    struct node *next;
};

struct node *push(struct node *root, int ikey){
    struct node *ptr;
    
    ptr=(struct node *)malloc(sizeof(struct node));
    ptr->data=ikey;
    ptr->next=root;
    root = ptr;
    return root;
}
struct node *pop(struct node *root){
    struct node *ptr;
    
    ptr = root;
    root = ptr->next;
    return root;
}
void Print_Stack(struct node *root){
    struct node *n;
    n = root;
    do{
        printf("Node -> %d \n",n->data);
        n = n->next;
    }while (n != NULL);

}

int main(){
    struct node *stoiva, *tmp;
    int x;
    tmp = (struct node *)malloc(sizeof(struct node));
    do{
        printf("Give x :");
        scanf("%d",&x);
        if (x > 0){
            stoiva = push(stoiva, x);
        }
    }while ( x > 0);
    tmp = stoiva;
    do{
        cout << tmp->data << endl;
        tmp = tmp->next;
    }while (tmp->next != NULL);
    printf ("-----------------\n");
    return 0;

}

Solution

  • Your code has a number of issues:

        do{
            cout << tmp->data << endl;
            tmp = tmp->next;
        }while (tmp->next != NULL);
    

    will crash if tmp is initially null (an empty stack) and will also fail to print the last element in the stack. Both problems can be solved with:

        while (tmp){
            cout << tmp->data << endl;
            tmp = tmp->next;
        }
    

    stoiva is not initialised so may not be null, you should always initialise pointers for safety.

    You should use new rather than malloc in c++, not only is it required for a lot of c++ types its much simpler too:

    ptr=(struct node *)malloc(sizeof(struct node))
    

    becomes:

    ptr=new node();
    

    You should check the result of scanf to make sure it succeeded before using the resulting value.

    You need to free the memory you allocated, in your case you need to use free but if using new you need to use delete.

    A fully working version would be:

    #include <iostream>
    #include<stdio.h>
    #include<stdlib.h>
    
    using namespace std;
    
    struct node{
        int data;
        node *next = nullptr;
    };
    
    node *push(node *root, int ikey){
        node *ptr = new node();
        ptr->data = ikey;
        ptr->next=root;
        root = ptr;
        return root;
    }
    
    node *pop(node *root){
        node *ptr = root;
        root = ptr->next;
        delete ptr;
        return root;
    }
    
    int main(){
        node *stoiva = nullptr;
        int x;
        do{
            printf("Give x :\n");
            if (scanf("%d",&x) != 1)
            {
                break;
            }
            if (x > 0){
                stoiva = push(stoiva, x);
            }
        }while ( x > 0);
        node * tmp = stoiva;
        while (tmp){
            cout << tmp->data << endl;
            tmp = tmp->next;
        }
        while (stoiva){
            stoiva = pop(stoiva);
        }
        printf ("-----------------\n");
        return 0;
    
    }