{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Circular Queue\n", "This code is heavily derived from [Circular Queue](https://www.geeksforgeeks.org/circular-queue-set-1-introduction-array-implementation/)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "using namespace std;\n", "struct Queue {\n", " // Initialize front and rear \n", " int rear, front;\n", "\n", " // Circular Queue \n", " int size;\n", " int *arr;\n", "\n", " Queue(int s) {\n", " front = rear = -1;\n", " size = s;\n", " arr = new int[s];\n", " }\n", "\n", " void enQueue(int value);\n", " int deQueue();\n", " void displayQueue();\n", "};" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "/* Function to create Circular queue */\n", "void Queue::enQueue(int value) {\n", " if ((front == 0 && rear == size - 1) ||\n", " (rear == (front - 1) % (size - 1))) {\n", " std::cout << \"Queue is Full\" << std::endl;\n", " return;\n", " } else if (front == -1) /* Insert First Element */\n", " {\n", " front = rear = 0;\n", " arr[rear] = value;\n", " } else if (rear == size - 1 && front != 0) {\n", " rear = 0;\n", " arr[rear] = value;\n", " } else {\n", " rear++;\n", " arr[rear] = value;\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "// Function to delete element from Circular Queue \n", "int Queue::deQueue() {\n", " if (front == -1) {\n", " std::cout << \"Queue is Empty\" << std::endl;\n", " return INT_MIN;\n", " }\n", "\n", " int data = arr[front];\n", " arr[front] = -1;\n", " if (front == rear) {\n", " front = -1;\n", " rear = -1;\n", " } else if (front == size - 1)\n", " front = 0;\n", " else\n", " front++;\n", "\n", " return data;\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "// Function displaying the elements \n", "// of Circular Queue \n", "void Queue::displayQueue() {\n", " if (front == -1) {\n", " std::cout << \"Queue is Empty\" << std::endl;\n", " return;\n", " }\n", " std::cout << \"Elements in Circular Queue are: \";\n", " if (rear >= front) {\n", " for (int i = front; i <= rear; i++)\n", " std::cout << arr[i] << \" \";\n", " } else {\n", " for (int i = front; i < size; i++)\n", " std::cout << arr[i] << \" \";\n", "\n", " for (int i = 0; i <= rear; i++)\n", " std::cout << arr[i] << \" \";\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Elements in Circular Queue are: 14 22 13 -6 " ] } ], "source": [ "Queue q(5);\n", "\n", "// Inserting elements in Circular Queue \n", "q.enQueue(14);\n", "q.enQueue(22);\n", "q.enQueue(13);\n", "q.enQueue(-6);\n", "\n", "q.displayQueue();" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14 22 " ] } ], "source": [ "// Deleting elements from Circular Queue\n", "std::cout << q.deQueue() << \" \";\n", "std::cout << q.deQueue() << \" \";" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Elements in Circular Queue are: 13 -6 " ] } ], "source": [ "q.displayQueue();" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Elements in Circular Queue are: 13 -6 9 20 5 " ] } ], "source": [ "q.enQueue(9);\n", "q.enQueue(20);\n", "q.enQueue(5);\n", "\n", "q.displayQueue(); " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Queue is Full\n" ] } ], "source": [ "q.enQueue(10); " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Elements in Circular Queue are: 13 -6 9 20 5 " ] } ], "source": [ "q.displayQueue();" ] } ], "metadata": { "kernelspec": { "display_name": "C++17", "language": "C++17", "name": "xeus-cling-cpp17" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "-std=c++17" } }, "nbformat": 4, "nbformat_minor": 2 }