+ 1
Why does it output 3 1 4 2 and not 1 2 3 4 ?
9 Réponses
+ 11
Vincent Maestro shouldn't be, just understand that you always get your pop up results in stack in reverse order compared to your pushing from input
+ 9
Stack is last input first output, so it is like you fill one railway with wagons and start to remove wagons from the end
+ 9
Vincent Maestro so you have to get them in reverse order, so everything is correct
+ 7
Vincent Maestro sorry first in last out, sorry i confused it with queue
+ 4
A stack is first in last out.
So the first value 1 comes out as the last value after 3.
A queue is first in first out.
+ 3
I don't get your question. Your code works fine, you just have to think about it.
stack1.push(1);
stack2.push(2);
stack1.push(3);
stack2.push(4);
If you think about it, the first stack contains [1, 3], and the second stack contains [2, 4]. If you pop stack1, then stack2, you should and will get [3, 1, 4, 2]
+ 2
Zhenis Otarbay first input, first output right?
Stack1 recieved (1) before it received (3) similarly Stack2 received (2) before receiving (4) so why does pop return 3 before 1 and 4 before 2 ?
+ 2
Airree I believe 1 was stored before 3 and 2 before 4 so why does the output go 3 1 4 2 ?
That's just a little confusing to me
+ 2
Any line of code doing some manipulation I don't know about?