Пример #1
0
        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.");
        }
Пример #2
0
        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());
        }
Пример #3
0
        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;
                }
            }
        }
Пример #4
0
        public void Push_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);
        }
        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());
        }
Пример #6
0
        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());
        }
Пример #7
0
        public void NonEmptyStackHasCorrectEmptyFlag()
        {
            var s = new StackViaLinkedList <int>();

            s.Push(34);
            Assert.False(s.IsEmpty());
        }
Пример #8
0
        public void EmptyFlagIsCorectAfterStackBecomesEmpty()
        {
            var s = new StackViaLinkedList <int>();

            s.Push(7);
            s.Pop();
            Assert.True(s.IsEmpty());
        }
Пример #9
0
 public TowerOfHanoi(int numOfPlanks)
 {
     numberOfPanksOnTower = numOfPlanks;
     towerA = new StackViaLinkedList <int>();
     for (int i = numberOfPanksOnTower; i >= 1; i--)
     {
         towerA.Push(i);
     }
     towerB = new StackViaLinkedList <int>();
     towerC = new StackViaLinkedList <int>();
 }
Пример #10
0
        public static StackViaLinkedList <int> CreateAStack(int num)
        {
            // Create the stack
            StackViaLinkedList <int> stack = new StackViaLinkedList <int>();

            // Populate the unsorted stack
            SingleLinkedListNode <int> node = LinkedListHelper.CreateSinglyLinkedList(num);

            while (node != null)
            {
                stack.Push(node.Data);
                node = node.NextNode;
            }
            return(stack);
        }
Пример #11
0
        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);
        }
Пример #12
0
        /// <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);
        }
Пример #13
0
 public void Move(StackViaLinkedList <int> source, StackViaLinkedList <int> destination)
 {
     destination.Push(source.Pop());
 }