I need help understanding a Push/Pop Data Structure

I've been learning push/pop data structures. So far I understand that the last item in is the first item out. The last item in will be at the bottom of the list in code view.

I know Push adds and Pop removes.

But I'm struggling to figure out this question.



What are the final contents of the stack after the following operations have been performed. The start is empty to begin with.


stackname.push(5);
stackname.push(14);
stackname.push(18);
stackname.pop();
stackname.push(5);
stackname.pop();
stackname.pop();
stackname.push(3);

What does it mean "The final contents of the stack? Does it mean how many int's are left or?
Probably means what are the values being held in the stack, it appears that there are more pushes than pulls so there should be some items still in the stack. What are they?

"If you were to pop the stack until it was empty and successively display each value on the screen, what would be displayed?"

Example:
stack.push(42);
stack.push(59);
stack.push(7);
stack.pop();
stack.push(4);

Contents:
4
59
42

Be sure to specify that you're listing the contents by order of pop(), to avoid confusion.
Last edited on
In your example helios is this what happened?

you added in 42 then 59 then 7 then you popped 7 off and added 4?

So my question

stackname.push(5);
stackname.push(14);
stackname.push(18);
stackname.pop();
stackname.push(5);
stackname.pop();
stackname.pop();
stackname.push(3);

would display the contents
5
14
3

?
No, that's wrong.
5
3?
In what order is that?
3 then 5 because 3 was the last item in?

So we started with nothing. Added 5, added 14, added 18, popped 18 off then added 5 then popped 5 off then popped 14 off then added 3

so 3,5?
I also have an additional question.

How do I display the results in a c++ program
cout << what?
hello friends,

Before answer your question let me tell you one thing " push and pop are operations on Stack which follows the rule of LIFO( Last In First Out) ".
stackname.push(5);
stackname.push(14);
stackname.push(18);
stackname.pop();
stackname.push(5);
stackname.pop();
stackname.pop();
stackname.push(3);
----------------------------------Result-------------
_____
| 3 | 5 |
if you need full code for Stack visit http://khawa.6te.net/stack.html
Last edited on
Topic archived. No new replies allowed.