Platinum Partner

Queue using 2 Stacks - C#

Queue using 2 Stacks - C#
public class QueueUsing2Stacks
        {
            private Stack _stack1 = new Stack();
            private Stack _stack2 = new Stack();

            public QueueUsing2Stacks() { }

            public void Enqueue(int data)
            {
               //Pop ALL existing elements from stack1, push them onto stack2
                int _stack1Count = _stack1.Count;
                for (int i = 0; i < _stack1Count; i++)
                {
                    _stack2.Push(_stack1.Pop());
                }
                //Push new data onto stack1
                _stack1.Push(data);

                int _stack2Count = _stack2.Count;
                //Pop ALL elements from stack2 and Push them back onto stack1
                for (int i = 0; i < _stack2Count; i++)
                {
                    _stack1.Push(_stack2.Pop());
                }
            }


            public int Dequeue()
            {
                if (_stack1.Count == 0)
                    throw new Exception("Queue is Empty");

                return _stack1.Pop();
            }
        }
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}