Skip to content

Commit ced9678

Browse files
authored
Update queue readme (#4721)
1 parent c6a22de commit ced9678

File tree

1 file changed

+49
-8
lines changed
  • src/main/java/com/thealgorithms/datastructures/queues

1 file changed

+49
-8
lines changed

src/main/java/com/thealgorithms/datastructures/queues/README.md

+49-8
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,58 @@
3131
- **Delay Queue:** Used for scheduling tasks to run after a specific delay or at a certain time. Elements are removed from the queue when their delay expires.
3232

3333
## Declaration
34-
35-
`Queue<Obj> queue = new PriorityQueue<Obj> ();`
34+
`Queue<Obj> queue = new PriorityQueue<Obj>();`
3635

3736
## Important operations
3837

39-
| Operations | Description |
40-
| ----------- | ----------- |
41-
|Enqueue|Adds an item to the queue|
42-
|Dequeue|Removes an item from the queue|
43-
|Front|Gets the front item from the queue|
44-
|Rear|Gets the last item from the queue|
38+
| Operations | Description |Time Complexity
39+
| ----------- | ----------- |-----------
40+
|Enqueue|Adds an item to the queue|O(1)
41+
|Dequeue|Removes an item from the queue|O(1)
42+
|Front|Gets the front item from the queue|O(1)
43+
|Rear|Gets the last item from the queue|O(n)
44+
45+
## Enqueue
46+
It adds an item to the rear of the queue.
47+
48+
For example: If we have `1, 2, 3, 4, 5` in queue, and if we call Enqueue(8),
49+
50+
`8` will be added to last index of queue -> `1, 2, 3, 4, 5, 8`.
51+
## Dequeue
52+
53+
It removes an item to the front of the queue.
54+
55+
For example: If we have `1, 2, 3, 4, 5` in queue, and we call Dequeue(),
56+
57+
`1` will be removed from front of queue and returned -> `2, 3, 4, 5`.
58+
59+
## Front
60+
It returns an item to the front of the queue.
61+
62+
For example: If we have `1, 2, 3, 5` in queue, and we call Front(),
63+
64+
`1` will be returned (without removing it from the queue).
65+
66+
## Rear
67+
It returns an item to the rear of the queue.
68+
69+
For example: If we have `1, 2, 3, 5` in queue, and we call Rear(),
70+
71+
`5` will be returned (without removing it from the queue).
72+
73+
# Real Life Applications
74+
`Task Scheduling in Operating Systems:`
75+
76+
Processes in a multitasking system are often scheduled using queues. For example, the ready queue contains processes ready to be executed.
77+
78+
`Multi-threaded Programming:`
79+
80+
Queues are often used to facilitate communication and synchronization between different threads.
81+
82+
`Breadth-First Search (BFS) in Graphs:`
83+
84+
Queues are used in algorithms like BFS to explore a graph level by level.
85+
4586

4687

4788

0 commit comments

Comments
 (0)