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
        public void Run()
        {
            MyQueue <int> myQueue = new ArrayQueue <int>(5);

            myQueue.EnQueue(1);
            myQueue.EnQueue(2);
            myQueue.EnQueue(3);
            myQueue.EnQueue(4);
            myQueue.EnQueue(5);

            while (!myQueue.IsEmpty())
            {
                Console.WriteLine("Item:" + myQueue.DeQueue());
            }
            Console.WriteLine("-------");

            MyQueue <int> sllQueue = new SllQueue <int>();

            sllQueue.EnQueue(1);
            sllQueue.EnQueue(2);
            sllQueue.EnQueue(3);
            sllQueue.EnQueue(4);
            sllQueue.EnQueue(5);
            sllQueue.EnQueue(6);

            while (!sllQueue.IsEmpty())
            {
                Console.WriteLine("Item:" + sllQueue.DeQueue());
            }
            Console.WriteLine("-------");

            MyQueue <int> crQueue = new CircularArrayQueue <int>(5);

            crQueue.EnQueue(1);
            crQueue.EnQueue(2);
            crQueue.EnQueue(3);
            crQueue.EnQueue(4);
            crQueue.DeQueue();
            crQueue.EnQueue(5);
            crQueue.DeQueue();
            crQueue.EnQueue(6);

            while (!crQueue.IsEmpty())
            {
                Console.WriteLine("Item:" + crQueue.DeQueue());
            }
            Console.WriteLine("-------");

            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            ArrayQueue <string> queue = new ArrayQueue <string>(2);

            queue.EnQueue("Miguel");
            Console.WriteLine(queue);

            Console.WriteLine($"Head: {queue.Head}\nTail: {queue.Tail}");

            queue.EnQueue("Roger");
            Console.WriteLine(queue);

            Console.WriteLine($"Head: {queue.Head}\nTail: {queue.Tail}");

            queue.EnQueue("Sergio");
            Console.WriteLine(queue);

            Console.WriteLine($"Head: {queue.Head}\nTail: {queue.Tail}");

            queue.DeQueue();
            Console.WriteLine(queue);

            Console.WriteLine($"Head: {queue.Head}\nTail: {queue.Tail}");

            queue.DeQueue();
            Console.WriteLine(queue);

            Console.WriteLine($"Head: {queue.Head}\nTail: {queue.Tail}");

            queue.DeQueue();
            Console.WriteLine(queue);

            queue.EnQueue("Miguel", "Roger", "Tostado");
            Console.WriteLine(queue);

            Console.WriteLine($"Head: {queue.Head}\nTail: {queue.Tail}");

            queue.EnQueue("Luis", "Sergio");
            Console.WriteLine(queue);

            Console.WriteLine($"Head: {queue.Head}\nTail: {queue.Tail}");

            Console.WriteLine($"Dequeing elements: {queue.DeQueue(5)}");
            Console.WriteLine(queue);

            Console.ReadKey();
        }
Example #4
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 #5
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
        static void Main(string[] args)
        {
            ArrayQueue obj = new ArrayQueue(5);

            obj.enqueue(10);
            obj.enqueue(20);
            obj.enqueue(30);

            var item = obj.dequeue();

            Console.WriteLine("Item removed was: " + item);
            Console.WriteLine(obj.ToString());

            obj.enqueue(40);
            obj.enqueue(50);

            var item1 = obj.dequeue();

            Console.WriteLine("Item removed was: " + item1);
            Console.WriteLine(obj.ToString());

            var item2 = obj.dequeue();

            Console.WriteLine("Item removed was: " + item2);
            Console.WriteLine(obj.ToString());

            obj.enqueue(60);
            obj.enqueue(70);
            obj.enqueue(80);

            //obj.enqueue(90);

            Console.WriteLine(obj.ToString());



            //------------------------------------//

            QueueUsingTwoStack queue = new QueueUsingTwoStack();

            queue.enqueue(200);
            queue.enqueue(300);
            queue.enqueue(400);

            var first = queue.dequeue();

            Console.WriteLine("Item first removed from QueueWithTwoStack is " + first);

            var second = queue.dequeue();

            Console.WriteLine("Item second removed from QueueWithTwoStack is " + second);

            var third = queue.dequeue();

            Console.WriteLine("Item third removed from QueueWithTwoStack is " + third);

            //var fourth = queue.dequeue();
            //Console.WriteLine("Item first removed from QueueWithTwoStack is " + fourth);


            //--------------------------------------/
            PriorityQueue pq = new PriorityQueue(6);

            pq.addItems(5);
            pq.addItems(3);
            pq.addItems(6);
            pq.addItems(8);
            pq.addItems(2);
            pq.addItems(4);

            var pq1 = pq.remove();

            Console.WriteLine("Item removed from PriorityQueue is : " + pq1.ToString());

            var pq2 = pq.remove();

            Console.WriteLine("Item removed from PriorityQueue is : " + pq2.ToString());

            Console.WriteLine("Values in PriorityQueue are : " + pq.ToString());


            //------------------------------------------//
            QueueReverser qr = new QueueReverser();

            qr.add(1);
            qr.add(2);
            qr.add(3);
            qr.add(4);
            qr.add(5);

            qr.ReverseTillK(3);

            Console.WriteLine(qr.ToString());

            Console.ReadLine();
        }