• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack to Queue  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to convert Stack(LIFO) to Queue(FIFO) using java ?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure you can convert the two, but you can easily pop all the members of the Stack and put them into a Queue. You can also use a little-known class which combines both functionalities.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could also create a second queue. pop everything off the first one, and push onto the second. when the first is empty, pop the element off queue 2 and save it.

then reverse by popping everything off queue 2 and pushing back onto queue 1. then return the saved element.




I didn't say it was a GOOD way to do it. But if you ONLY have queues, I think it would work.
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good idea, Kai Witte.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!