T
Thomas Ibbotson
I've been going through a tutorial on C++ and I've come across what I
regard as some strange behaviour. As part of the tutorial on classes a
simple stack is implemented with functions push() and pop().
I test this stack using:
stack a_stack; // Create an instance of stack
// Push some data onto the stack
a_stack.push(1);
a_stack.push(2);
a_stack.push(3);
// Now pop it off again and see what we get
cout << a_stack.pop() << " " << a_stack.pop() << " " <<
a_stack.pop() << endl;
// Refill the stack
a_stack.push(1);
a_stack.push(2);
a_stack.push(3);
// Try it a different way
cout << a_stack.pop() << endl;
cout << a_stack.pop() << endl;
cout << a_stack.pop() << endl;
The resulting output is:
1 2 3
3
2
1
Where I would have expected:
3 2 1
3
2
1
Can someone tell me what is going on?
Thanks,
Tom
regard as some strange behaviour. As part of the tutorial on classes a
simple stack is implemented with functions push() and pop().
I test this stack using:
stack a_stack; // Create an instance of stack
// Push some data onto the stack
a_stack.push(1);
a_stack.push(2);
a_stack.push(3);
// Now pop it off again and see what we get
cout << a_stack.pop() << " " << a_stack.pop() << " " <<
a_stack.pop() << endl;
// Refill the stack
a_stack.push(1);
a_stack.push(2);
a_stack.push(3);
// Try it a different way
cout << a_stack.pop() << endl;
cout << a_stack.pop() << endl;
cout << a_stack.pop() << endl;
The resulting output is:
1 2 3
3
2
1
Where I would have expected:
3 2 1
3
2
1
Can someone tell me what is going on?
Thanks,
Tom