A queue is a first-in-first-out (FIFO) data structure. It can be implemented using a linked list.
The following are the three main operations that can be performed on a queue:
enqueue: adding an element to the end of the list.
dequeue: popping an item from the head of the list.
peeking: displaying the element at the tail of the list.
#include <iostream>#include <list>using namespace std;// Add an element to the tail of the linked listvoid enqueue(int elem, list <int> &Queue){Queue.push_back(elem);}// Remove an element from the head of the linked listint dequeue(list <int> &Queue){if(!Queue.empty()){int temp = Queue.front();Queue.pop_front();return temp;}}// Display the element from the tail of the linked listint peek(list <int> &Queue){if(!Queue.empty()){return Queue.back();}}int main() {// Declaring a list to be used as a Queuelist <int> Queue;// Enqueue 3enqueue(3, Queue);// Display element at the tail of the linked listcout<<"Element at tail: "<<peek(Queue)<<endl;// Enqueue 4enqueue(4, Queue);// Display element at the tail of the linked listcout<<"Element at tail: "<<peek(Queue)<<endl;// Enqueue 5enqueue(5, Queue);// Display element at the tail of the linked listcout<<"Element at tail: "<<peek(Queue)<<endl;// Dequeue elementscout<<"Element dequeued: "<<dequeue(Queue)<<endl;cout<<"Element dequeued: "<<dequeue(Queue)<<endl;cout<<"Element dequeued: "<<dequeue(Queue)<<endl;return 0;}