From b419d9e7323ce0d502b882a2cee9754085867f1b Mon Sep 17 00:00:00 2001 From: AndreaOrlando23 <64850221+AndreaOrlando23@users.noreply.github.com> Date: Mon, 4 Oct 2021 07:57:50 +0200 Subject: [PATCH 1/4] [FEAT] first version of queue solution in cpp --- .../solutions/aorlando/cpp-version/README.md | 18 ++ .../solutions/aorlando/cpp-version/queue.cpp | 225 ++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 challenges/easy/queue/solutions/aorlando/cpp-version/README.md create mode 100644 challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp diff --git a/challenges/easy/queue/solutions/aorlando/cpp-version/README.md b/challenges/easy/queue/solutions/aorlando/cpp-version/README.md new file mode 100644 index 0000000..d99dc64 --- /dev/null +++ b/challenges/easy/queue/solutions/aorlando/cpp-version/README.md @@ -0,0 +1,18 @@ +# Queue - How does it work? + +Queues follow the First-in-First-Out ([FIFO](https://it.wikipedia.org/wiki/FIFO)) principle. + +***e.g.*** As if waiting in a queue for the movie tickets, the first one to stand in line is the first one to buy a ticket and enjoy the movie. + +I have created a data structure in cpp for a queue able to do the following methods: + + - `enqueue(item)` - adds an item to the queue + - `dequeue()` - removes an item from the queue (FIFO) + - `peek()` - returns the next item in the queue without removing it + - `isEmpty()` - tests to see whether the queue is empty + - `size()` - returns the number of items in the queue + - `isFull()` - tests to see whether the queue is full + - `display()` - return the item inside the queue + + + I've also tryed to develop a fancy CLI interface to insert the intructions directly from the user :) diff --git a/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp b/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp new file mode 100644 index 0000000..7e97070 --- /dev/null +++ b/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp @@ -0,0 +1,225 @@ +#include +using namespace std; + +class Queue { + private: + int front; + int rear; + int arr[5]; + int itemCount; + + public: + // constructor + Queue() { + int itemCount = 0; + front = -1; + rear = -1; + for(int i=0; i<5; i++) { + arr[i]=0; + } + } + + void enqueue(int item) { + if(isFull()) { + cout << "Queue is FULL" << endl; + return; + } + else if(isEmpty()) { + rear=0; + front=0; + arr[rear]=item; + } + else { + /* e.g. + Assume we have added an element in our array that have at least one element already: + rear == 0 + rear + 1 == 1 + 1 % 5 == 1, so rear become == 1 + then put the new element in the position 1 of our array + + enqueue() again: + rear == 1 + rear + 1 == 2 + 2 % 5 == 2, so rear become == 2 + then put the new element in the position 2 of our array + + Let's say that we have 5 elements [5 4 3 2 1] in our array so the queue is full. + In according to the algorithm, rear is == to 4 + If we dequeue(), the first element removed is 5 + then enqueue() the element 6: + rear == 4 + rear + 1 == 5 + 5 % 5 == 0, so rear become == 0 + then put the new element in the position 0 of our array + (see dequeue() method to understand the logic for that process) + */ + rear = (rear+1)%5; + arr[rear]=item; + + } + itemCount++; + } + + int dequeue() { + int x = 0; + if(isEmpty()) { + cout << "Queue is EMPTY" << endl; + return -1; //we have to reurn some value couse of the int function + } + else if(front == rear) { + /* + the only chance that could happen is when the arrey has only one element + so front and rear come back to the initial situation --> front = rear = -1 + */ + x = arr[front]; // I put the value wich I have to return at the variable x + arr[front] = 0; // I replace the value with a 0 + front = -1; + rear = -1; + itemCount--; + return x; + } + else { + /* e.g. + Assume we have at least two elements in the array [5 4]: + front == 0 + x = arr[0] --> 5 + raplace arr[front] with 0 as a placeholder + + (front+1)%5 == 1 + then front become == 1 + in that case, front = rear = 1. So if we dequeue() again, the second condition run + + dequeue() an array with 3 elements [5 4 3]: + front == 0 + x = arr[0] --> 5 + raplace arr[front] with 0 as a placeholder + + (front+1)%5 == 1 + then front become == 1 + in that case, front = 1 and rear = 2. So if we dequeue() again, the third condition run again + + */ + x = arr[front]; + arr[front] = 0; + front = (front+1)%5; + itemCount--; + return x; + } + } + + int peek() { + if(!isEmpty()) { + return arr[front]; + } + } + + bool isEmpty() { + if(front==-1 && rear==-1) + return true; + else + return false; + } + + int size() { + return itemCount; + } + + bool isFull() { + /* e.g. + Assume we have our array full of elements [5 4 3 2 1]: + front == 0 + rear == 4 + rear + 1 == 5 + 5 % 5 == 0 + so (rear+1)%5 == front and then the queue is full and the condition is true + + if we dequeue() one element: + front == 1 // (see the dequeue() logic) + rear == 4 // it remains the same as before till we enqueue() a new element + rear + 1 == 5 + 5 % 5 == 0 + so (rear+1)%5 != front and then the queue is not full and the condition is false + */ + if((rear+1)%5 == front) + return true; + else + return false; + } + + void display() { + for(int i=0; i<5; i++) { + cout << arr[i] << " "; + } + } + +}; + + +int main() { + + Queue test1; + int option, item; + + do { + cout << "\n\nWhat operation do you want to perform? Select Option number (0 to exit)." << endl; + cout << "1. enqueue(item)" << endl; + cout << "2. dequeue()" << endl; + cout << "3. peek()" << endl; + cout << "4. isEmpty()" << endl; + cout << "5. size()" << endl; + cout << "6. isFull()" << endl; + cout << "7. display()" << endl; + + + cin >> option; + + switch(option) { + case 0: + break; + + case 1: + cout << "*** Enqueue Operation 1*** \nPlease enter an item to Enqueue in the Queue:"<> item; + test1.enqueue(item); + break; + + case 2: + cout << "*** Dequeue Operation *** \nDequeued Value: " << test1.dequeue() < Date: Sat, 9 Oct 2021 10:58:19 +0200 Subject: [PATCH 2/4] refactor: from PR #51 - implement queue challenge using vector data structure --- .../solutions/aorlando/cpp-version/README.md | 1 - .../solutions/aorlando/cpp-version/queue.cpp | 155 +++--------------- 2 files changed, 24 insertions(+), 132 deletions(-) diff --git a/challenges/easy/queue/solutions/aorlando/cpp-version/README.md b/challenges/easy/queue/solutions/aorlando/cpp-version/README.md index d99dc64..577f58d 100644 --- a/challenges/easy/queue/solutions/aorlando/cpp-version/README.md +++ b/challenges/easy/queue/solutions/aorlando/cpp-version/README.md @@ -11,7 +11,6 @@ I have created a data structure in cpp for a queue able to do the following meth - `peek()` - returns the next item in the queue without removing it - `isEmpty()` - tests to see whether the queue is empty - `size()` - returns the number of items in the queue - - `isFull()` - tests to see whether the queue is full - `display()` - return the item inside the queue diff --git a/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp b/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp index 7e97070..39c4210 100644 --- a/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp +++ b/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp @@ -1,162 +1,63 @@ #include +#include using namespace std; -class Queue { + +class Queue +{ private: - int front; - int rear; - int arr[5]; - int itemCount; + vector myQueue; public: - // constructor - Queue() { - int itemCount = 0; - front = -1; - rear = -1; - for(int i=0; i<5; i++) { - arr[i]=0; - } - } void enqueue(int item) { - if(isFull()) { - cout << "Queue is FULL" << endl; - return; - } - else if(isEmpty()) { - rear=0; - front=0; - arr[rear]=item; - } - else { - /* e.g. - Assume we have added an element in our array that have at least one element already: - rear == 0 - rear + 1 == 1 - 1 % 5 == 1, so rear become == 1 - then put the new element in the position 1 of our array - - enqueue() again: - rear == 1 - rear + 1 == 2 - 2 % 5 == 2, so rear become == 2 - then put the new element in the position 2 of our array - - Let's say that we have 5 elements [5 4 3 2 1] in our array so the queue is full. - In according to the algorithm, rear is == to 4 - If we dequeue(), the first element removed is 5 - then enqueue() the element 6: - rear == 4 - rear + 1 == 5 - 5 % 5 == 0, so rear become == 0 - then put the new element in the position 0 of our array - (see dequeue() method to understand the logic for that process) - */ - rear = (rear+1)%5; - arr[rear]=item; - - } - itemCount++; + myQueue.push_back(item); } int dequeue() { int x = 0; if(isEmpty()) { - cout << "Queue is EMPTY" << endl; + cout << "Queue is Empty." << endl; return -1; //we have to reurn some value couse of the int function } - else if(front == rear) { - /* - the only chance that could happen is when the arrey has only one element - so front and rear come back to the initial situation --> front = rear = -1 - */ - x = arr[front]; // I put the value wich I have to return at the variable x - arr[front] = 0; // I replace the value with a 0 - front = -1; - rear = -1; - itemCount--; - return x; - } else { - /* e.g. - Assume we have at least two elements in the array [5 4]: - front == 0 - x = arr[0] --> 5 - raplace arr[front] with 0 as a placeholder - - (front+1)%5 == 1 - then front become == 1 - in that case, front = rear = 1. So if we dequeue() again, the second condition run - - dequeue() an array with 3 elements [5 4 3]: - front == 0 - x = arr[0] --> 5 - raplace arr[front] with 0 as a placeholder - - (front+1)%5 == 1 - then front become == 1 - in that case, front = 1 and rear = 2. So if we dequeue() again, the third condition run again - - */ - x = arr[front]; - arr[front] = 0; - front = (front+1)%5; - itemCount--; + x = myQueue.at(0); + myQueue.erase(myQueue.begin()); return x; } } int peek() { - if(!isEmpty()) { - return arr[front]; - } + if(!isEmpty()) + return myQueue.at(0); + else + cout << "Queue is Empty." << endl; + return -1; } bool isEmpty() { - if(front==-1 && rear==-1) + if(myQueue.size() == 0) return true; else return false; } int size() { - return itemCount; - } - - bool isFull() { - /* e.g. - Assume we have our array full of elements [5 4 3 2 1]: - front == 0 - rear == 4 - rear + 1 == 5 - 5 % 5 == 0 - so (rear+1)%5 == front and then the queue is full and the condition is true - - if we dequeue() one element: - front == 1 // (see the dequeue() logic) - rear == 4 // it remains the same as before till we enqueue() a new element - rear + 1 == 5 - 5 % 5 == 0 - so (rear+1)%5 != front and then the queue is not full and the condition is false - */ - if((rear+1)%5 == front) - return true; - else - return false; + return myQueue.size(); } void display() { - for(int i=0; i<5; i++) { - cout << arr[i] << " "; + if (isEmpty()) + cout << "|None"; + cout << "|"; + for(int i=0; i> option; @@ -188,7 +88,7 @@ int main() { break; case 3: - cout << "*** Peek Operation *** \nThe first item is: " << test1.peek() < Date: Sun, 10 Oct 2021 00:22:17 +0200 Subject: [PATCH 3/4] refactor: improve methods logic of Queue class --- .../solutions/aorlando/cpp-version/queue.cpp | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp b/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp index 39c4210..475f198 100644 --- a/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp +++ b/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp @@ -2,7 +2,6 @@ #include using namespace std; - class Queue { private: @@ -17,29 +16,29 @@ class Queue int dequeue() { int x = 0; if(isEmpty()) { - cout << "Queue is Empty." << endl; - return -1; //we have to reurn some value couse of the int function + cout << "Queue is Empty. The values in queue are: "; + return 0; //we have to return some value couse of the int function } else { x = myQueue.at(0); myQueue.erase(myQueue.begin()); + cout << "Dequeued Value: "; return x; } } int peek() { - if(!isEmpty()) + if(!isEmpty()) { + cout << "The last item inserted is: "; return myQueue.at(0); + } else - cout << "Queue is Empty." << endl; - return -1; + cout << "Queue is Empty. The values in queue are: "; + return 0; } bool isEmpty() { - if(myQueue.size() == 0) - return true; - else - return false; + return myQueue.size() == 0; } int size() { @@ -47,8 +46,6 @@ class Queue } void display() { - if (isEmpty()) - cout << "|None"; cout << "|"; for(int i=0; i Date: Tue, 12 Oct 2021 10:26:53 +0200 Subject: [PATCH 4/4] refactor: removed every cout << statement from methods of Queue class and improve readability of CLI interface --- .../solutions/aorlando/cpp-version/queue.cpp | 54 +++++++++---------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp b/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp index 475f198..0f89a0f 100644 --- a/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp +++ b/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp @@ -14,27 +14,13 @@ class Queue } int dequeue() { - int x = 0; - if(isEmpty()) { - cout << "Queue is Empty. The values in queue are: "; - return 0; //we have to return some value couse of the int function - } - else { - x = myQueue.at(0); - myQueue.erase(myQueue.begin()); - cout << "Dequeued Value: "; - return x; - } + int x = myQueue.at(0); + myQueue.erase(myQueue.begin()); + return x; } int peek() { - if(!isEmpty()) { - cout << "The last item inserted is: "; - return myQueue.at(0); - } - else - cout << "Queue is Empty. The values in queue are: "; - return 0; + return myQueue.at(0); } bool isEmpty() { @@ -45,11 +31,8 @@ class Queue return myQueue.size(); } - void display() { - cout << "|"; - for(int i=0; i display() { + return myQueue; } }; @@ -57,6 +40,7 @@ class Queue int main() { Queue test1; int option, item; + vector queue; do { cout << "\n\nWhat operation do you want to perform? Select Option number (0 to exit)." << endl; @@ -75,17 +59,23 @@ int main() { break; case 1: - cout << "*** Enqueue Operation 1*** \nPlease enter an item to Enqueue in the Queue:"<> item; test1.enqueue(item); break; case 2: - cout << "*** Dequeue Operation ***\n" << test1.dequeue() <