public SingleLinkedListNode <int> Pop()
        {
            SingleLinkedListNode <int> retVal          = mainStack.Pop();
            SingleLinkedListNode <int> minStackTopNode = minStack.Peek();

            if (minStackTopNode != null && retVal != null && minStackTopNode.Data == retVal.Data)
            {
                minStack.Pop();
            }
            return(retVal);
        }
Exemplo n.º 2
0
        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);
        }