private static void RunStackTest() { Console.WriteLine("Start"); Console.WriteLine("Push 1"); var stack = new Stack(1); Console.WriteLine("Push 2"); stack.Push(2); Console.WriteLine("Push 3"); stack.Push(3); Console.WriteLine("Push 4"); stack.Push(4); Console.WriteLine("Pop " + stack.Pop()); Console.WriteLine("Pop " + stack.Pop()); Console.WriteLine("Pop " + stack.Pop()); Console.WriteLine("Pop " + stack.Pop()); Console.WriteLine("Pop ??????? expected Exception"); try { stack.Pop(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Push 5 on Empty stack"); stack.Push(5); Console.WriteLine("Pop " + stack.Pop()); }
//DFS public static bool ExistsPath(Graph<int> graph, int start, int destination) { var nodes = graph.Nodes; foreach (var node in nodes) { node.NodeState=State.Unvisted; } var discovered = new Stack<Node<int>>(); var currentNode = graph.FindNode(start); currentNode.NodeState = State.Visited; discovered.Push(currentNode); while (discovered.Count>0) { currentNode = discovered.Pop(); foreach (var node in currentNode.Neighbors) { if (node.NodeState.Equals(State.Unvisted)) { node.NodeState=State.Visited; discovered.Push(node); } } } return graph.FindNode(destination).NodeState.Equals(State.Visited); }
public void Adding_Items_In_Descending_Order_Sets_Minimum_To_Last_Value() { DataStructures.Stack stack = new DataStructures.Stack(); stack.Push(5); stack.Push(4); stack.Push(3); stack.Push(2); stack.Push(1); Assert.AreEqual(1, stack.GetMinimumItem()); }
public void Adding_Items_In_Descending_Order_Then_Popping_Sets_Minimum_To_Previous_Minimum_Value() { DataStructures.Stack stack = new DataStructures.Stack(); stack.Push(5); stack.Push(4); stack.Push(3); stack.Push(2); stack.Push(1); stack.Pop(); Assert.AreEqual(2, stack.GetMinimumItem()); }
public void MinStack_Count_Equals_Stack_Count_For_One_Million_Items() { DataStructures.Stack stack = new DataStructures.Stack(); for (int i = 0; i < 1000000; i++) { stack.Push(i); } Assert.AreEqual(1000000, stack.GetMinStackCount()); }
//--------------------------------------------------------------------- //------------- D F S + I T E R A T I V E ------------------- //--------------------------------------------------------------------- //DFS: 先序 Pre-order Traversal, Stack public List<int> PreOrderTraversal_Iterative1(TreeNode root) { List<int> result = new List<int>(); Stack<TreeNode> stack = new Stack<TreeNode>(); stack.Push(root); while (stack.Count != 0) { root = stack.Pop(); if (root != null) { result.Add(root.val); //Prevent pushing null node to enhance performance. if (root.right != null) stack.Push(root.right); if (root.left != null) stack.Push(root.left); } } return result; }
public void GetMinimum_Runs_In_Constant_Time_For_One_Million_Items() { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); DataStructures.Stack stack = new DataStructures.Stack(); for (int i = 0; i <= 1000000; i++) { stack.Push(i); } watch.Start(); int result = stack.GetMinimumItem(); watch.Stop(); Assert.AreEqual(0, watch.ElapsedMilliseconds); }
//DFS: 先序 Pre-order Traversal, Stack public List<int> PreOrderTraversal_Iterative2(TreeNode root) { List<int> result = new List<int>(); Stack<TreeNode> stack = new Stack<TreeNode>(); while (root != null || stack.Count != 0) { if (root != null) { //Store root value first. result.Add(root.val); //Push right child into stack stack.Push(root.right); //Iterate left child first. root = root.left; } else { //if left child does not exist anymore, iterate the nearest right child. root = stack.Pop(); } } return result; }
//DFS: 后序 Post-order Traversal, (Reversed Pre-order Traversal) public List<int> PostOrderTraversal_Iterative2(TreeNode root) { List<int> result = new List<int>(); //Iterative: Reverse Pre-Order Traversal. Stack<TreeNode> stack = new Stack<TreeNode>(); while (root != null || stack.Count != 0) { if (root != null) { result.Add(root.val); if (root.left != null) stack.Push(root.left); root = root.right; } else { root = stack.Pop(); } } result.Reverse(); return result; }
//DFS: 后序 Post-order Traversal, Stack public List<int> PostOrderTraversal_Iterative1(TreeNode root) { //Postorder traversal is more complicated to implement comparing to Preorder and Inorder. //At what point shall we visit the root node: //Condition 1: The root node has no child at all. //Condition 2: The root's child has already been visited. List<int> result = new List<int>(); Stack<TreeNode> stack = new Stack<TreeNode>(); if (root != null) stack.Push(root); TreeNode prev = null; while (stack.Count != 0) { root = stack.Peek(); bool noChild = (root.left == null && root.right == null); bool doneChild = false; if (prev != null && (prev == root.left || prev == root.right)) doneChild = true; //直到没有child了或者child已经访问完了才把栈顶元素出栈 if (noChild || doneChild) { root = stack.Pop(); result.Add(root.val); prev = root; } else { if (root.right != null) stack.Push(root.right); if (root.left != null) stack.Push(root.left); } } return result; }
//DFS: 中序 In-order Traversal, Stack public List<int> InOrderTraversal_Iterative1(TreeNode root) { //Step 1: Iterate left subtree, push left node into stack. //Step 2: Stop push until node is null (its parent has no left child). //Step 3: Pop current node, store the value. //Step 4: Switch to the right subtree and iterate right subtree, List<int> result = new List<int>(); Stack<TreeNode> stack = new Stack<TreeNode>(); //Stop loop when both root is null and stack is empty. while (root != null || stack.Count != 0) { if (root != null) { //Store root for later use stack.Push(root); //Iterate left child first. root = root.left; } else { //If left child does not exist anymore, extract its parent root = stack.Pop(); //And store parent value result.Add(root.val); //Then iterate its right child. root = root.right; } } return result; }
public void Adding_An_Item_To_Stack_Changes_Minimum_Count() { DataStructures.Stack stack = new DataStructures.Stack(); stack.Push(2); Assert.AreEqual(2, stack.GetMinimumItem()); }
public int solution(string S) { int minInteger = 0; int maxInteger = (1 << 20) - 1; Stack<int> stack = new Stack<int>(); foreach (string op in S.Split(' ')) { if (op == "DUP") { if (!stack.Any()) { return -1; } stack.Push(stack.Peek()); } else if (op == "POP") { if (!stack.Any()) { return -1; } stack.Pop(); } else if (op == "+") { if (stack.Count() < 2) { return -1; } var top = stack.Pop(); var nextTop = stack.Pop(); var sum = top + nextTop; if (sum > maxInteger) { // Overflow return -1; } stack.Push(sum); } else if (op == "-") { if (stack.Count() < 2) { return -1; } var top = stack.Pop(); var nextTop = stack.Pop(); var diff = top - nextTop; if (diff < minInteger) { // Overflow return -1; } stack.Push(diff); } else { int number; if (!int.TryParse(op, out number)) { return -1; } if (number < minInteger || number > maxInteger) { return -1; } stack.Push(number); } } if (!stack.Any()) { return -1; } return stack.Pop(); }