

- #Linked list stack implememntation using two queues 261 full
- #Linked list stack implememntation using two queues 261 code
Solution: Maintain a queue of size n, and as the numbers keep on coming, keeping on pushing them to queue until queue is full. Think about it before looking at the solution. At any instant you need to output last n numbers which appeared in the stream. Queue implementation using Stacks Adhoc Problem 1
#Linked list stack implememntation using two queues 261 full
Click on the link below to see the full working code: We are just using the S2 as a supportive stack to preserve the order of elements present in S1. keep on doing the 4th step till S2 is empty.keep on popping from S2 and push the corresponding element to S1.Now our target element is the top element of S2, so pop() it.keep on popping from S1 and push the corresponding element to S2.Now when a push() happens we will insert the element into S1 and whenever a pop() operation happens, then we will follow the following steps: So we will try to achieve FIFO by the use of two stacks. Stack implementation using Queues Queue implementation using Stacks
#Linked list stack implememntation using two queues 261 code
The last two steps are just to transfer elements from Q2 to Q1.įollowing is the C++ code for the above(here I will be using STL Queues): Notice one thing here: we are using Q2 as supporting Queue (just to store popped elements from Q1). The above five steps are needed to be done, for each pop operation.

Now our target is to implement push() and pop() using Queues. Solution: We know that Stack follows LIFO property. So once they get into loop(if the loop exist) they will certainly meet at some point and if they don’t meet it means that there is no loop in the linked list.īrainStorm Will the above logic work if the other pointer speed is 3x,4x… times the first one? Stack implementation using Queues

Consider two pointers moving in the linked list and one is moving at twice the speed of the other. This idea we will use here to detect a loop in the linked list. Notice one thing- faster runner will cross the slower one at some point. Now, consider both the athletes are running at different speeds i.e. Solution The underlying idea which we will use here is that - Consider a circular track in where two atheletes are running.

Traverse over the string, on encounter with ‘(‘ push it to the stack.Solution: This problem can be solved by using stack. Given a sequence of opening and closing braces, find whether the given sequence is a valid sequence or not.
