Any reason whenever i try to pop the stack and print it, the program crashes
class stack { private: stack* link; int data; public : stack* push (stack *top, int x){ stack* temp = new stack; temp->data = x; temp->link = top; top = temp; return top; } void pop(stack *top){ stack*save; if (top == NULL){ return; } else{ save = top; top = top->link; delete save; } } void print (stack *top){ stack* temp = top; while(temp != NULL){ cout<<temp->data <<" "; temp = temp->link; }cout <<endl; } };
@e.mccormick
Does it give an error?
it gives strange numbers on the compiler then crashes. it does the push just fine, but the pop, not so well
Are you sure it is nul terminated? If not, you would go out of range.
it should be null terminated if i didn't make any mistakes
Well, "strange numbers" generally means some unallocated memory or some other address that can cause garbage to pop up. Following this with a crash is a second indication of out of range.
i see.. i can't figure out where the problem might be
Couple examples for comparison: http://www.cstutoringcenter.com/tutorials/cpp/cpp18.php http://www.tutorialspoint.com/cplusplus/cpp_templates.htm
Do you need a return statement in a void function?
it is not returning any values to the void function
Join our real-time social learning platform and learn together with your friends!