Implementando uma pilha a partir de uma fila
In a conversation on LinkedIn, the author explores implementing a stack using only one queue, contrasting with the classic two-stack queue. The article first clarifies what it means to implement one data structure with another, then presents the trivial case of a queue from two stacks. The core algorithm for a single-queue stack uses rotations: after each push, the queue is rotated so that the newest element is at the front, ensuring pop returns the most recently added item. The author provides Java code snippets and references prior work on queue automata and Turing machine equivalence. The implementation avoids side effects and uses a standard queue interface. The article is part of a series on data structure implementations, with links to related posts on Dijkstra's algorithm and finite automata.
Shows a clever algorithm for stack behavior using minimal resources.