コード例 #1
0
        public void Push(int data)
        {
            mainStack.Push(data);
            SingleLinkedListNode <int> minStackTopNode = minStack.Peek();

            if (minStackTopNode == null || minStackTopNode.Data >= data)
            {
                minStack.Push(data);
            }
        }
コード例 #2
0
ファイル: SortAStack.cs プロジェクト: jacobkurien1/Algorithms
        public static StackViaLinkedList <int> SortAStackAscendingOrDescending(StackViaLinkedList <int> unsortedStack, SortingOrder sortOrder)
        {
            StackViaLinkedList <int> sortedStack = new StackViaLinkedList <int>(); // This will be the sorted stack

            while (unsortedStack.Peek() != null)
            {
                SingleLinkedListNode <int> currentNode = unsortedStack.Pop();
                // if the current node from unsorted stack is greater than the top of the sorted stack
                // then keep popping the sorted stack till this condition holds true
                // Here we are finding the final position where the current element should be placed in sorted stack
                while (sortedStack.Peek() != null && ((sortOrder == SortingOrder.Ascending) ? sortedStack.Peek().Data <currentNode.Data : sortedStack.Peek().Data> currentNode.Data))
                {
                    // We will push the popped elements from sorted stack to unsorted stack
                    unsortedStack.Push(sortedStack.Pop());
                }
                sortedStack.Push(currentNode);
            }

            return(sortedStack);
        }