Posts

Showing posts with the label Queue

Priority Queue

A priority queue is an abstract data structure and it is similar to the regular queue . Each element in the priority queue has a priority associated with it. In the priority queue, we dequeue the highest priority element before the lowest priority and if the priority is the same then as per the insertion order it will dequeue. One of the best real-time applications, CPU Scheduling is basically done the same thing. If the higher priority task occurs then it interrupts the ongoing process and shares the resources to the higher priority. We have taken the example of RabbitMq in the queue blog . In RabbitMq, we push elements in the queue and wait until the subscriber subscribes to the messages. Suppose, If there are n messages that are pending to subscribe and one of the messages needs to be processed as soon as possible. If we have pushed the message by setting the high priority in the queue then it will process first then rest of them. Some of the languages implement the priority queue

Deque

Image
Double Ended Queue is an abstract data structure that allows us to enqueue from both of the sides but dequeue can only be done either of the side (rear or front) . We have to decide before writing a code whether we are allowing dequeue from front or rear. We can implement the deque using an Array or Linked List . It is easy to create Deque using a linked list but in an array, we need to very careful to implement it.  Dequeue From Rear Dequeue From Front NOTE: - Generic <T> type can accept any data type, If you have not passed explicitly then it will consider it as an Object. Deque Implementation using an Array #1 Initialization of Custom Deque - O(1) public class CustomDeque< T > { T [] data ; int length , front , rear , capacity ; public CustomDeque( T [] data) { this . data = data; capacity = data. length ; } } #2 Adding Element at the Front - O(1) public void addFirst( T ele) { if (ele == null ) throw new NullPointerExceptio

Queue

Image
A  Queue  is a data structure that holds the element, will insert from the rear of the queue, and remove from the front of the queue. Insertion is known as Enqueue and Deletion is known as Dequeue. It follows the First In First Out(FIFO) or Last In Last out(LILO). There are many problem statements where we generally use the queue to hold the object and process it whenever it is required. One of the most used application is RabbitMq in real-time projects. Let's take an example, there are 1 publisher and 1 subscriber, publisher publish an object into the queue, and the subscriber will subscribe and remove it from the queue. We can implement a queue using an Array or Linked List .                                                                                NOTE: - Generic <T> type can accept any data type, If you have not passed explicitly then it will consider it  as an  Object. Queue Implementation using Array #1 Initialization of Custom Queue public class CustomQueue<

Cache Replacement Algorithm - FIFO

Cache Replacement FIFO Algorithm - (First In First Out) Before, we understood how the cache replacement algorithm works using FIFO. We will just look for a while to understand the FIFO with the layman term. Suppose a company running a seminar and max capacity of the seminar hall is 100 employees. They have decided whoever comes first they will get the entry and once's seminar hall is full then wouldn't allow anyone. There is no priority to anyone or any designation. So, we will implement a cache replacement algorithm using the Queue abstract data type. We can implement a queue using ArrayList or LinkedList. Let's do the code and understand the implementation of the algorithm. Implement Cache Replacement FIFO policy using ArrayList import java.util.Arrays; import java.util.List; import java.util.Optional; /** * @author Chandra Shekhar Paatni on 26/04/20 */ public class CacheReplacementFIFO< T > { T [] cacheReplacementFIFOS ; int index = - 1 ; pu