mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 12:11:28 +08:00
Update queue readme (#4721)
This commit is contained in:
@ -31,17 +31,58 @@
|
|||||||
- **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.
|
- **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.
|
||||||
|
|
||||||
## Declaration
|
## Declaration
|
||||||
|
|
||||||
`Queue<Obj> queue = new PriorityQueue<Obj>();`
|
`Queue<Obj> queue = new PriorityQueue<Obj>();`
|
||||||
|
|
||||||
## Important operations
|
## Important operations
|
||||||
|
|
||||||
| Operations | Description |
|
| Operations | Description |Time Complexity
|
||||||
| ----------- | ----------- |
|
| ----------- | ----------- |-----------
|
||||||
|Enqueue|Adds an item to the queue|
|
|Enqueue|Adds an item to the queue|O(1)
|
||||||
|Dequeue|Removes an item from the queue|
|
|Dequeue|Removes an item from the queue|O(1)
|
||||||
|Front|Gets the front item from the queue|
|
|Front|Gets the front item from the queue|O(1)
|
||||||
|Rear|Gets the last item from the queue|
|
|Rear|Gets the last item from the queue|O(n)
|
||||||
|
|
||||||
|
## Enqueue
|
||||||
|
It adds an item to the rear of the queue.
|
||||||
|
|
||||||
|
For example: If we have `1, 2, 3, 4, 5` in queue, and if we call Enqueue(8),
|
||||||
|
|
||||||
|
`8` will be added to last index of queue -> `1, 2, 3, 4, 5, 8`.
|
||||||
|
## Dequeue
|
||||||
|
|
||||||
|
It removes an item to the front of the queue.
|
||||||
|
|
||||||
|
For example: If we have `1, 2, 3, 4, 5` in queue, and we call Dequeue(),
|
||||||
|
|
||||||
|
`1` will be removed from front of queue and returned -> `2, 3, 4, 5`.
|
||||||
|
|
||||||
|
## Front
|
||||||
|
It returns an item to the front of the queue.
|
||||||
|
|
||||||
|
For example: If we have `1, 2, 3, 5` in queue, and we call Front(),
|
||||||
|
|
||||||
|
`1` will be returned (without removing it from the queue).
|
||||||
|
|
||||||
|
## Rear
|
||||||
|
It returns an item to the rear of the queue.
|
||||||
|
|
||||||
|
For example: If we have `1, 2, 3, 5` in queue, and we call Rear(),
|
||||||
|
|
||||||
|
`5` will be returned (without removing it from the queue).
|
||||||
|
|
||||||
|
# Real Life Applications
|
||||||
|
`Task Scheduling in Operating Systems:`
|
||||||
|
|
||||||
|
Processes in a multitasking system are often scheduled using queues. For example, the ready queue contains processes ready to be executed.
|
||||||
|
|
||||||
|
`Multi-threaded Programming:`
|
||||||
|
|
||||||
|
Queues are often used to facilitate communication and synchronization between different threads.
|
||||||
|
|
||||||
|
`Breadth-First Search (BFS) in Graphs:`
|
||||||
|
|
||||||
|
Queues are used in algorithms like BFS to explore a graph level by level.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user