Implement Queue using Stacks

Run Settings
LanguageC#
Language Version
Run Command
using System; using System.Collections.Generic; class MainClass { public static void Main() { MyQueue q = new MyQueue(); q.Push(1); q.Push(2); //Console.WriteLine(q.Peek()); //Console.WriteLine(q.Pop()); //Console.WriteLine(q.Empty()); //while (!q.Empty()) //{ Console.Write($"{q.Pop()} --> "); } } } public class MyQueue { private Stack<int> queueStack; private Stack<int> helperStack; public MyQueue() { queueStack = new Stack<int>(); helperStack = new Stack<int>(); } public void Push(int x) { queueStack.Push(x); //push to front of queue - fifo } public int Pop() { Peek(); //does all the organising return helperStack.Pop(); } public int Peek() { if (helperStack.Count != 0) { return helperStack.Peek(); } while(queueStack.Count != 0) { helperStack.Push(queueStack.Pop()); } return helperStack.Peek(); //need to peek at bottom of queue stack as that's the front of the queue } public bool Empty() { return queueStack.Count == 0 && helperStack.Count == 0; } }
Editor Settings
Theme
Key bindings
Full width
Lines