From ced96786990696589b86cc9fa3d8bc098510dc00 Mon Sep 17 00:00:00 2001 From: Anuj Rathour Date: Mon, 9 Oct 2023 21:03:34 +0530 Subject: [PATCH] Update queue readme (#4721) --- .../datastructures/queues/README.md | 57 ++++++++++++++++--- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/queues/README.md b/src/main/java/com/thealgorithms/datastructures/queues/README.md index ca5e2972f..ef1b89b5c 100644 --- a/src/main/java/com/thealgorithms/datastructures/queues/README.md +++ b/src/main/java/com/thealgorithms/datastructures/queues/README.md @@ -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 queue = new PriorityQueue ();` +`Queue queue = new PriorityQueue();` ## 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. +