vault backup: 2025-01-30 09:27:31
This commit is contained in:
120
Data Structures/AI Notes/Queues.md
Executable file
120
Data Structures/AI Notes/Queues.md
Executable file
@@ -0,0 +1,120 @@
|
||||
# Abstract Data Type: Queue
|
||||
|
||||
## Introduction to Queues
|
||||
|
||||
A queue is an important abstract data type (ADT) characterised by its FIFO (First In, First Out) structure. Elements are added to the queue from one end (the rear) and removed from the other end (the front). This structure is particularly useful in various applications where order is vital, such as in CPU scheduling and asynchronous data transfer between processes.
|
||||
|
||||
## Key Concepts of Queues
|
||||
|
||||
- **Structure:** A queue consists of two primary ends:
|
||||
|
||||
- _Front:_ The position from which elements are removed.
|
||||
|
||||
- _Rear:_ The position where elements are added.
|
||||
|
||||
- **Core Operations:** The basic operations associated with a queue are:
|
||||
|
||||
- **isEmpty():** Checks if the queue is empty.
|
||||
|
||||
- **peek():** Returns the front element without removing it.
|
||||
|
||||
- **put(x):** Adds an element x at the rear of the queue.
|
||||
|
||||
- **remove():** Removes and returns the front element.
|
||||
|
||||
|
||||
## Queue Applications
|
||||
|
||||
Queues have various applications due to their structure, such as:
|
||||
|
||||
- CPU and disk scheduling, where resources are shared among multiple consumers.
|
||||
|
||||
- Buffering in asynchronous data transfers like I/O operations, where data may not be received at the same rate as it is sent.
|
||||
|
||||
- Handling jobs in a network printer which generally follows a FIFO order.
|
||||
|
||||
|
||||
## Queue Representation
|
||||
|
||||
Queues can be implemented in two ways:
|
||||
|
||||
- **Linear Array Queue:** Uses a one-dimensional array along with pointers for front and rear.
|
||||
|
||||
- **Circular Array Queue:** Employs a circular structure to efficiently utilize array space, preventing the needs of shifting elements when the array is full.
|
||||
|
||||
|
||||
## Linear Queue Representation
|
||||
|
||||
In a linear array, after several operations, the queue might appear as:
|
||||
|
||||
``` java
|
||||
Queue: A B C D (front = 1, rear = 4)
|
||||
```
|
||||
|
||||
## Circular Queue Representation
|
||||
|
||||
A circular queue, uses the following logic:
|
||||
|
||||
- Front points 'anti-clockwise' from the first element while rear points to the last inserted element.
|
||||
|
||||
- Utilizes the modulo operator to wrap around the indices as elements are added or removed.
|
||||
|
||||
|
||||
## Implementation of a Circular Queue
|
||||
|
||||
The implementation comprises several stages:
|
||||
|
||||
1. **Initialization:** Set front and rear to 0.
|
||||
|
||||
2. **Insertion:** Update the rear index with a modulo operation. For example, rear=(rear+1)\%queue.length.
|
||||
|
||||
3. **Removal:** Increment the front index to remove an element.
|
||||
|
||||
|
||||
## Challenges with Circular Queues
|
||||
|
||||
One of the critical issues with circular queues is distinguishing between a full and an empty queue state; both conditions can show the same value for front and rear. A common solution is to maintain a count of elements or add a condition to never allow the queue to get fully utilized.
|
||||
|
||||
## Queue Implementation Using Array
|
||||
|
||||
The _ArrayQueue_ class illustrates array-based implementation:
|
||||
|
||||
``` java
|
||||
public class ArrayQueue implements Queue { ... }
|
||||
```
|
||||
|
||||
This class defines data members like _front_ and _rear_, along with methods for manipulating the queue.
|
||||
|
||||
## Queue Methods Overview
|
||||
|
||||
Here are key methods defined in the Queue interface:
|
||||
|
||||
1. **isEmpty():** Returns true if the queue is empty.
|
||||
|
||||
2. **peek():** Returns the front element or null if the queue is empty.
|
||||
|
||||
3. **put(Object theObject):** Adds an element to the queue and may increase array length if the queue is full.
|
||||
|
||||
4. **remove():** Removes the front element and updates the front pointer to the next element.
|
||||
|
||||
|
||||
## Queue Implementation Using Linked List
|
||||
|
||||
A linked list can also be utilized for queue implementation.
|
||||
|
||||
``` java
|
||||
public class LinkedQueue implements Queue { ... }
|
||||
```
|
||||
|
||||
This structure provides constant time complexity for insertions and deletions, offering an efficient alternative to array-based queues.
|
||||
|
||||
## Summarizing Queue Operations
|
||||
|
||||
- **Adding Elements:** In a linked implementation, a new node is created and linked appropriately.
|
||||
|
||||
- **Removing Elements:** The front element is removed and the front pointer is updated.
|
||||
|
||||
|
||||
## Conclusion
|
||||
|
||||
Queues are fundamental data structures in computer science with a range of applications spanning from scheduling tasks to implementing buffers. Their efficient management of elements through well-defined operations supports numerous algorithms and data handling strategies in programming.
|
Reference in New Issue
Block a user