A similar question was asked earlier there, but the question here is the reverse of it, using two queues as a stack. The question...
Given two queues with their sta
import java.util.LinkedList;
import java.util.Queue;
public class StackQueue {
static Queue<Integer> Q1 = new LinkedList<Integer>();
static Queue<Integer> Q2 = new LinkedList<Integer>();
public static void main(String args[]) {
push(24);
push(34);
push(4);
push(10);
push(1);
push(43);
push(21);
System.out.println("Popped element is "+pop());
System.out.println("Popped element is "+pop());
System.out.println("Popped element is "+pop());
}
public static void push(int data) {
Q1.add(data);
}
public static int pop() {
if(Q1.isEmpty()) {
System.out.println("Cannot pop elements , Stack is Empty !!");
return -1;
}
else
{
while(Q1.size() > 1) {
Q2.add(Q1.remove());
}
int element = Q1.remove();
Queue<Integer> temp = new LinkedList<Integer>();
temp = Q1;
Q1 = Q2;
Q2 = temp;
return element;
}
}
}
Here is very simple solution which uses one Queue and gives functionality like Stack.
public class CustomStack<T>
{
Queue<T> que = new Queue<T>();
public void push(T t) // STACK = LIFO / QUEUE = FIFO
{
if( que.Count == 0)
{
que.Enqueue(t);
}
else
{
que.Enqueue(t);
for (int i = 0; i < que.Count-1; i++)
{
var data = que.Dequeue();
que.Enqueue(data);
}
}
}
public void pop()
{
Console.WriteLine("\nStack Implementation:");
foreach (var item in que)
{
Console.Write("\n" + item.ToString() + "\t");
}
var data = que.Dequeue();
Console.Write("\n Dequeing :" + data);
}
public void top()
{
Console.Write("\n Top :" + que.Peek());
}
}
So in the above class named "CustomStack" what I am doing is just checking the queue for empty , if empty then insert one and from then on wards insert and then remove insert. By this logic first will come last. Example : In queue I inserted 1 and now trying to insert 2. Second time remove 1 and again insert so it becomes in reverse order.
Thank you.
queue<int> q1, q2;
int i = 0;
void push(int v) {
if( q1.empty() && q2.empty() ) {
q1.push(v);
i = 0;
}
else {
if( i == 0 ) {
while( !q1.empty() ) q2.push(q1.pop());
q1.push(v);
i = 1-i;
}
else {
while( !q2.empty() ) q1.push(q2.pop());
q2.push(v);
i = 1-i;
}
}
}
int pop() {
if( q1.empty() && q2.empty() ) return -1;
if( i == 1 ) {
if( !q1.empty() )
return q1.pop();
else if( !q2.empty() )
return q2.pop();
}
else {
if( !q2.empty() )
return q2.pop();
else if( !q1.empty() )
return q1.pop();
}
}
Let S1 and S2 be the two Stacks to be used in the implementation of queues.
struct Stack
{ struct Queue *Q1;
struct Queue *Q2;
}
We make sure that one queue is empty always.
Push operation : Whichever queue is not empty, insert the element in it.
Push (struct Stack *S, int data)
{
if(isEmptyQueue(S->Q1)
EnQueue(S->Q2, data);
else EnQueue(S->Q1, data);
}
Time Complexity: O(1)
Pop Operation: Transfer n-1 elements to other queue and delete last from queue for performing pop operation.
`
int Pop(struct Stack *S){
int i, size;
if(IsEmptyQueue(S->Q2))
{
size=size(S->Q1);
i=0;
while(i<size-1)
{ EnQueue(S->Q2, Dequeue(S->Q1)) ;
i++;
}
return DeQueue(S->Q1);
}
else{
size=size(S->Q2);
while(i<size-1)
EnQueue(S->Q1, Dequeue(S->Q2)) ;
i++;
}
return DeQueue(S->Q2);
} }
Time Complexity: Running Time of pop Operation is O(n) as each time pop is called, we are transferring all the elements from one queue to oter.
import java.util.LinkedList;
import java.util.Queue;
class MyStack {
Queue<Integer> queue1 = new LinkedList<Integer>();
Queue<Integer> queue2 = new LinkedList<Integer>();
// Push element x onto stack.
public void push(int x) {
if(isEmpty()){
queue1.offer(x);
}else{
if(queue1.size()>0){
queue2.offer(x);
int size = queue1.size();
while(size>0){
queue2.offer(queue1.poll());
size--;
}
}else if(queue2.size()>0){
queue1.offer(x);
int size = queue2.size();
while(size>0){
queue1.offer(queue2.poll());
size--;
}
}
}
}
// Removes the element on top of the stack.
public void pop() {
if(queue1.size()>0){
queue1.poll();
}else if(queue2.size()>0){
queue2.poll();
}
}
// Get the top element. You can make it more perfect just example
public int top() {
if(queue1.size()>0){
return queue1.peek();
}else if(queue2.size()>0){
return queue2.peek();
}
return 0;
}
// Return whether the stack is empty.
public boolean isEmpty() {
return queue1.isEmpty() && queue2.isEmpty();
}
}