Update queue readme (#4721)

This commit is contained in:
Anuj Rathour 2023-10-09 21:03:34 +05:30 committed by GitHub
parent c6a22de12f
commit ced9678699
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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.
## Declaration
`Queue<Obj> queue = new PriorityQueue<Obj> ();`
`Queue<Obj> queue = new PriorityQueue<Obj>();`
## Important operations
| Operations | Description |
| ----------- | ----------- |
|Enqueue|Adds an item to the queue|
|Dequeue|Removes an item from the queue|
|Front|Gets the front item from the queue|
|Rear|Gets the last item from the queue|
| Operations | Description |Time Complexity
| ----------- | ----------- |-----------
|Enqueue|Adds an item to the queue|O(1)
|Dequeue|Removes an item from the queue|O(1)
|Front|Gets the front item from the queue|O(1)
|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.