I just wrote a a stack implementation using linked list..
when i pop the values it is as expected
when I pop the values..it prints out as 7 8 9! it only prints correctly if I put the print statements on different lines..literally. It doesn't make sense to me..Code:stack.push(7); stack.push(8) ; stack.push(9) ; cout << stack.pop() << endl << stack.pop() << endl ;
i debugged and the first pop definitely returns a 9..so how does it end up being at the end?
here is the code.. .cpp file
Code:#include "StackLinkedList.h" Stack::Stack() { size=0; stackPtr = NULL; } Stack::~Stack() { delete stackPtr ; } void Stack::push(int x) { ++size ; Node *temp ; temp= stackPtr; stackPtr = new Node(x) ; stackPtr->next =temp; } bool Stack::empty() { return (stackPtr==NULL) ; } int Stack::pop() { int n; Node *t = stackPtr ; stackPtr = stackPtr->next ; n = t->data; delete t ; return n ; }



LinkBack URL
About LinkBacks



