
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Basic Operations for Queue in Data Structure
Queue is a collection of different data types and is an important part of data structure, following a particular order to insert and remove elements. In this tutorial, we will understand the basic operations of the queue.
What is Queue in Data Structure?
A Queue is a linear data structure that resembles a queue in real life. You all have been part of some queue in school, the billing counter, or any other place, where the one entered first will exit first in the queue. Similarly, a queue in data structure also follows the FIFO principle, which defines First In First Out. The element inserted first in the queue will terminate first, compared to the remaining.
A queue has two endpoints and is open to both ends.
Front − It is the end from where elements move out of the queue.
Rear − It is the end from where elements are inserted in the queue.

It can be implemented using a one-dimensional array, pointer, structures, and linked list. C++ library contains various built-in functions that help to manage the queue, its operations take place only at the front and rear ends.
Syntax to declare a queue
queue<data type> queue_name
Example
queue<int> q queue<string> s
Basic Queue Operations
The most useful operation of the queue in C++ are as follows −
pop() −It removes the front element of the queue. Syntax −queue_name.pop();
push() −(): It is used to insert elements at the starting or rear end of the queue. Syntax −queue_name.push(data_value);
front() −(): It checks or returns the element at the front of the queue. Syntax −queue_name.front();
size() −It is used to get the size of the queue. Syntax −queue_name.size();
empty() −It checks if the queue is empty or not. Returns boolean value as per the condition. Syntax −queue_name.empty();
Code for the push() function.
#include <iostream> #include<queue> using namespace std; int main() { queue<int> q; //initializing queue q.push(4); //inserting elements into the queue using push() method q.push(5); q.push(1); cout<<"Elements of the Queue are: "; while(!q.empty()) { cout<<q.front()<<""; // printing 1st element of the queue q.pop(); // removing elements from the queue } return 0; }
Output
Elements of the queue are: 451
In the above example, we created a queue q and inserted elements into it using the push() function, the function inserts all elements at the rear end.
Use the empty() function to check if the queue is empty or not, if not, the queue will return the element at the front, and using the pop() function will remove queue elements from the front end.
Example
#include <iostream> #include<queue> using namespace std; int main() { queue<int> q; //initializing queue q.push(4); //inserting elements into the queue using push() method q.push(5); q.push(1); cout<<"Elements of the Queue are: "; while(!q.empty()) { cout<<q.front()<<""; // printing 1st element of the queue q.pop(); // removing elements from the queue } return 0; }
Output
size of queue is : 451
Example of empty() function of the queue.
#include <iostream> #include<queue> using namespace std; int main() { queue<string> q; //declaring string type of queue q.push("cpp"); //inserting elements into the queue using push() method q.push("Java"); q.push("C++"); if(q.empty()) //using empty() function to return the condition cout<<"yes, Queue is empty"; else cout<<"No, queue has elements"; return 0; }
Output
No queue has elements
Conclusion
A queue can store integer and string elements. In the data structure, there is one more queue called the priority queue, which has priority with all the queue elements.
I hope this tutorial will help you to understand the meaning of queue in the data structure.
- Related Articles
- Queue Data Structure in Javascript
- Operations on Queue in Data Structures
- Dictionary Operations in Data Structure
- Circular Queue Data Structure in C++
- Max WBLT Operations in Data Structure
- Explain linear data structure queue in C language
- A data structure for n elements and O(1) operations?
- Differences between stack and queue data structure
- Java Program to Implement the queue data structure
- Golang program to implement the Queue data structure
- STL Priority Queue for Structure or Class in C++
- Meldable Priority Queue Operations
- Python Program to Implement Queue Data Structure using Linked List
- Basic List Operations in Python
- Basic Tuples Operations in Python
