22import java .util .Queue ;
33import java .util .concurrent .ArrayBlockingQueue ;
44
5- /* QueueInterface
6- * This class demonstrates how the Queue interface works in Java
7- * using two implementations:
8- * 1. LinkedList (unbounded queue).
9- * 2. ArrayBlockingQueue (bounded queue with fixed capacity).
10-
11- * Queue follows FIFO (First In, First Out).
12- * Key methods: add(), offer(), remove(), poll(), element(), peek().
13- */
14-
155public class QueueInterfaceDemo {
166 public static void main (String [] args ) {
17- // ---------------- Queue with LinkedList ----------------
7+ // Queue with LinkedList
188 Queue <Integer > queue1 = new LinkedList <>();
199
2010 // Add elements (FIFO order).
2111 queue1 .add (1 );
2212 queue1 .add (3 );
2313 queue1 .add (2 );
14+ queue1 .add (4 );
2415
2516 System .out .println ("Queue1 (LinkedList) contents: " + queue1 );
2617 System .out .println ("Queue1 size: " + queue1 .size ());
@@ -38,7 +29,7 @@ public static void main(String[] args) {
3829 System .out .println ("Peeking head element: " + queue1 .peek ());
3930
4031
41- // ---------------- Queue with ArrayBlockingQueue ----------------
32+ // Queue with ArrayBlockingQueue
4233 // ArrayBlockingQueue is a bounded blocking queue with fixed capacity.
4334 Queue <Integer > queue2 = new ArrayBlockingQueue <>(2 );
4435
@@ -56,6 +47,16 @@ public static void main(String[] args) {
5647}
5748
5849/*
50+ QueueInterface
51+ This class demonstrates how the Queue interface works in Java
52+
53+ using two implementations:
54+ 1. LinkedList (unbounded queue).
55+ 2. ArrayBlockingQueue (bounded queue with fixed capacity).
56+
57+ Queue follows FIFO (First In, First Out).
58+ Key methods: add(), offer(), remove(), poll(), element(), peek().
59+
59601. Queue Interface:
6061 - Part of java.util package.
6162 - Represents a collection designed for holding elements prior to processing (FIFO).
@@ -111,5 +112,4 @@ ASCII Example (FIFO):
111112Enqueue 1: [1]
112113Enqueue 2: [1,2]
113114Offer 3: fails (capacity full) → [1,2]
114-
115115*/
0 commit comments