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() < +#include +using namespace std; + + +class Stack { + + private: + vector myStack; + + public: + + void push(int item) { + myStack.push_back(item); + } + + int pop() { + int x = myStack.size(); + myStack.pop_back(); + return myStack[x-1]; + } + + int peek() { + return myStack.at(myStack.size() - 1); + } + + bool isEmpty() { + return myStack.size() == 0; + } + + int size() { + return myStack.size(); + } + + vector display() { + return myStack; + } +}; + + +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; + cout << "1. push(item)" << endl; + cout << "2. pop()" << endl; + cout << "3. peek()" << endl; + cout << "4. isEmpty()" << endl; + cout << "5. size()" << endl; + cout << "6. display()" << endl; + + + cin >> option; + + switch(option) { + case 0: + break; + + case 1: + cout << "*** Push Operation 1***\nPlease enter an item to push in the Stack:"<> item; + test1.push(item); + break; + + case 2: + if(test1.isEmpty()) + cout << "Stack is Empty." << endl; + else + cout << "*** Pop Operation ***\nPop item: " << test1.pop() <