Skip to content

Commit 430a56c

Browse files
committed
feat: add QueueDemo showing FIFO operations using LinkedList as a Queue
Implemented QueueDemo.java to demonstrate enqueue, dequeue, and peek operations using LinkedList. Highlights LinkedList’s dual behavior as List and Queue, explaining FIFO processing, complexity, and real-world use cases. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent bbd93f8 commit 430a56c

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

Section 25 Collections Frameworks/Queue Interface/src/QueueDemo.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
import java.util.LinkedList;
22

3-
/* This class demonstrates how a LinkedList in Java can be used as a Queue.
4-
* - LinkedList implements the Queue and Deque interfaces.
5-
* - Queue follows FIFO (First In, First Out) order.
6-
7-
* Key Operations:
8-
* - Enqueue (addLast): Add an element at the rear (end).
9-
* - Dequeue (removeFirst): Remove an element from the front (head).
10-
* - Peek: View the head element without removing it.
11-
*/
12-
133
public class QueueDemo {
144
public static void main(String[] args) {
15-
// LinkedList can be used as both List and Queue.
5+
// LinkedList can be used as both List and Queue and also Stack.
166
LinkedList<Integer> list = new LinkedList<>();
17-
187
// Adding elements (acts like a normal list here).
198
list.add(3);
209
list.add(2);
@@ -48,9 +37,12 @@ public static void main(String[] args) {
4837
}
4938

5039
/*
51-
==========================
40+
Key Operations:
41+
- Enqueue (addLast): Add an element at the rear (end).
42+
- Dequeue (removeFirst): Remove an element from the front (head).
43+
- Peek: View the head element without removing it.
44+
5245
Queue Theory with LinkedList
53-
==========================
5446
5547
1. Why LinkedList as Queue?
5648
- LinkedList implements Queue and Deque interfaces.
@@ -80,4 +72,8 @@ public static void main(String[] args) {
8072
6. Difference from Stack:
8173
- Queue → FIFO (first in, first out).
8274
- Stack → LIFO (last in, first out).
75+
76+
This class demonstrates how a LinkedList in Java can be used as a Queue.
77+
- LinkedList implements the Queue and Deque interfaces.
78+
- Queue follows FIFO (First In, First Out) order.
8379
*/

0 commit comments

Comments
 (0)