public void ExercisePushPopPeak() { var s = new StackViaLinkedList <int>(); s.Push(5); Assert.AreEqual(5, s.Peak()); s.Push(45); s.Push(-34); Assert.AreEqual(-34, s.Peak()); Assert.AreEqual(-34, s.Pop()); Assert.AreEqual(45, s.Peak()); Assert.AreEqual(45, s.Pop()); Assert.AreEqual(5, s.Peak()); }
public static string InOrderTraversalReturnAsString(BinaryTreeNode <int> treeNode) { StringBuilder returnString = new StringBuilder(); StackViaLinkedList <BinaryTreeNode <int> > st = new StackViaLinkedList <BinaryTreeNode <int> >(); st.Push(treeNode); BinaryTreeNode <int> currentNode = treeNode; bool shouldCheckLeft = true; while (!st.IsEmpty()) { while (currentNode.Left != null && shouldCheckLeft) { st.Push(currentNode.Left); currentNode = currentNode.Left; } currentNode = st.Pop().Data; shouldCheckLeft = false; returnString.Append(currentNode.Data + " "); if (currentNode.Right != null) { st.Push(currentNode.Right); currentNode = currentNode.Right; shouldCheckLeft = true; } } return(returnString.ToString()); }
public static void InOrderTraversalIterative(BinarySearchTreeNode <int> treeNode) { StackViaLinkedList <BinarySearchTreeNode <int> > st = new StackViaLinkedList <BinarySearchTreeNode <int> >(); st.Push(treeNode); BinarySearchTreeNode <int> currentNode = treeNode; bool shouldCheckLeft = true; while (!st.IsEmpty()) { while (currentNode.Left != null && shouldCheckLeft) { st.Push(currentNode.Left); currentNode = currentNode.Left; } currentNode = st.Pop().Data; shouldCheckLeft = false; Console.Write(currentNode.Data + " "); if (currentNode.Right != null) { st.Push(currentNode.Right); currentNode = currentNode.Right; shouldCheckLeft = true; } } }
public void EmptyFlagIsCorectAfterStackBecomesEmpty() { var s = new StackViaLinkedList <int>(); s.Push(7); s.Pop(); Assert.True(s.IsEmpty()); }
public void Pop_Test() { IStack <char> stack = new StackViaLinkedList <char>(); stack.Push('A'); stack.Push('B'); stack.Push('C'); stack.Push('D'); stack.Push('E'); stack.IsEmpty.Should().BeFalse(); stack.Count.Should().Be(5); stack.Pop().Should().Be('E'); stack.Count.Should().Be(4); stack.Pop().Should().Be('D'); stack.Count.Should().Be(3); stack.Pop().Should().Be('C'); stack.Count.Should().Be(2); stack.Pop().Should().Be('B'); stack.Count.Should().Be(1); stack.Pop().Should().Be('A'); stack.Count.Should().Be(0); Action act = () => stack.Pop(); act.Should().Throw <IndexOutOfRangeException>() .WithMessage("Stack is empty."); }
static void Main(string[] args) { StackViaLinkedList <int> viaLinkedList = new StackViaLinkedList <int>(); int i = 7; viaLinkedList.Push(i); viaLinkedList.Push(i * 2); viaLinkedList.Push(i * 3); Console.WriteLine(viaLinkedList.Peek()); Console.WriteLine(viaLinkedList.Pop()); }
public void reverseStringUsingStack() { string str = "abcdefghijklmn"; var s = new StackViaLinkedList <char>(); for (int i = 0; i < str.Length; ++i) { s.Push(str[i]); } string reversed_string = ""; for (int i = 0; i < str.Length; ++i) { reversed_string += s.Pop(); } Assert.AreEqual("nmlkjihgfedcba", reversed_string); }
/// <summary> /// Algo1: 1. Keep a stack and every time a char(other than < or >) is encountered, make stack.top.child = newelement /// Push this new element into the stack. /// 2. When a < is encountered, do nothing /// 3. When a > is encountered, do a pop() /// </summary> /// <param name="expression"></param> /// <returns></returns> private static TreeNode <char> CreateTreeFromExpression(char[] expression) { StackViaLinkedList <TreeNode <char> > stackForTree = new StackViaLinkedList <TreeNode <char> >(); TreeNode <char> headOfTree = null; for (int expIndex = 0; expIndex < expression.Length; expIndex++) { if (expression[expIndex] == '<' || expression[expIndex] == ' ') { // No-op } else if (expression[expIndex] == '>') { SingleLinkedListNode <TreeNode <char> > top = stackForTree.Pop(); if (top == null) { throw new Exception("The expression is not well formed"); } } else { SingleLinkedListNode <TreeNode <char> > top = stackForTree.Peek(); TreeNode <char> currentNode = new TreeNode <char>(expression[expIndex]); if (top == null) { headOfTree = currentNode; } else { top.Data.Children.Add(currentNode); } stackForTree.Push(currentNode); } } // After this step the stack should be empty for a well formed expression if (stackForTree.Top != null) { throw new Exception("The expression is not well formed"); } return(headOfTree); }
public void ExceptionThrownOnPopFromEmptyStack() { var s = new StackViaLinkedList <int>(); Assert.Throws <IndexOutOfRangeException>(() => s.Pop()); }
public void Move(StackViaLinkedList <int> source, StackViaLinkedList <int> destination) { destination.Push(source.Pop()); }