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/5] [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: Mon, 4 Oct 2021 08:06:26 +0200 Subject: [PATCH 2/5] [FEAT] first solution of stack problem in cpp --- .../solutions/aorlando/cpp-version/README.md | 15 ++ .../solutions/aorlando/cpp-version/stack.cpp | 148 ++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 challenges/easy/stack/solutions/aorlando/cpp-version/README.md create mode 100644 challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp diff --git a/challenges/easy/stack/solutions/aorlando/cpp-version/README.md b/challenges/easy/stack/solutions/aorlando/cpp-version/README.md new file mode 100644 index 0000000..18654c4 --- /dev/null +++ b/challenges/easy/stack/solutions/aorlando/cpp-version/README.md @@ -0,0 +1,15 @@ +# Stack - How does it work? + +Stacks follow the Last-in-First-Out ([LIFO](https://it.wikipedia.org/wiki/LIFO)) principle. + +***e.g.*** As if stacking coins one one top of the other, the last coin we put on the top is the one that is the first to be removed from stack later. + +I have created a data structure in cpp for a stack able to do the following methods: + +- `push(item)` - Add an item to the top of the stack. +- `pop()` - Remove the top item from the stack. +- `peek()` - Returns a copy of the top item in the stack. +- `isEmpty()` - Return a boolean indicating whether or not the stack is empty. +- `size()` - Return the number of items in the stack. + +I've also tryed to develop a fancy CLI interface to insert the intructions directly from the user :) diff --git a/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp b/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp new file mode 100644 index 0000000..31bfd46 --- /dev/null +++ b/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp @@ -0,0 +1,148 @@ +#include +#include + +using namespace std; + + +class Stack { + private: + int top; + int arr[5]; + + public: + // constructor + Stack() { + top = -1; + for(int i=0; i<5; i++) { + arr[i] = 0; + } + } + + void push(int item) { + if(isFull()) { + cout<<"Stack is FULL."<=0; i--) { // in order to display it in the stack manner + cout<> option; + + switch(option) { + case 0: + break; + + case 1: + cout << "*** Push Operation 1*** \nPlease enter an item you want to Stack:"<> item; + test1.push(item); + break; + + case 2: + cout << "*** Pop Operation *** \nPop Value: " << test1.pop() < Date: Sat, 9 Oct 2021 10:46:55 +0200 Subject: [PATCH 3/5] refactor: from PR #52 - implement stack challenge using vector data structure --- .../solutions/aorlando/cpp-version/stack.cpp | 94 +++++++------------ 1 file changed, 32 insertions(+), 62 deletions(-) diff --git a/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp b/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp index 31bfd46..c830651 100644 --- a/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp +++ b/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp @@ -1,85 +1,63 @@ #include -#include - +#include using namespace std; -class Stack { +class Stack +{ private: - int top; - int arr[5]; + vector myStack; public: - // constructor - Stack() { - top = -1; - for(int i=0; i<5; i++) { - arr[i] = 0; - } - } void push(int item) { - if(isFull()) { - cout<<"Stack is FULL."<=0; i--) { // in order to display it in the stack manner - cout<> option; @@ -101,7 +78,7 @@ int main() { break; case 1: - cout << "*** Push Operation 1*** \nPlease enter an item you want to Stack:"<> item; test1.push(item); break; @@ -111,7 +88,7 @@ int main() { break; case 3: - cout << "*** Peek Operation *** \nThe last item inserted is: " << test1.peek() < Date: Sun, 10 Oct 2021 00:19:33 +0200 Subject: [PATCH 4/5] refactor: improve methods logic of Stack class --- .../solutions/aorlando/cpp-version/stack.cpp | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp b/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp index c830651..bf77b48 100644 --- a/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp +++ b/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp @@ -17,29 +17,29 @@ class Stack int pop() { int x = 0; if(isEmpty()) { - cout << "Stack is Empty." << endl; - return -1; //we have to reurn some value couse of the int function + cout << "Stack is Empty. The values in stack are: "; + return 0; //we have to return some value couse of the int function } else { x = myStack.size(); myStack.pop_back(); + cout << "Pop value: "; return myStack[x-1]; } } int peek() { - if(!isEmpty()) + if(!isEmpty()){ + cout << "The first item inserted in Stack is: "; return myStack.at(myStack.size() - 1); + } else - cout << "Stack is Empty." << endl; - return -1; + cout << "Stack is Empty. The values in stack are: "; + return 0; } bool isEmpty() { - if(myStack.size() == 0) - return true; - else - return false; + return myStack.size() == 0; } int size() { @@ -47,8 +47,6 @@ class Stack } void display() { - if (isEmpty()) - cout << "|None"; cout << "|"; for(int i = 0; i < myStack.size(); i++) { cout << myStack[i] << "|"; @@ -78,17 +76,17 @@ int main() { break; case 1: - cout << "*** Push Operation 1*** \nPlease enter an item to push in the Stack:"<> item; test1.push(item); break; case 2: - cout << "*** Pop Operation *** \nPop Value: " << test1.pop() < Date: Tue, 12 Oct 2021 10:15:08 +0200 Subject: [PATCH 5/5] refactor: removed every cout << statement from methods of Stack class and improve readability of CLI interface --- .../solutions/aorlando/cpp-version/stack.cpp | 56 +++++++++---------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp b/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp index bf77b48..b1913b5 100644 --- a/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp +++ b/challenges/easy/stack/solutions/aorlando/cpp-version/stack.cpp @@ -3,8 +3,8 @@ using namespace std; -class Stack -{ +class Stack { + private: vector myStack; @@ -15,27 +15,13 @@ class Stack } int pop() { - int x = 0; - if(isEmpty()) { - cout << "Stack is Empty. The values in stack are: "; - return 0; //we have to return some value couse of the int function - } - else { - x = myStack.size(); - myStack.pop_back(); - cout << "Pop value: "; - return myStack[x-1]; - } + int x = myStack.size(); + myStack.pop_back(); + return myStack[x-1]; } int peek() { - if(!isEmpty()){ - cout << "The first item inserted in Stack is: "; - return myStack.at(myStack.size() - 1); - } - else - cout << "Stack is Empty. The values in stack are: "; - return 0; + return myStack.at(myStack.size() - 1); } bool isEmpty() { @@ -46,11 +32,8 @@ class Stack return myStack.size(); } - void display() { - cout << "|"; - for(int i = 0; i < myStack.size(); i++) { - cout << myStack[i] << "|"; - } + vector display() { + return myStack; } }; @@ -58,6 +41,7 @@ class Stack int main() { Stack test1; int option, item; + vector stack; do { cout << "\n\nWhat operation do you want to perform? Select Option number (0 to exit)." << endl; @@ -82,11 +66,17 @@ int main() { break; case 2: - cout << "*** Pop Operation ***\n" << test1.pop() <