Example #1
0
        public void second_test()
        {
            var queue = new TwoStackQueue <Item>();

            queue.Count.Should().Be(0);

            var first = Enumerable
                        .Range(0, 5)
                        .Select(x => new Item {
                Index = x
            })
                        .ToList();

            foreach (var item in first)
            {
                queue.Enqueue(item);
            }

            queue.Count.Should().Be(5);

            var firstPeek = queue.Peek();

            firstPeek.Index.Should().Be(0);

            var second = Enumerable
                         .Range(5, 5)
                         .Select(x => new Item {
                Index = x
            })
                         .ToList();

            foreach (var item in second)
            {
                queue.Enqueue(item);
            }

            queue.Count.Should().Be(10);

            var peek = queue.Peek();

            peek.Index.Should().Be(0);

            for (var i = 0; i < 10; i++)
            {
                var current = queue.Dequeue();
                current.Index.Should().Be(i);
            }

            Action fail = () => queue.Dequeue();

            fail.Should().Throw <Exception>().Where(_ => _.Message.Contains("empty"));
        }
Example #2
0
    static void Main(String[] args)
    {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        int n = Int32.Parse(Console.ReadLine());
        TwoStackQueue <string> q = new TwoStackQueue <string>();

        for (int i = 0; i < n; i++)
        {
            string[] line  = Console.ReadLine().Split(' ');
            string   query = line[0];

            if (query == "1")
            {
                string value = line[1];
                q.Enqueue(value);
            }

            if (query == "2")
            {
                q.Dequeue();
            }

            if (query == "3")
            {
                Console.WriteLine(q.Peek());
            }
        }
    }
Example #3
0
 public void Test()
 {
     target.Add(1);
     target.Add(2);
     Assert.Equal(1, target.Peek());
     target.Add(3);
     Assert.Equal(1, target.Poll());
     target.Add(4);
     Assert.Equal(2, target.Poll());
 }
        //https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks
        public static void TwoStackQueue()
        {
            Console.WriteLine("https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks");
            int q     = Convert.ToInt32(Console.ReadLine());
            var queue = new TwoStackQueue <int>();

            for (int i = 0; i < q; i++)
            {
                string[] line   = Console.ReadLine().Split(' ');
                int[]    action = Array.ConvertAll(line, Int32.Parse);

                switch (action[0])
                {
                //Enqueue element x into the end of the queue
                case 1: {
                    queue.Enqueue(action[1]);
                    break;
                }

                //Dequeue the element at the front of the queue
                case 2: {
                    queue.Dequeue();
                    break;
                }

                //Print the element at the front of the queue
                case 3: {
                    Console.WriteLine(queue.Peek().ToString());
                    break;
                }

                default:
                    break;
                }
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            int numberOfQueries       = Convert.ToInt32(Console.ReadLine());
            TwoStackQueue <int> queue = new TwoStackQueue <int>();

            for (int i_query = 0; i_query < numberOfQueries; i_query++)
            {
                string[] queryParsed = Console.ReadLine().Split(' ');
                switch (Convert.ToInt32(queryParsed[0]))
                {
                case 1:
                    queue.Enqueue(Convert.ToInt32(queryParsed[1]));
                    break;

                case 2:
                    queue.Dequeue();
                    break;

                case 3:
                    Console.WriteLine(queue.Peek());
                    break;
                }
            }
        }