Example #1
0
        static void Main(string[] args)
        {
            // FIFO - first in, first out
            var q = new Queue <int>();

            q.Enqueue(10);
            q.Enqueue(20);
            q.Enqueue(30);

            Reverse(q);

            var aq = new ArrayQueue(10);

            aq.Enqueue(10);
            aq.Enqueue(20);
            aq.Enqueue(30);

            Console.WriteLine(aq.Peek());

            aq.Dequeue();
            Console.WriteLine(aq.Peek());
            aq.Dequeue();
            Console.WriteLine(aq.Peek());
            aq.Dequeue();
        }
Example #2
0
        static void Main(string[] args)
        {
            var reverser = new QueueReverser();
            var input    = new Queue <int>();

            input.Enqueue(10);
            input.Enqueue(20);
            input.Enqueue(30);
            reverser.Reverse(input);
            reverser.Print(input);

            var arrayQueue = new ArrayQueue(5);

            arrayQueue.Enqueue(100);
            arrayQueue.Enqueue(200);
            arrayQueue.Enqueue(300);
            arrayQueue.Dequeue();
            arrayQueue.Enqueue(400);
            arrayQueue.Enqueue(500);
            arrayQueue.Enqueue(600);
            arrayQueue.Print();

            var stackQueue = new StackQueue();

            stackQueue.Enqueue(10);
            stackQueue.Enqueue(20);
            System.Console.WriteLine(stackQueue.Dequeue());
            System.Console.WriteLine(stackQueue.Dequeue());

            var priorityQueue = new PriorityQueue(5);

            priorityQueue.Enqueue(2);
            priorityQueue.Enqueue(5);
            priorityQueue.Enqueue(1);
            priorityQueue.Enqueue(3);
            priorityQueue.Enqueue(5);
            priorityQueue.Print();
            System.Console.WriteLine($"The first one to dequeue is {priorityQueue.Dequeue()}");
            System.Console.WriteLine($"The second one to dequeue is {priorityQueue.Dequeue()}");
        }
Example #3
0
        static void Main(string[] args)
        {
            // -------------------- Using Queues --------------------
            var queue = new Queue <int>();

            queue.Enqueue(10);
            queue.Enqueue(20);
            queue.Enqueue(30);
            Console.WriteLine($"[{string.Join(", ", queue)}]");

            var front = queue.Dequeue();

            Console.WriteLine(front);


            // -------------------- Exercise: Reversing a Queue --------------------
            Reverse(queue);
            Console.WriteLine($"[{string.Join(", ", queue)}]");


            // -------------------- Exercise: Building a Queue using an Array --------------------
            ArrayQueue aQueue = new ArrayQueue(5);

            aQueue.Enqueue(10);
            aQueue.Enqueue(20);
            aQueue.Enqueue(30);
            Console.WriteLine(aQueue);

            aQueue.Dequeue();
            var aFront = aQueue.Dequeue();

            Console.WriteLine(front);
            Console.WriteLine(aQueue);

            aQueue.Enqueue(40);
            aQueue.Enqueue(50);
            aQueue.Enqueue(60);
            aQueue.Enqueue(70);
            aQueue.Dequeue();
            aQueue.Enqueue(80);
            Console.WriteLine(aQueue);

            Console.WriteLine(aQueue.Peek());
            Console.WriteLine(aQueue);


            // -------------------- Exercise: Building a Queue using Two Stacks --------------------
            QueueWithTwoStacks sQueue = new QueueWithTwoStacks();

            sQueue.Enqueue(10);
            sQueue.Enqueue(20);
            sQueue.Enqueue(30);
            var sFront = sQueue.Dequeue();

            Console.WriteLine(sFront);


            // -------------------- Exercise: Create a Priority Queue with an Array --------------------
            PriorityQueue prQueue = new PriorityQueue();

            prQueue.Add(5);
            prQueue.Add(3);
            prQueue.Add(6);
            prQueue.Add(1);
            prQueue.Add(4);
            Console.WriteLine(prQueue);

            while (!prQueue.IsEmpty())
            {
                Console.WriteLine(prQueue.Remove());
            }


            // -------------------- Exercise: Reverse K Elements of a Queue --------------------
            Queue <int> reQueue = new Queue <int>();

            reQueue.Enqueue(10);
            reQueue.Enqueue(20);
            reQueue.Enqueue(30);
            reQueue.Enqueue(40);
            reQueue.Enqueue(50);
            QueueReverser.reverse(reQueue, 3);
            Console.WriteLine($"[{string.Join(", ", reQueue)}]");


            // -------------------- Exercise: Build a Queue with a Linked List from Scratch --------------------
            LinkedListQueue lQueue = new LinkedListQueue();

            lQueue.Enqueue(10);
            lQueue.Enqueue(20);
            lQueue.Enqueue(30);
            lQueue.Enqueue(40);
            lQueue.Enqueue(50);
            Console.WriteLine(lQueue);
            lQueue.Dequeue();
            Console.WriteLine(lQueue.Dequeue());
            Console.WriteLine(lQueue.Peek());
            Console.WriteLine(lQueue);
            Console.WriteLine(lQueue.Size());


            // -------------------- Exercise: Build a Queue with a Linked List from Scratch --------------------
            StackWithTwoQueues qStack = new StackWithTwoQueues();

            qStack.push(10);
            qStack.push(20);
            qStack.push(30);
            qStack.push(40);
            qStack.push(50);
            Console.WriteLine(qStack);
            qStack.pop();
            Console.WriteLine(qStack.pop());
            Console.WriteLine(qStack.peek());
            Console.WriteLine(qStack);
            Console.WriteLine(qStack.size());
        } // Main method