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
Can we just use one queue to implement a stack? I can use two queues, but considering single queue would be more efficient. Here is the code:
public void Push(T val)
{
queLower.Enqueue(val);
}
public T Pop()
{
if (queLower.Count == 0 )
{
Console.Write("Stack is empty!");
return default(T);
}
if (queLower.Count > 0)
{
for (int i = 0; i < queLower.Count - 1;i++ )
{
queLower.Enqueue(queLower.Dequeue ());
}
}
return queLower.Dequeue();
}