FIFO Approach in data structureFIFO stands for First In First Out, in which we will enter the data elements into the data structure; the data element added at last in any data structure will be removed out last and the element added first will be removed first. Here, we treat the data elements with a fair chance; the element which has entered first will get the opportunity to leave first. The other name we call First Come First Serve is implemented using the data structure named a queue. The best example to understand the FIFO principle is when we stand in a queue and want to take a movie ticket, and there are a lot of people standing before us, and we have no other choice left instead of waiting for our chance. If we observe the above example, we can easily see that we will get the chance after the people standing before us and will be treated as first come, first serve.
The approach, as mentioned earlier, is known as the First-In-First-Out approach or FIFO. Applications of FIFO ApproachThe applications of FIFO are as follows: 1) Data StructuresThe queue is a linear data structure based on the FIFO approach, in which data elements enter into the queue using the rear end, and from the front end, deletion of elements occurs. FIFO approach is mostly used in network bridges, switches, and routers. It is further categorized as linear queue Linear QueueA linear queue is a linear data structure based on FIFO (First in First Out) principle, in which data enters from the rear end and is deleted from the front end. A linear queue is said to be the simple queue, and whenever we need to talk about the queue, it is by default set that we are considering a linear queue. We will discuss these ends in more detail further. Other examples like->
Operations of linear Queue There are certain basic operations has performed in the linear queue are as follows->
Two Ends of Linear Queue
Methods of Implementation of Linear queueThere are various methods of implementation of the queue:
Implementation of Linear queue using an ArrayHere we discuss the implementation of queue using an array-> Queues can easily be implemented by using linear arrays. As stated earlier, the queue has front and rear pointers from which deletion and insertion of data elements can be done. Steps
Algorithm of Enqueue operation Step 1- Check if REAR == FRONT. Step 2- If the above statement holds, then such a condition is said to be an Overflow, and further insertion of data elements is not possible. Step 3- If it holds false, then
Algorithm of Dequeue operation Step 1- Check if FRONT = REAR + 1 Step 2- If the above statement holds, then
Step 3- If it holds false, then
Step 4- After then Set FRONT = FRONT + 1, it points next data element. Algorithm of Peek operation Step 1- Check if FRONT = REAR + 1. Step 2- If the above statement holds, display the message; the queue is empty. Step 3- If it holds false,
FIFO Approach implementation in C Programming language by queue using arrayThe Output of the above program is: FIFO Approach implementation in C++ Programming language by queue using ArrayThe Output of the above program is: FIFO Approach implementation in Java Programming language using queueThe Output of the above program is: Queue is Empty 20 <-- 30 <-- 40 <-- 50 <-- Queue is full 20 <-- 30 <-- 40 <-- 50 <-- after two node deletion 40 <-- 50 <-- Front Element is: 40 FIFO Approach implementation in Python Programming language using queueThe Output of the above program is: Elements of queue - [ 0, 1, 2, 3, 4 ] removed element - 0 [ 1, 2, 3, 4 ] Peek value of queue - 1 Size of queue - 4 FIFO Approach implementation in C# Programming language using queueThe Output of the above program is: Elements of queue - [ 0, 1, 2, 3, 4 ] removed element - 0 [ 1, 2, 3, 4 ] Peek value of queue - 1 Size of queue - 4 FIFO Approach implementation in JavaScript Programming language using queueThe Output of the above program is: Elements of queue - [ 0, 1, 2, 3, 4 ] removed element - 0 [ 1, 2, 3, 4 ] Peek value of queue - 1 Size of queue - 4 FIFO Approach implementation in C Programming language by queue using stackThe Output of the above program is: Queue is empty !! Queue is full !! 20 30 40 50 FIFO Approach implementation in C++ Programming language by queue using stackThe Output of the above program is: Queue is empty !! Queue is full !! 20 3 40 50 Circular QueueImplementation of FIFO Approach using a circular queueA circular queue is also one of the important types of a queue. It is similar to the linear queue but has some variations. The end position of a circular queue is connected back to the start position, forming the circular-like structure and making a circle. A circular queue is also based on the First In First Out (FIFO) principle. It is also known as 'Ring Buffer'. As we have seen previously, in a linear queue, further insertion of data elements is not possible when REAR reaches the end, and FRONT also reaches to end. The queue is still empty, but it shows that the queue is full, so to overcome the above problem concept of circular is introduced. In the circular queue, this problem will not arise. We will further discuss it in more detail in the below section. The data structure, which is based on the FIFO principle, is a stack. We mainly perform two operations on it, push and pop. Push operation is used to push the data element into the stack, and pop operation is used to pop out the data elements from the stack. There are certain basic operations has performed in the linear queue are as follows->
Two ends of Circular Queue In a circular queue, there is a circle like structure; hence there are no two ends distinguished but still, to differentiate the insertion and deletion criteria of data elements, we refer to these two ends:
Implementation of Circular Queue using an arrayA circular queue can easily be implemented using arrays. It works on the circular increment process. As stated earlier, it has two pointers, front and rear. From the rear end, insertion of data elements is possible. If the rear pointer reaches the end, it again increments back to the initial position of the queue so that further insertions can also be possible; hence this implies an increment process. From the front end, deletion of data elements is possible. If the front pointer reaches the end, it returns to the queue's initial position to make further deletions possible. Using modulo division with the queue size, the circular increment is performed. Steps
Implementation of Circular Queue Algorithm of Enqueue operation Step 1- Check if FRONT == 0 && REAR == (n-1) || REAR == (FRONT - 1) % (n-1). Step 2- If the above statement holds, then such condition is said to be an Overflow, and further insertion of data elements is not possible. Step 3- Else if FRONT == -1 then,
Step 4- Else if REAR == (n-1) && FRONT != 0
Step 5- Else
Algorithm of Dequeue operation Step 1- Check if FRONT == -1 Step 2- If the above statement holds, then
Step 3- If it holds false, then
Step 4- After then Set FRONT = FRONT + 1, it points next data element. Step 5- If FRONT == REAR then, set FRONT = -1 and REAR = -1. Step 6- Else if FRONT == n-1 then, set FRONT = 0. FIFO Approach implementation in C Programming language by Circular queueThe Output of the above program is: Enqueue value 20 successfully !! Enqueue value 30 successfully !! Enqueue value 40 successfully !! Enqueue value 60 successfully !! Enqueue value 70 successfully !! Queue is full !! Deleting value: 20 Deleting value: 30 Deleting value: 40 Deleting value: 60 FIFO Approach implementation in C++ Programming language by Circular queue using StructureThe Output of the above program is: Enqueue value 7 successfully !! Enqueue value 11 successfully !! Enqueue value 4 successfully !! Enqueue value 6 successfully !! Enqueue value 7 successfully !! Queue is full !! Deleting value: 7 Deleting value: 11 Deleting value: 4 Deleting value: 6 FIFO Approach implementation in Java Programming language by Circular Queue using ArrayThe Output of the above program is: Elements in Circular Queue are : 14 22 13 8 Deleted value = 14 Deleted value = 22 Elements in Circular Queue are : 13 8 Elements in Circular Queue are : 13 8 9 20 5 Queue is Full !! FIFO Approach implementation in Python Programming language by Circular queue using ArrayThe Output of the above program is: Elements in Circular Queue are : 4 22 3 -6 Deleted value = 4 Deleted value = 22 Elements in Circular Queue are : 3 -6 Elements in Circular Queue are : 3 -6 19 2 15 Queue is Full !! FIFO Approach implementation in C Programming language by Circular Queue using Linked ListThe Output of the above program is: Enqueue value is: 20 Enqueue value is: 30 Enqueue value is: 40 Deleting value: 20 Deleting value: 30 Deleting value: 40 Queue is empty !! Enqueue value is: 60 Enqueue value is: 50 Enqueue value is: 70 Queue is full !! FIFO Approach implementation in C++ Programming language by Circular queue using Linked ListThe Output of the above program is: Enqueue value is: 20 Enqueue value is: 30 Enqueue value is: 40 Deleting value: 20 Deleting value: 30 Deleting value: 40 Queue is empty !! Enqueue value is: 60 Enqueue value is: 50 Enqueue value is: 70 Queue is full !! FIFO Approach implementation in Java Programming language by Circular Queue using Linked ListThe Output of the above program is: Elements in Circular Queue are : 10 2 16 Deleted value = 10 Deleted value = 2 Elements in Circular Queue are : 16 Elements in Circular Queue are : 16 19 7 FIFO Approach implementation in Python Programming language by Circular Queue using Linked ListThe Output of the above program is: Elements in Circular Queue are : 10 2 16 Deleted value = 10 Deleted value = 2 Elements in Circular Queue are : 16 Elements in Circular Queue are : 16 19 7 Advantages of Circular Queue over Linear queue
|