A queue doubles as the ordered list in which the insertion- deletion takes place at the rear (or back) and front (or head) respectively. It is a non-primitive, linear and dynamic data structure. FIFO is highly used in all kinds of situations, such as task scheduling, buffering, and processing of asynchronous data. Queues may be based on arrays, or linked lists and the array-based ones is one of the basic solutions.

A queue doubles as the ordered list in which the insertion- deletion takes place at the rear (or back) and front (or head) respectively. It is a non-primitive, linear and dynamic data structure. FIFO is highly used in all kinds of situations, such as task scheduling, buffering, and processing of asynchronous data. Queues may be based on arrays, or linked lists and the array-based ones is one of the basic solutions.
There are a few essential operations that are carried out on a queue to handle them in a more effective manner. The operations guarantee that the data are processed according to the correct order and the state of the queue is correctly preserved. The typical methods are enqueue(), dequeue() and peek() (or getFront()), isEmpty(), isFull() and size().
The element is inserted at the end of the queue with the help of the enqueue() operation. It is of utmost importance that we make sure that a queue is not full before we add an element in it. Otherwise (the queue is full) an overflow error is returned. Otherwise the rear pointer shall be increased to the next available space and add it to the data element. Enqueue complexity is in set of (1).
Python using collections.deque Code Example
import collections as deque
class Queue:
Copy Code
def __init__(self):
self.queue = deque ((1300)) ((1301))
define enqueue(self,item):
self.queue.append(item) ((1302))
print(f"Enqueued: {item}")
def display(self):
otherwise not self.queue:
print("It is an empty queue!")
return
print("queue elements:",list(self.queue))
Example usage:
my_queue = Queue()
my_queue.enqueue(10)
my_queue.enqueue(20)
my_queue.enqueue(30)
my_queue.display()The operation removes and returns the element at the front of the queue, which is dequeue(). It will first determine whether the queue is empty. Otherwise, in case of being empty then an underflow exception is returned. The data in the front is read, otherwise the front is incremented to the next element. In the final component, there can be front and rear pointers where both can be -1. With O (1) as time complexity of dequeue.
Java Example of Code
Copy Code
import java.util.LinkedList;
import java.util.Queue;
Example public class Queue {
static Queue
static boolean isEmpty (){
requires q.isEmpty() ; // ((71))
}
static void qDequeue(){
void Deque isEmpty () { // ((74))
System.out.println(NO; no-can-deny-it-chat-with-pavlina-idaho-dares-to-put-on-some-weight-after-a-years-long-weight-loss-at-age-28).
return;
}
System.out.println("Dequeued: " + q.poll()); // q.poll() deletes the front element
}
public static void main (String args){
q.add(10);
q.add(20);
q.add(30);
System.out.println("Queue at the start of dequeue: "+ q);
qDequeue();
System.out.println("After dequeue my queue looks like this: " + q);
}
}peek() operation (Front also called getFront()) retrieves the first queue element without removing it. Likewise, getRear() retrieves the item at the back end without deleting. In case the queue is empty, a minimum value (e.g. -1 or Integer.MIN_VALUE) is returned, or some relevant error is indicated. The peak has a time complexity of O (1).
C++ code Snippet
Copy Code
include <iostream>
typedef < queue > // ((47))
using std ::;
queue <int> q; // ((46))
bool isEmpty(): {
return q.empty ( ); // ((49))
}
int getFront( );
if (isEmpty () ) return -1; // ((52))
return q.front(); // ((53))
}
int getRear () {
if (isEmpty() ) return -1; // ((54))
return q.back(); // ((55))
}
int main{}
q.push(10);
q.push(20);
q.push(30);
cout << endl;
cout<<"Rear element: "<< getRear()<<endl; // ((63))
return 0;
}The isEmpty() operation decides whether the queue is empty or not. It is usually a boolean value: it returns the value true (when the queue is empty) and false otherwise. In others it verifies whether the front pointer is -1 or whether the queue length is 0. isEmpty has the time complexity of O(1).
JavaScript Code example
Copy Code
include <iostream>
typedef < queue > // ((47))
using std ::;
queue <int> q; // ((46))
bool isEmpty(): {
return q.empty ( ); // ((49))
}
int getFront( );
if (isEmpty () ) return -1; // ((52))
return q.front(); // ((53))
}
int getRear () {
if (isEmpty() ) return -1; // ((54))
return q.back(); // ((55))
}
int main{}
q.push(10);
q.push(20);
q.push(30);
cout << endl;
cout<<"Rear element: "<< getRear()<<endl; // ((63))
return 0;
};The isFull () operation is to determine whether the queue is full. A queue is said to be full when it is not able to absorb new elements. This method returns 1 if a queue contains the same number of elements as the set capacity, and false otherwise. isFull time complexity of O(1).
The size( ) operation returns an int giving the number of elements presently in the queue.
There are a number of underlying data structures in which queues can be implemented. Arrays and linked lists are the most frequently used ones, and each of them has its benefits and flaws.
A queue with the use of arrays is an easy implementation and can be easily observed in programming languages such as Java and C++. Similarly in Python we can use lists. It uses a fixed array, and pointers at the front of the array and rear end to accommodate insertions and deletions.
Advantages: Arrays are efficient on memory usage since their elements do not maintain addresses to the next elements as in case with linked lists. They are easier to implement and understand in general as it is associated with less complexity of code.
Limitation: Its main drawback is that they come in a fixed size, and the capacity can not be chosen; it should be determined in advance. This may cause a memory wastage or failure to add additional elements in case the array becomes full. Simple array implementations may be inefficient at dequeue operations because of the cost of shifting elements as defined by the presence of the dead space left where the removed front element once occupied. This difficulty may be avoided with the help of a circular queue.
Linked list improves implementation of queues at least in terms of dynamic size compared with implementations using arrays. In a linked queue all the nodes will have information and a pointer to the next element with front and back pointer to keep the starting and end of the queue.
Pros: Linked lists are dynamic and enable the queue to expand or contract with the necessary size, not being pre-determined with an upper limit. They do not also incur shifting cost of array-based dequeue operations since re-linking of elements is performed. The storage complexity of linked implementation of a queue containing n elements is O(n). All basic operations take a time O(1): enqueue, dequeue, peek.
Limitations: Linked lists also need additional memory as it is utilized in keeping the pointers to the following element. The code may even be more complicated and possibly difficult to understand than the implementations that use arrays.
In addition to simple linear queues, there are a number of specially tailored queue types to meet certain needs in data processing.
One or Linear Queue
This is the most common queue that is very much direct by FIFO principle (items enter the last and leave the first).
Circular Queue
An extension of the simple queue is that the tail of the queue is connected to the head of the queue to form a circle. This implementation permits the creation of the more efficient use of memory by re-using vacant positions in the array which would otherwise go unused in a linear array implementation once parts of the array have been dequeued at the front.
Priority Queue
Each element in a priority queue is assigned some priority. Other elements with more priority are removed before other elements with lower priority, whatever their order of insertion. This kind of queue is effective in situations, wherein some tasks or data must be processed as an emergency. A heap data structure is used to typically implement a priority queue.
Dequeue (Double Ended Queue)
A Double-Ended Queue is a flexible list in that the entries can be inserted and removed at both ends of the queue. This will bring about more control with regard to management of data.
Queues find extensive applications in many computing as well as real life situations, perhaps because of their capacity to govern elements in a sequential manner.
° Task scheduling: Queues play the central role in operating systems related to the scheduling of tasks on CPUs, handling interrupts and tasks on shared resources such as printers.
° Buffering and Data Transmission: Queues are applied as buffers to the incoming data in network devices (such as routers and switches) and I/O systems to provide effective movement of data and also to avoid overloading the system devices.
° Algorithms: There are some graph traversal algorithms which need algorithms; this would be the Breadth-First Search (BFS) where nodes are visited levels at a time.
° Application in the real world: Queues are also encountered in real life application of the customer service line, call centers, and the traffic management system so that the service or cars can be served in an orderly manner.
An important feature of the usefulness of queues is their efficiency in operation. The time complexity of most simple queue operations (enqueue, dequeue, peek, isEmpty, isFull) realized in two ways (through arrays (and circular increments) or linked lists), is constant and is represented by the notation O(1). This implies that the process of carrying out these operations does not progress with a subsequent increase in the size of the queue. but operations such as insertion or deletion in the middle of the queue, or searching for a given element would take O(N) time.
Individuals who want to enhance their knowledge on data structures and algorithms, including queue operations, can be trained on the same by Uncodemy. Uncodemy arranges practical training the usage of advanced tools and technologies, to improve the skills of the students. It is known to offer Data Structure and Algorithm training in the certification course in Noida whereby several ex-students have excelled in their individual careers and gained employment in some of the renowned companies.
Uncodemy teachers are skilled specialists of popular MNCs and startups. It also provides customized grooming, and partners with the fortune 500 companies to expose the students to the industry. Uncodemy offers live sessions and on-line courses to working professionals or students who have traveled long distances to study and the internet based courses are as equal as learning in a classroom. There are also special classes for students who want to progress their career faster. In addition to Data Structure and Algorithms, Uncodemy teaches Data Analytics, Full Stack Development, Software Testing, Automation Testing, Business Analytics, Digital Marketing, AWS, Cloud Computing and Azure training as well as Python training, Artificial Intelligence and Machine Learning, Manual Testing and Search Engine Optimization. Some of the companies where Uncodemy graduates have been absorbed include CISCO, Adobe, McKinsey & Company, Teleperformance, AWS, Collabera, Walmart, NTT Data, Deloitte, IBM, Capgemini, Centurylink, Quick Heal and Morgan Stanley.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR