023-2023-winter domain crawl of the Luxembourg web domain (.lu) performed by Internet Archive on behalf of the National Library of Luxembourg / Bibliothèque nationale de Luxembourg.
TIMESTAMPS
The Wayback Machine - https://web.archive.org/web/20240118104834/https://www.geeksforgeeks.org/introduction-to-queue-data-structure-and-algorithm-tutorials/
We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end. The element which is first pushed into the order, the delete operation is first performed on that.
A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served. (i.e. First come first serve).
Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the queue, is called the front of the queue(sometimes, head of the queue), similarly, the position of the last entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the queue. See the below figure.
FIFO property of queue
Characteristics of Queue:
Queue can handle multiple data.
We can access both ends.
They are fast and flexible.
Queue Representation:
1. Array Representation of Queue:
Like stacks, Queues can also be represented in an array: In this representation, the Queue is implemented using the array. Variables used in this case are
Queue: the name of the array storing queue elements.
Front: the index where the first element is stored in the array representing the queue.
Rear: the index where the last element is stored in an array representing the queue.
Array representation of queue:
C
// Creating an empty queue
// A structure to represent a queue
structQueue {
intfront, rear, size;
unsigned capacity;
int* array;
};
// function to create a queue of given capacity
// It initializes size of queue as 0
structQueue* createQueue(unsigned capacity)
{
structQueue* queue
= (structQueue*)malloc(sizeof(structQueue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1;
queue->array
= (int*)malloc(queue->capacity * sizeof(int));
returnqueue;
}
// This code is contributed by Susobhan Akhuli
C++
// Creating an empty queue
// A structure to represent a queue
classQueue {
public:
intfront, rear, size;
unsigned cap;
int* arr;
};
// Function to create a queue of given capacity
// It initializes size of queue as 0
Queue* createQueue(unsigned cap)
{
Queue* queue = newQueue();
queue->cap = cap;
queue->front = queue->size = 0;
queue->rear = cap - 1;
queue->arr = newint[(queue->cap * sizeof(int))];
returnqueue;
}
Java
/*package whatever //do not write package name here */
importjava.io.*;
classGFG {
// A structure to represent a queue
staticclassQueue {
intfront, rear, size;
intcap;
intarr[];
}
// Function to create a queue of given capacity
// It initializes size of queue as 0
Queue createQueue(intcap)
{
Queue queue = newQueue();
queue.cap = cap;
queue.front = 0;
queue.size = 0;
queue.rear = cap - 1;
queue.arr = newint[queue.cap];
returnqueue;
}
}
// This code is contributed by aadityapburujwale
Python3
# Creating an empty queue
# A structure to represent a queue
classQueue:
# constructor
def__init__(self, cap):
self.cap =cap
self.front =0
self.size =0
self.rear =cap -1
self.arr =[0] *cap
# Function to create a queue of given capacity
# It initializes size of queue as 0
defcreateQueue(self):
returnQueue(self.cap)
# This code is contributed by Tapesh (tapeshdua420)
C#
// Creating an empty queue
classGFG {
// A structure to represent a queue
staticclassQueue {
publicintfront, rear, size;
publicintcap;
publicint[] arr;
}
// Function to create a queue of given capacity
// It initializes size of queue as 0
publicstaticQueue createQueue(intcap)
{
Queue queue = newQueue();
queue.cap = cap;
queue.front = 0;
queue.size = 0;
queue.rear = cap - 1;
queue.arr = newint[queue.cap];
returnqueue;
}
}
// This code is contributed by Tapesh (tapeshdua420)
Javascript
<script>
// Queue class
class Queue
{
// Array is used to implement a Queue
constructor()
{
this.items = [];
}
}
// This code is contributed by Susobhan Akhuli
<script>
2. Linked List Representation of Queue:
A queue can also be represented using following entities:
Linked-lists,
Pointers, and
Structures.
C
// A C program to demonstrate linked list based
// implementation of queue
// A linked list (LL) node to store a queue entry
structQNode {
intkey;
structQNode* next;
};
// The queue, front stores the front node of LL and rear
// stores the last node of LL
structQueue {
structQNode *front, *rear;
};
// A utility function to create a new linked list node.
structQNode* newNode(intk)
{
structQNode* temp
= (structQNode*)malloc(sizeof(structQNode));
temp->key = k;
temp->next = NULL;
returntemp;
}
// A utility function to create an empty queue
structQueue* createQueue()
{
structQueue* q
= (structQueue*)malloc(sizeof(structQueue));
q->front = q->rear = NULL;
returnq;
}
// This code is contributed by Susobhan Akhuli
C++
structQNode {
intdata;
QNode* next;
QNode(intd)
{
data = d;
next = NULL;
}
};
structQueue {
QNode *front, *rear;
Queue() { front = rear = NULL; }
};
Java
/*package whatever //do not write package name here */
importjava.io.*;
classGFG {
staticclassQNode {
intdata;
QNode next;
QNode(intdata)
{
this.data = data;
next = null;
}
}
staticclassQueue {
QNode front, rear;
Queue()
{
front = null;
rear = null;
}
}
}
// This code is contributed by aadityapburujwale
Python3
classQNode:
def__init__(self, data):
self.data =data
self.next=None
classQueue:
def__init__(self):
self.front =None
self.rear =None
# This code is contributed by Tapesh (tapeshdua420)
C#
// Include namespace system
usingSystem;
publicclassGFG {
staticclassQNode {
publicintdata;
publicQNode next;
publicQNode(intdata)
{
this.data = data;
this.next = null;
}
}
staticclassQueue {
publicQNode front;
publicQNode rear;
publicQueue()
{
this.front = null;
this.rear = null;
}
}
}
// This code is contributed by aadityaburujwale.
Javascript
<script>
// JavaScript program for linked-list implementation of queue
Input Restricted Queue: This is a simple queue. In this type of queue, the input can be taken from only one end but deletion can be done from any of the ends.
Output Restricted Queue: This is also a simple queue. In this type of queue, the input can be taken from both ends but deletion can be done from only one end.
Circular Queue: This is a special type of queue where the last position is connected back to the first position. Here also the operations are performed in FIFO order. To know more refer this.
Double-Ended Queue (Dequeue): In a double-ended queue the insertion and deletion operations, both can be performed from both ends. To know more refer this.
Priority Queue: A priority queue is a special queue where the elements are accessed based on the priority assigned to them. To know more refer this.
To learn more about different types of queues, read the article on “Types of Queues“.
Basic Operations for Queue in Data Structure:
Some of the basic operations for Queue in Data Structure are:
Enqueue() – Adds (or stores) an element to the end of the queue..
Dequeue() – Removal of elements from the queue.
Peek() or front()- Acquires the data element available at the front node of the queue without deleting it.
rear() – This operation returns the element at the rear end without removing it.
isFull() – Validates if the queue is full.
isNull() – Checks if the queue is empty.
There are a few supporting operations (auxiliary operations):
1. Enqueue():
Enqueue() operation in Queue adds (or stores) an element to the end of the queue. The following steps should be taken to enqueue (insert) data into a queue:
Step 1: Check if the queue is full.
Step 2: If the queue is full, return overflow error and exit.
Step 3: If the queue is not full, increment the rear pointer to point to the next empty space.
Step 4: Add the data element to the queue location, where the rear is pointing.
Application of queue is common. In a computer system, there may be queues of tasks waiting for the printer, for access to disk storage, or even in a time-sharing system, for use of the CPU. Within a single program, there may be multiple requests to be kept in a queue, or one task may create other tasks, which must be done in turn by keeping them in a queue.
It has a single resource and multiple consumers.
It synchronizes between slow and fast devices.
In a network, a queue is used in devices such as a router/switch and mail queue.
Variations: dequeue, priority queue and double-ended priority queue.
FAQs (Frequently asked questions) on Queue:
1. What data structure can be used to implement a priority queue?
Priority queues can be implemented using a variety of data structures, including linked lists, arrays, binary search trees, and heaps. Priority queues are best implemented using the heap data structure.
2. Queues are used for what purpose?
In addition to making your data persistent, queues reduce errors that occur when different parts of your system are down.
3. In data structures, what is a double-ended queue?
In a double-ended queue, elements can be inserted and removed at both ends.
4. What is better, a stack or a queue?
If you want things to come out in the order you put them in, use a queue. Stacks are useful when you want to reorder things after putting them in.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Please go through our recently updated Improvement Guidelines before submitting any improvements.
This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab.
You will be notified via email once the article is available for improvement.
Thank you for your valuable feedback!
Please go through our recently updated Improvement Guidelines before submitting any improvements.
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.