|
31 | 31 | - **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.
|
32 | 32 |
|
33 | 33 | ## Declaration
|
34 |
| - |
35 |
| -`Queue<Obj> queue = new PriorityQueue<Obj> ();` |
| 34 | +`Queue<Obj> queue = new PriorityQueue<Obj>();` |
36 | 35 |
|
37 | 36 | ## Important operations
|
38 | 37 |
|
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 | + |
45 | 86 |
|
46 | 87 |
|
47 | 88 |
|
|
0 commit comments