--- title: queue --- ## Queues `queue` is one of the most used containers in C++. A container is a data structure that stores a collection of objects, some in order, some not. All containers have a different set of functions that allow you to access an object(s) in that collection. `std::queue` is part of the C++ standard library (hence the prefix `std::`) and allows you to store data in First In First Out (FIFO) order. NOTE: **All objects within a queue must be of the same data type** The data type you store within a queue goes within angle brackets next to the queue keyword. For example, if you would like to store a collection of integers the queue would be `std::queue queue_name` ### Queue LIFO Explanation `queue` allows us to push/enqueue and pop/dequeue in specific order. **Push** means inserting an object at the front of the queue. **Pop** means pulling out the "oldest" object from end of the queue. So when you push it is at the front and when you pop you extract the oldest element. ![alt text](https://github.com/mohammadaziz313/helloworld/blob/master/Fifo_queue.png "FIFO Queue Enqueue and Dequeue Example") ### Queue Operations The queue container supports the following operations: - push (enqueue) - pop (dequeue) - empty - size - front - back #### Push Allows you to inserts a new element at the end of the queue, after its current last element. ```cpp //Push operation in Queue #include // std::cout #include // std::queue int main () { std::queue q; q.push(1); //Pushing 1 at front of the queue q.push(2); //Pushing 2 at front of the queue return 0; } ``` #### Front Allows you to access the next element in the queue without removing it. The next element is the "oldest" element in the queue. ```cpp //Front operation in Queue #include // std::cout #include // std::queue int main () { std::queue q; q.push(1); //Pushing 1 at front of the queue q.push(2); //Pushing 2 at front of the queue std::cout< // std::cout #include // std::queue int main () { std::queue q; q.push(1); //Pushing 1 at front of the queue q.push(2); //Pushing 2 at front of the queue std::cout< // std::cout #include // std::queue int main () { std::queue q; q.push(1); //Pushing 1 at front of the queue q.push(2); //Pushing 2 at front of the queue std::cout< // std::cout #include // std::stack int main () { std::queue q; q.push(1); q.push(2); while(q.empty() != true){ std::cout< // std::cout #include // std::queue int main () { std::queue q; q.push(1); //Pushing 1 at front of the queue q.push(2); //Pushing 2 at front of the queue std::cout<