-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathFIFO.java
More file actions
37 lines (29 loc) · 918 Bytes
/
FIFO.java
File metadata and controls
37 lines (29 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Java program to demonstrate
// working of FIFO
// using Queue interface in Java
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args)
{
Queue<Integer> q = new LinkedList<>();
// Adds elements {0, 1, 2, 3, 4} to queue
for (int i = 0; i < 5; i++)
q.add(i);
// Display contents of the queue.
System.out.println("Elements of queue-" + q);
// To remove the head of queue.
// In this the oldest element '0' will be removed
int removedele = q.remove();
System.out.println("removed element-" + removedele);
System.out.println(q);
// To view the head of queue
int head = q.peek();
System.out.println("head of queue-" + head);
// Rest all methods of collection interface,
// Like size and contains can be used with this
// implementation.
int size = q.size();
System.out.println("Size of queue-" + size);
}
}