public void first_test() { var queue = new TwoStackQueue <Item>(); queue.Count.Should().Be(0); var items = Enumerable .Range(0, 10) .Select(x => new Item { Index = x }) .ToList(); foreach (var item in items) { 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")); }
public void TwoStackQueueTest() { var q = new TwoStackQueue <int>(); q.Enqueue(1); q.Enqueue(2); Assert.IsTrue(q.Count == 2); int i = q.Dequeue(); Assert.IsTrue(i == 1); Assert.IsTrue(q.Count == 1); i = q.Dequeue(); Assert.IsTrue(i == 2); Assert.IsTrue(q.Count == 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()); } } }
//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; } } }
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; } } }