We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Queue using Two Stacks
- Discussions
Queue using Two Stacks
Queue using Two Stacks
Sort by
recency
|
227 Discussions
|
Please Login in order to post a comment
Could someone show me how to unlock the test cases using Hackos. I only the lock sign and test saying, Hidden test cases, Unlock this testcase for 5 hackos. But there is no link or button to click.
JS
Summary
Here is a pure C solution. The standard C library doesn't include any pre-canned data structure implementations, so this solution includes a home-grown stack and queue implementation.
Approach
The main idea is to implement a queue (FIFO) using two stacks (LIFO) where each stack manages one end of the queue. The nifty part is that both stacks manage the same memory buffer. The 'rear' stack grows upward and is initially empty, whereas the 'front' stack grows downward and is initially full. Enqueue operations are only done on the 'rear' stack (i.e. push operations to an initially empty stack), and dequeue operations are only done to the 'front' stack (i.e. pop operations on a stack that is 'upside down' in memory and initially full).
Code
java code:
Java
Following is a single stack solution for those who are interested
public static void main(String[] args) {