From 74bbee41a63ec289a99f8bb72d6b59e0b09e6f8a Mon Sep 17 00:00:00 2001 From: FarazRashid Date: Fri, 20 Oct 2023 17:00:16 +0500 Subject: [PATCH 1/2] Implemented the circular queue The user inputs max size of array in main function and then an example of how to use the code has been provided --- .vscode/launch.json | 4 +- C++/CircularQueue.cpp | 119 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 C++/CircularQueue.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json index 2f014ea..ad1f061 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,8 +8,8 @@ "args": [], "stopAtEntry": false, "externalConsole": true, - "cwd": ".", - "program": "build/Debug/outDebug", + "cwd": "d:/Github/Hacktoberfest-2023/C++", + "program": "d:/Github/Hacktoberfest-2023/C++/build/Debug/outDebug", "MIMode": "gdb", "miDebuggerPath": "gdb", "setupCommands": [ diff --git a/C++/CircularQueue.cpp b/C++/CircularQueue.cpp new file mode 100644 index 0000000..59b0aa4 --- /dev/null +++ b/C++/CircularQueue.cpp @@ -0,0 +1,119 @@ +#include +using namespace std; + +#define MAX_SIZE 5 + +class CircularQueue { +private: + int* arr; + int maxSize; + int front, rear; + +public: + // Constructor initializes front and rear to -1 indicating an empty queue + CircularQueue(int size) : maxSize(size), front(-1), rear(-1) { + arr = new int[maxSize]; + } + + // Function to check if the queue is empty + bool isEmpty() { + return (front == -1 && rear == -1); + } + + // Function to check if the queue is full + bool isFull() { + return (front == (rear + 1) % MAX_SIZE); + } + + // Function to enqueue an element to the queue + void enqueue(int item) { + if (isFull()) { + cout << "Queue is full. Cannot enqueue element." << endl; + } else { + // If the queue is empty, set front and rear to 0 + if (isEmpty()) { + front = rear = 0; + } else { + // Circularly increment rear index + rear = (rear + 1) % MAX_SIZE; + } + arr[rear] = item; + cout << item << " enqueued to the queue." << endl; + } + } + + // Function to dequeue an element from the queue + void dequeue() { + if (isEmpty()) { + cout << "Queue is empty. Cannot dequeue element." << endl; + } else { + int removedItem = arr[front]; + // If there is only one element in the queue, reset front and rear to -1 + if (front == rear) { + front = rear = -1; + } else { + // Circularly increment front index + front = (front + 1) % MAX_SIZE; + } + cout << removedItem << " dequeued from the queue." << endl; + } + } + + // Function to display the elements in the queue + void display() { + if (isEmpty()) { + cout << "Queue is empty." << endl; + } else { + int i = front; + cout << "Queue: "; + // Loop through the queue elements using circular index calculation + do { + cout << arr[i] << " "; + i = (i + 1) % MAX_SIZE; + } while (i != (rear + 1) % MAX_SIZE); + cout << endl; + } + } + + // Destructor to release dynamically allocated memory + ~CircularQueue() { + delete[] arr; + } + +}; + +// Driver program +int main() { + int maxSize; + cout << "Enter the maximum size of the circular queue: "; + cin >> maxSize; + + CircularQueue queue(maxSize); + + // Enqueue elements to the queue + queue.enqueue(1); + queue.enqueue(2); + queue.enqueue(3); + queue.display(); // Queue: 1 2 3 + + // Dequeue an element and display the queue + queue.dequeue(); + queue.display(); // Queue: 2 3 + + // Enqueue more elements + queue.enqueue(4); + queue.enqueue(5); + queue.display(); // Queue: 2 3 4 5 + + // Attempt to enqueue when the queue is full + queue.enqueue(6); // Queue is full, cannot enqueue 6 + + // Dequeue all elements from the queue + queue.dequeue(); + queue.dequeue(); + queue.dequeue(); + queue.dequeue(); + queue.dequeue(); // Queue is empty, cannot dequeue further + + return 0; +} From ad7a75df5b2200dc5b27b6be6c44b8c7f311498a Mon Sep 17 00:00:00 2001 From: Md Samer Ansari Date: Sat, 21 Oct 2023 23:05:04 +0530 Subject: [PATCH 2/2] Delete .vscode/launch.json Signed-off-by: Md Samer Ansari --- .vscode/launch.json | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index ad1f061..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "C/C++ Runner: Debug Session", - "type": "cppdbg", - "request": "launch", - "args": [], - "stopAtEntry": false, - "externalConsole": true, - "cwd": "d:/Github/Hacktoberfest-2023/C++", - "program": "d:/Github/Hacktoberfest-2023/C++/build/Debug/outDebug", - "MIMode": "gdb", - "miDebuggerPath": "gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ] - } - ] -} \ No newline at end of file