Perl | Implementing a Queue
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 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.
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
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.
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
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.
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.
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 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.
- 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.
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.
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.