This chapter introduces the Queue data structure, emphasizing its First-In-First-Out (FIFO) principle. It covers operations like enqueue and dequeue, and provides implementation examples in Python, including Deque, a double-ended queue.
This chapter focuses on the Queue data structure, an essential topic in programming and computer science. It outlines the fundamental concepts of Queue including its operations and Python implementation while also introducing the Deque (Double-Ended Queue).
Queues are linear data structures that follow a First-In-First-Out (FIFO) principle. This means that the first element added to the queue will be the first one removed. The characteristics of a queue include:
These examples illustrate the practical applications of queues in day-to-day scenarios.
In a queue, the first element added is the first to be served, resembling a line of customers where the first person in line gets served first. This principle is crucial for maintaining order in many processes, including customer service and task scheduling.
Queues have several applications, including:
(A) Real-Life Applications:
(B) Computing Applications:
Queues facilitate several operations:
Using a simple queue implemented with a list:
1. ENQUEUE(A) -> [A]
2. ENQUEUE(B) -> [A, B]
3. DEQUEUE() -> [B]
4. ENQUEUE(C) -> [B, C]
5. Dequeue() -> [C] ```
## 4.3 Implementation of Queue Using Python
You can implement a queue in Python with lists. Here’s how:
- **Creating a Queue**:
```python myQueue = [] ```
- **Defining Functions**: Create functions for `enqueue`, `dequeue`, `isEmpty`, `peek`, and `size`. Each function should manage the queue operations effectively.
### Sample Code
```python def enqueue(myQueue, element):
myQueue.append(element)
...
def dequeue(myQueue):
if not isEmpty(myQueue):
return myQueue.pop(0)
... ```
## 4.4 Introduction to Deque
Deque, or double-ended queue, allows insertion and deletion from both ends. Key characteristics include:
- **INSERTFRONT**: Insert at the front.
- **INSERTREAR**: Insert at the rear.
- **DELETIONFRONT**: Remove from the front.
- **DELETIONREAR**: Remove from the rear.
### Applications of Deque
- Used in train ticket counters allowing customers to return to the queue from the front.
- Maintaining browser history or for 'undo' functions in text editors.
### Deque Operations Algorithm
To check if a string is a palindrome: 1) Insert all characters into a deque, 2) Remove from both ends to compare if they are the same.
## 4.5 Implementation of Deque Using Python
Similar to queues, deques can be implemented using lists with functions defined for insertion and deletion from both ends.
### Sample Code
```python def insertFront(myDeque, element):
myDeque.insert(0, element)
... ```
## Summary Remarks
This chapter delves into important data structures--Queue and Deque, highlighting their operations and applications within computer science and real-world scenarios. Understanding these concepts is fundamental for efficient data management in programming.