Example #1
0
        public void Sort(Stack <int> stack)
        {
            var r = new Stack <int>();

            while (!stack.IsEmpty())
            {
                var t = stack.Pop();
                while (!r.IsEmpty() && t < r.Peek())
                {
                    stack.Push(r.Pop());
                }
                r.Push(t);
            }

            while (!r.IsEmpty())
            {
                stack.Push(r.Pop());
            }
        }
Example #2
0
        public static Stack <T> SortStackAlgo(Stack <T> stackToBeSorted)
        {
            var tempStack = new Stack <T>();

            while (!stackToBeSorted.IsEmpty())
            {
                var popedItem = stackToBeSorted.Pop();
                while (!tempStack.IsEmpty() && (popedItem.CompareTo(tempStack.Peek()) < 0))
                {
                    stackToBeSorted.Push(tempStack.Pop());
                }
                tempStack.Push(popedItem);
            }

            while (!tempStack.IsEmpty())
            {
                stackToBeSorted.Push(tempStack.Pop());
            }
            return(stackToBeSorted);
        }
Example #3
0
        static void Main(string[] args)
        {
            //Stack Operations
            Stack stack = new Stack();

            stack.Push(70);
            stack.Push(30);
            stack.Push(56);
            stack.Display();
            stack.Peek();
            stack.Pop();
            stack.IsEmpty();
            stack.Display();
            //Queue Operations
            Queue linkedListQueue = new Queue();

            linkedListQueue.Enqueue(56);
            linkedListQueue.Enqueue(30);
            linkedListQueue.Enqueue(70);
            linkedListQueue.Dequeue();
            linkedListQueue.Display();
        }