# Implement a stack using queue

Problem: Using a queue data structure, obtain a stack.

Problem Explanation: Stack: A stack is a Last in First Out(LIFO) collection. The last thing we added(pushed) is the first thing that gets pulled(popped) off.

Queue: A queue is a First In First Out(FIFO) collection. The first thing we added(pushed) is the first thing that gets pulled(popped) off.

For any given queue,

Input: 10<-20<-30<-40(order of elements pushed- left to right)

Output: 10->20->30->40(order of elements popped- right to left)

Java Implementation

```public class StackFromQueue {
Queue&lt;Integer&gt; queue = new LinkedList&lt;Integer&gt;();

public void push(int value) {
int qSize = queue.size();
queue.add(value);

for (int i = 0; i &lt; qSize; i++) {
queue.add(queue.remove());
}
}

public void pop() {
System.out.println("Element removed from stack is:" + queue.remove());
}

public static void main(String[] args) {
StackFromQueue stackFromQueue = new StackFromQueue();
stackFromQueue.push(10);
stackFromQueue.push(20);
stackFromQueue.push(30);
stackFromQueue.push(40);

System.out.println(stackFromQueue.queue);
stackFromQueue.pop();
System.out.println(stackFromQueue.queue);

}
}

```

TimeComplexity: O(n)

Note: If you find any other better way to approach to this problem or you find any issue/error in above code snippets/approaches – please share it in the comments section below and get a chance to enrol free of cost for an online Live Data Structure and Algorithm course (specially designed for interview preparation for software companies like Amazon, Google, Facebook, FlipKart, SnapDeal, HealthKart…)