Implement Queue using Stacksby@deft
339 reads

Implement Queue using Stacks

tldt arrow
Read on Terminal ReaderPrint this storyRead this story w/o Javascript

Too Long; Didn't Read

Sergei Golitsyn proposes a first in first out (FIFO) queue using only two stacks. The main trick will be in **pop** and **peak** operations. For pop operation, we have to move all elements from the current stack to the tmp stack. If **tmp** stack contains an element we can simply return an element from this stack. The problem is simple and clear, just add an element into the stack. For pop, we need to move an element to the next stack and return it.

Coin Mentioned

Mention Thumbnail
featured image - Implement Queue using Stacks
Sergey Golitsyn HackerNoon profile picture

@deft

Sergey Golitsyn

About @deft
LEARN MORE ABOUT @DEFT'S EXPERTISE AND PLACE ON THE INTERNET.

Credibility

react to story with heart

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa