Advertisement
klippa

Queue by 2 Stacks

Aug 19th, 2022
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.43 KB | None | 0 0
  1. public class Queue<T> where T : class
  2. {
  3.     private Stack<T> input = new Stack<T>();
  4.     private Stack<T> output = new Stack<T>();
  5.  
  6.     public void Enqueue(T t)
  7.     {
  8.         input.Push(t);
  9.     }
  10.  
  11.     public T Dequeue()
  12.     {
  13.         if (output.Count == 0)
  14.         {
  15.             while (input.Count != 0)
  16.             {
  17.                 output.Push(input.Pop());
  18.             }
  19.         }
  20.  
  21.         return output.Pop();
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement