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..577f58d --- /dev/null +++ b/challenges/easy/queue/solutions/aorlando/cpp-version/README.md @@ -0,0 +1,17 @@ +# 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 + - `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..0f89a0f --- /dev/null +++ b/challenges/easy/queue/solutions/aorlando/cpp-version/queue.cpp @@ -0,0 +1,113 @@ +#include +#include +using namespace std; + +class Queue +{ + private: + vector myQueue; + + public: + + void enqueue(int item) { + myQueue.push_back(item); + } + + int dequeue() { + int x = myQueue.at(0); + myQueue.erase(myQueue.begin()); + return x; + } + + int peek() { + return myQueue.at(0); + } + + bool isEmpty() { + return myQueue.size() == 0; + } + + int size() { + return myQueue.size(); + } + + vector display() { + return myQueue; + } +}; + + +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; + cout << "1. enqueue(item)" << endl; + cout << "2. dequeue()" << 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 << "*** Enqueue Operation *** \nPlease add an item to Queue:"<> item; + test1.enqueue(item); + break; + + case 2: + if(test1.isEmpty()) + cout << "Queue is Empty" << endl; + else + cout << "*** Dequeue Operation ***\nDequeued item: " << test1.dequeue() <