Why does it output 3 1 4 2 and not 1 2 3 4 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why does it output 3 1 4 2 and not 1 2 3 4 ?

https://code.sololearn.com/cEU52VbjTvrp/?ref=app

20th Jul 2019, 11:27 AM
Vincent Maestro
Vincent Maestro - avatar
9 Answers
+ 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
20th Jul 2019, 12:05 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 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
20th Jul 2019, 11:38 AM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 9
Vincent Maestro so you have to get them in reverse order, so everything is correct
20th Jul 2019, 12:03 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 7
Vincent Maestro sorry first in last out, sorry i confused it with queue
20th Jul 2019, 12:59 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 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.
20th Jul 2019, 12:54 PM
Dragonxiv
Dragonxiv - avatar
+ 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]
20th Jul 2019, 11:42 AM
Airree
Airree - avatar
+ 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 ?
20th Jul 2019, 12:01 PM
Vincent Maestro
Vincent Maestro - avatar
+ 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
20th Jul 2019, 12:03 PM
Vincent Maestro
Vincent Maestro - avatar
+ 2
Any line of code doing some manipulation I don't know about?
20th Jul 2019, 12:04 PM
Vincent Maestro
Vincent Maestro - avatar