Exemplo n.º 1
0
        public void TestDequeue()
        {
            QueueWithTwoStacks myTwoStackQueue = new QueueWithTwoStacks();
            Node one   = new Node(1);
            Node two   = new Node(2);
            Node three = new Node(3);
            Node four  = new Node(4);
            Node five  = new Node(5);

            myTwoStackQueue.Enqueue(one);                             //first set first item added to stack
            myTwoStackQueue.Enqueue(two);
            myTwoStackQueue.Enqueue(three);                           //first set last item added to stack

            Assert.NotNull(myTwoStackQueue.EnqueueStack.Top);         //make sure items are in first stack to then switch to second stack
            Assert.Equal(myTwoStackQueue.EnqueueStack.Peak(), three); //make sure last input is first out of first stack
            Assert.Equal(myTwoStackQueue.Dequeue(), one);             //make sure first input is first output, meaning last to go into second stack and poped off.
            Assert.Equal(myTwoStackQueue.DequeueStack.Peak(), two);   // make sure second input is put to the top of second stack
            Assert.Null(myTwoStackQueue.EnqueueStack.Top);            //make sure after first dequue first stack is emptyed

            myTwoStackQueue.Enqueue(four);
            myTwoStackQueue.Enqueue(five);
            Assert.Equal(myTwoStackQueue.EnqueueStack.Peak(), five); //make sure first stack is getting new inputs
            Assert.Equal(myTwoStackQueue.DequeueStack.Peak(), two);  //make sure second stack still has left over values untill empty

            myTwoStackQueue.Dequeue();
            myTwoStackQueue.Dequeue();
            Assert.Null(myTwoStackQueue.DequeueStack.Peak()); //make sure after all first inputs are dequeue that second stack is empty
            Assert.NotNull(myTwoStackQueue.EnqueueStack.Top); //make sure valuse still excist in first stack

            myTwoStackQueue.Dequeue();
            Assert.Null(myTwoStackQueue.EnqueueStack.Top);           //make sure nothing is in first stack after second one is empty and dequeue is called again
            Assert.Equal(myTwoStackQueue.DequeueStack.Peak(), five); //make sure the last item is in second stack waiting to be next in line for output.
        }
Exemplo n.º 2
0
        public void Run()
        {
            var queue = new QueueWithTwoStacks();

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);
            queue.Enqueue(4);
            Console.WriteLine("Queue has 1,2,3,4");

            Console.WriteLine($"Dequeued: {queue.Dequeue()}");
            Console.WriteLine($"Dequeued: {queue.Dequeue()}");
        }
Exemplo n.º 3
0
        static void TestQueueWithTwoStacks()
        {
            QueueWithTwoStacks queue = new QueueWithTwoStacks(3);

            queue.Enqueue(10);
            queue.Enqueue(20);
            queue.Enqueue(30);
            //queue.Enqueue(40);

            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            Console.WriteLine(queue.Dequeue());
            //queue.Dequeue();
        }
Exemplo n.º 4
0
    static void Main(String[] args)
    {
        var stackQueue = new QueueWithTwoStacks <int>();
        int q          = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < q; i++)
        {
            string[] a_temp = Console.ReadLine().Split(' ');
            int      type   = Convert.ToInt32(a_temp[0]);
            if (type == 1)
            {
                int x = Convert.ToInt32(a_temp[1]);
                stackQueue.Enqueue(x);
            }
            else if (type == 2)
            {
                stackQueue.Dequeue();
            }
            else
            {
                Console.WriteLine(stackQueue.Peek());
            }
        }

        Console.ReadKey();
    }
Exemplo n.º 5
0
    static void Main(String[] args)
    {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        var stackQueue = new QueueWithTwoStacks <int>();
        int q          = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < q; i++)
        {
            string[] a_temp = Console.ReadLine().Split(' ');
            int      type   = Convert.ToInt32(a_temp[0]);
            if (type == 1)
            {
                int x = Convert.ToInt32(a_temp[1]);
                stackQueue.Enqueue(x);
            }
            else if (type == 2)
            {
                stackQueue.Dequeue();
            }
            else
            {
                Console.WriteLine(stackQueue.Peek());
            }
        }

        Console.ReadKey();
    }
Exemplo n.º 6
0
        public void QueueWithTwoStacksTest()
        {
            var queue = new QueueWithTwoStacks();

            queue.Enqueue(2);
            queue.Enqueue(3);
            queue.Enqueue(4);
            queue.Enqueue(5);
            Assert.Equal(4, queue.Count);
            Assert.Equal(2, queue.Peek());
            Assert.Equal(2, queue.Dequeue());
            Assert.Equal(3, queue.Dequeue());
            Assert.Equal(4, queue.Peek());
            Assert.Equal(2, queue.Count);
            queue.Enqueue(6);
            Assert.Equal(3, queue.Count);
            Assert.Equal(4, queue.Dequeue());
            Assert.Equal(5, queue.Dequeue());
            Assert.Equal(6, queue.Dequeue());
        }
Exemplo n.º 7
0
        public void TestDequeueOnEmpty()
        {
            QueueWithTwoStacks myTwoStackQueue   = new QueueWithTwoStacks();
            Exception          expectedException = null;

            try
            {
                myTwoStackQueue.Dequeue();
            }
            catch (NullReferenceException ex)
            {
                expectedException = ex;
            }



            Assert.NotNull(expectedException);//make sure we catch the exception when error
        }
Exemplo n.º 8
0
        static void DSQueueArrayMain(string[] args)
        {
            try
            {
                #region CustomQueueArray
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine("Implementing the circular Queue with Array");
                Console.ForegroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Pushing the 10, 20, 30, 40, 50 elements to Queue");
                Console.ForegroundColor = ConsoleColor.White;

                CustomQueueArray customQueueArray = new CustomQueueArray(5);
                customQueueArray.Enqueue(10);
                customQueueArray.Enqueue(20);
                customQueueArray.Enqueue(30);
                customQueueArray.Enqueue(40);
                customQueueArray.Enqueue(50);

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Printing the elements");
                Console.ForegroundColor = ConsoleColor.White;
                customQueueArray.Print();

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing the element from Queue");
                Console.ForegroundColor = ConsoleColor.White;
                int front = customQueueArray.Dequeue();
                Console.WriteLine("Removed Item is: {0}", front);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Printing the elements");
                Console.ForegroundColor = ConsoleColor.White;
                customQueueArray.Print();

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing one more element from Queue");
                Console.ForegroundColor = ConsoleColor.White;
                front = customQueueArray.Dequeue();
                Console.WriteLine("Removed Item is: {0}", front);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Printing the elements");
                Console.ForegroundColor = ConsoleColor.White;
                customQueueArray.Print();

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Adding new item as 11");
                Console.ForegroundColor = ConsoleColor.White;
                customQueueArray.Enqueue(11);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Printing the elements");
                Console.ForegroundColor = ConsoleColor.White;
                customQueueArray.Print();

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Adding new item as 22");
                Console.ForegroundColor = ConsoleColor.White;
                customQueueArray.Enqueue(22);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Printing the elements");
                Console.ForegroundColor = ConsoleColor.White;
                customQueueArray.Print();

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Peeking the element");
                Console.ForegroundColor = ConsoleColor.White;
                front = customQueueArray.Peek();
                Console.WriteLine("Peek item is: {0}", front);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine();
                #endregion

                #region ReverseQueue
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine("Reversing the Queue");
                Console.ForegroundColor = ConsoleColor.White;

                ReverseQueue reverseQueue = new ReverseQueue();

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Printing the elements of Queue before reversing");
                Console.ForegroundColor = ConsoleColor.White;
                reverseQueue.Print();

                reverseQueue.Reverse();

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Printing the elements of Queue after reversing");
                Console.ForegroundColor = ConsoleColor.White;
                reverseQueue.Print();
                #endregion

                #region QueueWithTwoStacks
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine("Implemeting Queue with two Stacks");
                Console.ForegroundColor = ConsoleColor.White;

                QueueWithTwoStacks queueWithTwoStacks = new QueueWithTwoStacks();

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Pushing the 10, 20, 30 elements to Queue");
                Console.ForegroundColor = ConsoleColor.White;
                queueWithTwoStacks.Enqueue(10);
                queueWithTwoStacks.Enqueue(20);
                queueWithTwoStacks.Enqueue(30);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing the element from Queue");
                Console.ForegroundColor = ConsoleColor.White;
                front = queueWithTwoStacks.Dequeue();
                Console.WriteLine("Removed Item is: {0}", front);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing one more element from Queue");
                Console.ForegroundColor = ConsoleColor.White;
                front = queueWithTwoStacks.Dequeue();
                Console.WriteLine("Removed Item is: {0}", front);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Adding element 40 to the Queue");
                Console.ForegroundColor = ConsoleColor.White;
                queueWithTwoStacks.Enqueue(40);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing the element from Queue");
                Console.ForegroundColor = ConsoleColor.White;
                front = queueWithTwoStacks.Dequeue();
                Console.WriteLine("Removed Item is: {0}", front);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing one more element from Queue");
                Console.ForegroundColor = ConsoleColor.White;
                front = queueWithTwoStacks.Dequeue();
                Console.WriteLine("Removed Item is: {0}", front);
                Console.WriteLine();
                #endregion

                #region QueueWithOneStacks
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Implemeting Queue with one Stack");

                QueueWithOneStack queueWithOneStack = new QueueWithOneStack();

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Pushing the 10, 20, 30 elements to Queue");
                Console.ForegroundColor = ConsoleColor.White;
                queueWithOneStack.Enqueue(10);
                queueWithOneStack.Enqueue(20);
                queueWithOneStack.Enqueue(30);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing the element from Queue");
                Console.ForegroundColor = ConsoleColor.White;
                front = queueWithOneStack.Dequeue();
                Console.WriteLine("Removed Item is: {0}", front);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing one more element from Queue");
                Console.ForegroundColor = ConsoleColor.White;
                front = queueWithOneStack.Dequeue();
                Console.WriteLine("Removed Item is: {0}", front);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Adding element 40 to the Queue");
                Console.ForegroundColor = ConsoleColor.White;
                queueWithOneStack.Enqueue(40);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing the element from Queue");
                Console.ForegroundColor = ConsoleColor.White;
                front = queueWithOneStack.Dequeue();
                Console.WriteLine("Removed Item is: {0}", front);

                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Removing one more element from Queue");
                Console.ForegroundColor = ConsoleColor.White;
                front = queueWithOneStack.Dequeue();
                Console.WriteLine("Removed Item is: {0}", front);
                Console.WriteLine();

                #endregion

                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ForegroundColor = ConsoleColor.White;
            }
        }