Perl | Implementing a Queue
Last Updated :
01 Sep, 2021
Prerequisite: Stack
Queue in Perl is a linear abstract data structure which follows the FIFO (First In First Out) order. It resembles the properties of the queue which we encounter in our daily life where the person who comes first will be served first. It is open at both ends. Unlike stack where the operation of insertion and deletion occurs at same end called the top of stack, in queue these operations occur at different ends called the front and rear end of the queue.
Operations on Queue:
The following basic operations are performed on a queue:
- Enqueue (Insertion): Adds an item to the queue. An Overflow condition occurs if the queue is full.
- Dequeue (deletion): Removes an item from the queue. An Underflow condition occurs if the queue is empty.
- Front: Get the front item from queue.
- Rear: Get the last item from queue.

Creating a Queue
Creating a queue in Perl is very simple. It can be done by declaring an array which can be either empty or initialized with some pre-filled data.
@queue; # Queue is empty.
@queue = (1, 2, 3); # Queue with initialized values.
Types of Queues
Based on the criterion of Insertion and deletion, queues can be of the following types:
Simple Queue
Simple Queue is simply a queue where insertion takes place at front and deletion takes place at end of the queue.
In Perl, the operations of a queue can be implemented using push and shift functions.
The push function is used to add one or more values at the end of the queue.
The shift function will move the queue one unit to the left deleting (popping) the first element. It will return undef in case of empty queue.
Examples:
Perl
#!/usr/bin/perl
use strict;
use warnings;
my @queue = (1, 2, 3);
print "Original Queue: @queue" ;
push ( @queue , (4, 5, 6));
print ( "\nUpdated Queue after Push: @queue" );
my $val1 = shift ( @queue );
print ( "\nElement popped is: $val1" );
my $val2 = shift ( @queue );
print ( "\nElement popped is: $val2" );
print ( "\nUpdated Queue after Pop: @queue" );
|
Output:
Original Queue: 1 2 3
Updated Queue after Push: 1 2 3 4 5 6
Element popped is: 1
Element popped is: 2
Updated Queue after Pop: 3 4 5 6
Circular Queue
In circular queue, the last position of the queue is connected to the first position to make a circle, also called as Ring Buffer. If the queue is empty from front side but full at the end, then also we can insert elements in the circular queue, which is not the case in the simple queue. The circular queue will be of fixed size, unlike a normal queue.
In additions to push and shift functions, circular queue also has two variables front and rear to store the front and the rear positions of the queue.

Example:
Perl
#!/usr/bin/perl
use strict;
use warnings;
my @queue = (1, 2, 3);
my $size = 7;
my $front = 0;
my $rear = 2;
print "Original Queue: @queue" ;
$rear = ( $rear + 1) % $size ;
my $val = 4;
while (1)
{
if ( $rear == $front )
{
print ( "\nQueue is full." );
last ;
}
else
{
print ( "\nPushed $val in queue" );
$queue [ $rear ] = $val ;
$rear = ( $rear + 1) % $size ;
$val += 1;
}
}
print ( "\nUpdated Queue after Push: @queue" );
my $val1 = $queue [ $front ];
$queue [ $front ] = -1;
$front = ( $front + 1) % $size ;
print ( "\nElement popped is: $val1" );
my $val2 = $queue [ $front ];
$queue [ $front ] = -1;
$front = ( $front + 1) % $size ;
print ( "\nElement popped is: $val2" );
print ( "\nUpdated Queue after Pop: @queue" );
while (1)
{
if ( $rear % $size == $front )
{
print ( "\nQueue is full." );
last ;
}
else
{
print ( "\nPushed $val in queue" );
$queue [ $rear ] = $val ;
$rear += 1;
$val += 1;
}
}
print ( "\nUpdated Queue after Push: @queue" );
|
Output:
Original Queue: 1 2 3
Pushed 4 in queue
Pushed 5 in queue
Pushed 6 in queue
Pushed 7 in queue
Queue is full.
Updated Queue after Push: 1 2 3 4 5 6 7
Element popped is: 1
Element popped is: 2
Updated Queue after Pop: -1 -1 3 4 5 6 7
Pushed 8 in queue
Pushed 9 in queue
Queue is full.
Updated Queue after Push: 8 9 3 4 5 6 7
Priority Queue
In priority queue, every item has a defined priority associated with it. An item having the least priority will be the first to be removed from the priority queue. The items with same priority will be dealt with in the order of their
position in the queue.
Examples:
Perl
#!/usr/bin/perl
use strict;
use warnings;
my @queue = ([1, 3], [2, 7], [4, 3], [5, 2]);
sub pull_highest_prio
{
my $max = 0;
for ( my $i = 0; $i < scalar ( @queue ); $i ++)
{
if ( $queue [ $i ][1] > $queue [ $max ][1])
{
$max = $i ;
}
}
print ( "\nPopped item: $queue[$max][0], " ,
"Priority: $queue[$max][1]" );
splice @queue , $max , 1;
}
print ( "\nOriginal Queue is " );
for ( my $i = 0; $i < 4; $i ++)
{
print ( "$queue[$i][0] " );
}
push ( @queue , [11, 9]);
push ( @queue , [7, 0]);
print ( "\nUpdated Queue after push is " );
for ( my $i = 0; $i < 6; $i ++)
{
print ( "$queue[$i][0] " );
}
pull_highest_prio;
pull_highest_prio;
pull_highest_prio;
pull_highest_prio;
print ( "\nUpdated Queue after pop is " );
for ( my $i = 0; $i < 2; $i ++)
{
print ( "$queue[$i][0] " );
}
|
Output:
Original Queue is 1 2 4 5
Updated Queue after push is 1 2 4 5 11 7
Popped item: 11, Priority: 9
Popped item: 2, Priority: 7
Popped item: 1, Priority: 3
Popped item: 4, Priority: 3
Updated Queue after pop is 5 7
Deque(Doubly Ended Queue)
Doubly Ended Queue is just a generalized version of simple queue, except the insertion and deletion operations, can be done at both Front and Rear end of the queue. It is a very important type of data structure as it can be used both as a stack or a queue.
Examples:
Perl
#!/usr/bin/perl
use strict;
use warnings;
my @queue = (1, 2, 3);
my $front = 0;
my $rear = 2;
sub insert_front
{
my $val = $_ [0];
unshift ( @queue , $val );
$rear += 1;
}
sub insert_rear
{
my $val = $_ [0];
push ( @queue , $val );
$rear += 1;
}
sub delete_front
{
print ( "\nElement popped is: $queue[0]" );
splice @queue , 0, 1;
$rear -= 1;
}
sub delete_rear
{
print ( "\nElement popped is: $queue[$rear]" );
splice @queue , scalar ( @queue ) - 1, 1;
$rear -= 1;
}
print "Original Queue: @queue" ;
&insert_rear(4);
&insert_rear(3);
print ( "\nRear element is $queue[$rear]" );
&insert_front(1);
&insert_front(7);
print ( "\nFront element is $queue[$front]" );
print ( "\nUpdated Queue after Push: @queue" );
delete_rear;
delete_front;
print ( "\nFront element is $queue[$front]" );
&insert_front(20);
delete_rear;
print ( "\nRear element is $queue[$rear]" );
print ( "\nUpdated Queue after Pop: @queue" );
|
Output:
Original Queue: 1 2 3
Rear element is 3
Front element is 7
Updated Queue after Push: 7 1 1 2 3 4 3
Element popped is: 3
Element popped is: 7
Front element is 1
Element popped is: 4
Rear element is 3
Updated Queue after Pop: 20 1 1 2 3
Abstract Implementation
Abstract means to hide the functionality of the operations from the normal user, that is to provide the operational functionality without defining the logic behind the function.
In Perl, the abstract implementation of queue can be achieved using Perl’s in-built module Thread::Queue which provide thread-safe FIFO queues that can be accessed by any number of threads.
Basic methods:
- new(list) – Creates a new queue with the provided list of items or if not listed it creates a new empty queue.
- enqueue(list) – Adds a list of items onto the end of the queue.
- dequeue(COUNT) – Removes the requested number of items from the head of the queue, and returns them and if not mentioned it dequeues one element from the queue.
- pending() – Returns the number of items still left in the queue or we can say, it basically returns the size of the queue.
- end() – declares that no more items will be added to the queue.
Examples:
Perl
#!/usr/bin/perl
use strict;
use warnings;
use Thread::Queue;
my $q = Thread::Queue->new(1, 2, 3);
sub head
{
if ( $q ->pending() == 0)
{
print ( "\nThe queue is empty." );
}
else
{
my $item = $q ->dequeue();
print ( "\nHead of the Queue is $item." );
}
}
my $isize = $q ->pending();
print ( "Initial size of the queue is $isize" );
$q ->enqueue(5, 6, 7);
&head();
my $size = $q ->pending();
print ( "\nThe size of the queue is $size" );
$q ->dequeue();
&head();
$q ->dequeue(2);
my $size2 = $q ->pending();
print "\nThe size of the queue is $size2" ;
&head();
&head();
$q ->end();
|
Output:
size of the queue is 3
Head of the Queue is 1.
The size of the queue is 5
Head of the Queue is 3.
The size of the queue is 1
Head of the Queue is 7.
The queue is empty.
Parallel or Asynchronous Processing
The word Parallel means simultaneously or at the same time and Asynchronous refers to continuous or non-blocking. The implementation operations of a queue are blocking i.e while performing one operation the other operation have to wait for it to end first and are then executed. This is fine for small number of tasks but for a large number of tasks or operations, this type of processing is a bit hectic and time-consuming.
To overcome this problem, Perl provide some way of doing parallel or asynchronous processing so that these operations can run simultaneously or without waiting for other to end. The two major solutions for this is threading and forking. Forking is to create exactly same copy of some process and then these two processes executes in parallel without much connection between their variables, although they manipulate or work upon same variables(copy). It is done using fork() function.
Examples:
Perl
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
say "PID $$" ;
my $pid = fork ();
die if not defined $pid ;
say "PID $$ ($pid)" ;
|
Output:
PID 22112
PID 22112 (22113)
PID 22113 (0)
Before calling fork(), we got a PID (22112) which is the process ID for current process, after forking we got two lines. The first printed line came from the same process as the current (it has the same PID), the second printed line came from the child process (with PID 63779). The first one received a $pid from fork containing the number of the child process. The second, the child process got the number 0.
Perl also supports event-driven programming through POE framework.
Similar Reads
Queue meaning in DSA
A Queue is defined as a linear data structure that is open at both ends and the operations are performed in the First In First Out (FIFO) order. Characteristics of Queue:The first item added to the queue is the first one to be processed (or can be firstly deleted/removed), and subsequent items are p
3 min read
C++ Program to Implement Circular Queue
In C++, Queues are a fundamental data structure in computer science which works on the principle of FIFO (First In, First Out). They can be implemented using both array and linked list. A circular queue is a type of queue in which the last element is connected to the first element, forming a circula
7 min read
C Program to Implement Priority Queue
Priority queue is an abstract data type(ADT) in the computer science which is designed to the operate much like the regular queue except that each element has the certain priority. The priority can determines the order in which elements are dequeued - elements with the higher priority are removed fr
6 min read
C Program to Implement Circular Queue
A circular queue is a linear data structure that follows the FIFO (First In, First Out) principle but connects the last position back to the first, forming a circle. In this article, we will learn how to implement circular queue in C programming language. What is a Circular Queue in C?In a circular
5 min read
Find Largest element in a Queue
Given a queue of integers, the task is to write a program that efficiently finds the largest element in that queue. Return -1 if the queue is empty. Examples: Input: Queue = {15, 27, 18}Output: 27Explanation: Among 15(front), 27 and 18(back), 27 is the largest. Input: Queue = {12, 25, 29, 16, 32}Out
5 min read
Implement thread-safe queue in C++
What is a Thread-safe Queue?A thread-safe queue is a data structure that is designed to provide thread safety for a concurrent environment. It is a data structure that allows multiple threads to access the same queue and enqueue and dequeue elements concurrently. The threads do not need to be synchr
3 min read
C++ Program to Implement Queue using Array
A queue is a linear data structure that consists of elements arranged in a sequential order where one end is used to add elements, and another for removing them which results in the FIFO (First-In First-Out) order of operations. In this article, we will learn how to write a program to implement queu
8 min read
C++ Implementation Double-Ended Queue
A Double-Ended Queue (Deque) is an abstract data structure that generalizes a queue, for which elements can be added to or removed from either the front (head) or back (tail). It is also often called a head-tail linked list. In this article, we will learn how to implement a Deque in C++ along with i
9 min read
C++ Program to Implement Queue using Linked List
Queue is the fundamental data structure that follows the First In, First Out (FIFO) principle where the elements are added at the one end, called the rear and removed from other end called the front. In this article, we will learn how to implement queue in C++ using a linked list. Queue Using Linked
5 min read
Queue Implementation Using Linked List in Java
Queue is the linear data structure that follows the First In First Out(FIFO) principle where the elements are added at the one end, called the rear, and removed from the other end, called the front. Using the linked list to implement the queue allows for dynamic memory utilization, avoiding the cons
4 min read