Insertion in Queue in C++
Insertion in Queue in C++
Insertion in Queue in C++ – Queue is a data structure where you can insert elements in First in First out manner, so you can store values in order to they are coming in a structure. This basic data structure has two major properties, enqueue and dequeue. In this particular article, we will find how to insert elements in a queue in C++, i.e. Enqueueing elements. Here we will use OOps Concept and List prebuilt structure.

Insertion in Queue in C++
Insertion in Queue in C++ – Queue is a data structure where you can insert elements in First in First out manner, so you can store values in order to they are coming in a structure. This basic data structure has two major properties, enqueue and dequeue. In this particular article, we will find how to insert elements in a queue in C++, i.e. Enqueueing elements. Here we will use OOps Concept and List prebuilt structure.

What is Queue?
Queue is a standard linear user defined data structure that allows you to pre-store elements in the specific order it gets, as get a first in first out or FIFO feature in that.
Simple Examples:
- Waiting line at a ticket counter – the person who comes first gets served first.
- Printer queue – the first document sent to the printer is printed first.
- Customer service calls – calls are answered in the order they are received.
The Algorithm to insert in Queue : Enqueue
Here goes the following algorithm that tells us how to enqueue / Insert elements in a queue.
- Let’s say the queue is a linked list. Here we have used a predefined linkedlist that is available inside C++ standard template library (List in STL), and made a class where there is a member variable of that type.
- Now Queue is first in first out. So the pushes will be at back of the list. You can obtain that in a different way, in C too : Here it is.
- That means, if queue is empty, we will simply push that element into it.
- Otherwise we will make a place at the back and add the element there.


The code to implement this:
#include<bits/stdc++.h> using namespace std; class Queue { public: list<int> L; void Push(int i) { cout<<"Pushing the element : "<<(i)<<endl; L.push_back(i); } int pop() { if(L.empty()) { cout<<"The queue is empty"<<endl; } int a=L.front(); L.pop_front(); return a; } void Show() { for(auto i:L) cout<<i<<" " ; cout<<endl; } }; int main() { Queue q; q.Push(2); q.Push(9); q.Push(3); q.Push(5); q.Push(12); q.Push(1); q.Show(); }
Output:
Pushing the element : 2 Pushing the element : 9 Pushing the element : 3 Pushing the element : 5 Pushing the element : 12 Pushing the element : 1 2 9 3 5 12 1
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
FAQs - Queue using Linked List in C++
FAQs - Queue using Linked List in C++
Arrays have a fixed size, which means you need to define the maximum number of elements beforehand. If the array gets full, you either need to resize it (which is expensive) or stop inserting. On the other hand, a linked list is dynamic, it uses memory as needed. You can keep adding elements until system memory is available, making it more efficient and flexible for queue operations.
If we try to remove (dequeue) an element when the queue is already empty, it should not crash the program. Instead, the program should display a proper message like “Queue is empty. Cannot dequeue.” and skip the deletion process. In linked list implementation, this is handled by checking if the front pointer is NULL.
Yes, circular queues can also be implemented using linked lists. In a circular queue, after the last node, the next pointer links back to the first node. This structure is useful in scenarios like memory management, round-robin scheduling, or buffering, where the queue starts over again after reaching the end.
Yes. When we remove a node from the front of the queue, we also free the memory allocated to that node using the delete keyword in C++. This ensures we don’t waste memory or cause memory leaks. It’s one of the benefits of using dynamic memory allocation responsibly in linked lists.
Yes, both enqueue (insertion at rear) and dequeue (deletion at front) operations are performed in O(1) time. This is because they involve only pointer changes and do not require shifting of elements or traversal of the list, unlike array-based implementations where shifting may be needed.
The rear pointer keeps track of the last node in the queue. This allows us to directly add new elements to the end in constant time (O(1)). Without the rear pointer, we would have to start from the front and traverse the entire list every time we wanted to insert a new node, which would be inefficient (O(n) time).
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Stacks
- Introduction to Stack in Data Structure
Click Here - Operations on a Stack
Click Here - Stack: Infix, Prefix and Postfix conversions
Click Here - Stack Representation in –
C | C++ | Java - Representation of a Stack as an Array. –
C | C++ | Java - Representation of a Stack as a Linked List. –
C | C++ | Java - Infix to Postfix Conversion –
C | C++ | Java - Infix to prefix conversion in –
C | C++ | Java - Postfix to Prefix Conversion in –
C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
Click Here - Queues Program in C and implementation
Click Here - Implementation of Queues using Arrays | C Program
Click Here - Types of Queues in Data Structure
Click Here - Application of Queue Data Structure
Click Here - Insertion in Queues Program (Enqueuing) –
C | C++ | Java - Deletion (Removal) in Queues Program(Dequeuing) –
C | C++ | Java - Reverse a Queue –
C | C++ | Java - Queues using Linked Lists –
C | C++ | Java - Implement Queue using Stack –
C | C++ | Java - Implement Queue using two Stacks –
C | C++ | Java
Circular Queues
- Circular queue in Data Structure
Click Here - Applications of Circular Queues
Click Here - Circular queue in –
C | C++ | Java - Circular queue using Array –
C | C++ | Java - Circular Queue using Linked Lists –
C | C++ | Java
Priority Queue
Stacks
- Introduction to Stack in Data Structure
- Operations on a Stack
- Stack: Infix, Prefix and Postfix conversions
- Stack Representation in – C | C++ | Java
- Representation of a Stack as an Array. – C | C++ | Java
- Representation of a Stack as a Linked List. – C | C++ | Java
- Infix to Postfix Conversion – C | C++ | Java
- Infix to prefix conversion in – C | C++ | Java
- Postfix to Prefix Conversion in – C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
- Queues Program in C and implementation
- Implementation of Queues using Arrays | C Program
- Types of Queues in Data Structure
- Application of Queue Data Structure
- Insertion in Queues Program (Enqueuing) – C | C++ | Java
- Deletion (Removal) in Queues Program(Dequeuing) – C | C++ | Java
- Reverse a Queue – C | C++ | Java
- Queues using Linked Lists – C | C++ | Java
- Implement Queue using Stack – C | C++ | Java
- Implement Queue using two Stacks – C | C++ | Java
Circular Queues
- Circular queue in Data Structure
- Applications of Circular Queues
- Circular queue in – C | C++ | Java
- Circular queue using Array – C | C++ | Java
- Circular Queue using Linked Lists – C | C++ | Java
Login/Signup to comment