|
| 1 | +<p><b>First in, First out</b> system of approach is used in :-</p> |
| 2 | + |
| 3 | +1. Data Structures:<br/> |
| 4 | +There are data structures like queue and other variants of queue where we use FIFO approach of processing the data. |
| 5 | +2. Disk Scheduling Algorithms:<br/> |
| 6 | +Disk controllers use FIFo as a disk scheduling algorithm for ordering dist I/O Requests. |
| 7 | +3. Communications and Networking:<br/> |
| 8 | +FIFO system is used in communication network bridges, switches and routers in computer networks to hold data packets enroute to their next destination. |
| 9 | + |
| 10 | +Program example foe FIFO implementation in Queue: |
| 11 | + |
| 12 | +``` java |
| 13 | + |
| 14 | +// Java program to demonstrate |
| 15 | +// working of FIFO |
| 16 | +// using Queue interface in Java |
| 17 | + |
| 18 | +import java.util.LinkedList; |
| 19 | +import java.util.Queue; |
| 20 | + |
| 21 | +public class QueueExample { |
| 22 | + public static void main(String[] args) |
| 23 | + { |
| 24 | + Queue<Integer> q = new LinkedList<>(); |
| 25 | + |
| 26 | + // Adds elements {0, 1, 2, 3, 4} to queue |
| 27 | + for (int i = 0; i < 5; i++) |
| 28 | + q.add(i); |
| 29 | + |
| 30 | + // Display contents of the queue. |
| 31 | + System.out.println("Elements of queue-" + q); |
| 32 | + |
| 33 | + // To remove the head of queue. |
| 34 | + // In this the oldest element '0' will be removed |
| 35 | + int removedele = q.remove(); |
| 36 | + System.out.println("removed element-" + removedele); |
| 37 | + |
| 38 | + System.out.println(q); |
| 39 | + |
| 40 | + // To view the head of queue |
| 41 | + int head = q.peek(); |
| 42 | + System.out.println("head of queue-" + head); |
| 43 | + |
| 44 | + // Rest all methods of collection interface, |
| 45 | + // Like size and contains can be used with this |
| 46 | + // implementation. |
| 47 | + int size = q.size(); |
| 48 | + System.out.println("Size of queue-" + size); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +``` |
| 54 | +Output: |
| 55 | +
|
| 56 | +Elements of queue-[0, 1, 2, 3, 4] |
| 57 | +removed element-0 |
| 58 | +[1, 2, 3, 4] |
| 59 | +head of queue-1 |
| 60 | +Size of queue-4 |
| 61 | +``` |
| 62 | + |
| 63 | + |
| 64 | +<hr> |
| 65 | + |
| 66 | + Contributed by <a href="https://github.com/ShyamKumar1">Shyam Kumar</a> With 💜. |
| 67 | + |
| 68 | + Reach me on |
| 69 | +<p align='center'> |
| 70 | + <a href="https://www.linkedin.com/in/shyam-kumar-9b9841157/"><img src="https://img.shields.io/badge/linkedin-%230077B5.svg?&style=for-the-badge&logo=linkedin&logoColor=white" /></a> |
| 71 | + <a href="https://www.instagram.com/_smiling_storm_/" target="_blank"><img src="https://img.shields.io/badge/Instagram-%23E4405F.svg?&style=for-the-badge&logo=instagram&logoColor=white" alt="Instagram"></a> |
| 72 | + < a href= "mailto:[email protected]?subject=Olá%20Punit">< img src= "https://img.shields.io/badge/gmail-%23D14836.svg?&style=for-the-badge&logo=gmail&logoColor=white" /></ a> |
| 73 | + <a href="https://www.facebook.com/shyam.george15/" target="_blank"><img src="https://img.shields.io/badge/Facebook-%231877F2.svg?&style=for-the-badge&logo=facebook&logoColor=white" alt="Facebook"></a> |
| 74 | +</p> |
0 commit comments