Queues are a fundamental data structure in computer science, characterized by their First-In-First-Out (FIFO) property. This means that the first element added to the queue will be the first one to be removed. In this section, we will cover the basic operations that can be performed on queues, including enqueue, dequeue, peek, and checking if the queue is empty.
Key Concepts
- Enqueue: Adding an element to the end of the queue.
- Dequeue: Removing an element from the front of the queue.
- Peek: Viewing the element at the front of the queue without removing it.
- IsEmpty: Checking if the queue is empty.
Enqueue Operation
The enqueue operation adds an element to the end of the queue. Here is a simple implementation in Python:
class Queue:
def __init__(self):
self.items = []
def enqueue(self, item):
self.items.append(item)
print(f"Enqueued: {item}")
# Example usage
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)Explanation
__init__method: Initializes an empty list to store the queue elements.enqueuemethod: Appends the new item to the end of the list.
Dequeue Operation
The dequeue operation removes an element from the front of the queue. Here is how you can implement it:
class Queue:
def __init__(self):
self.items = []
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
if not self.is_empty():
item = self.items.pop(0)
print(f"Dequeued: {item}")
return item
else:
print("Queue is empty")
return None
def is_empty(self):
return len(self.items) == 0
# Example usage
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.dequeue()
queue.dequeue()
queue.dequeue()
queue.dequeue() # This will print "Queue is empty"Explanation
dequeuemethod: Removes and returns the first item in the list if the queue is not empty. If the queue is empty, it prints a message and returnsNone.is_emptymethod: Checks if the list is empty.
Peek Operation
The peek operation allows you to view the element at the front of the queue without removing it:
class Queue:
def __init__(self):
self.items = []
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
else:
return None
def peek(self):
if not self.is_empty():
return self.items[0]
else:
return None
def is_empty(self):
return len(self.items) == 0
# Example usage
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
print(f"Front item: {queue.peek()}") # This will print "Front item: 1"Explanation
peekmethod: Returns the first item in the list without removing it if the queue is not empty. If the queue is empty, it returnsNone.
IsEmpty Operation
The is_empty operation checks if the queue is empty:
class Queue:
def __init__(self):
self.items = []
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
else:
return None
def peek(self):
if not self.is_empty():
return self.items[0]
else:
return None
def is_empty(self):
return len(self.items) == 0
# Example usage
queue = Queue()
print(f"Is queue empty? {queue.is_empty()}") # This will print "Is queue empty? True"
queue.enqueue(1)
print(f"Is queue empty? {queue.is_empty()}") # This will print "Is queue empty? False"Explanation
is_emptymethod: ReturnsTrueif the list is empty andFalseotherwise.
Summary
In this section, we covered the basic operations that can be performed on queues:
- Enqueue: Adding an element to the end of the queue.
- Dequeue: Removing an element from the front of the queue.
- Peek: Viewing the element at the front of the queue without removing it.
- IsEmpty: Checking if the queue is empty.
These operations form the foundation for working with queues and are essential for understanding more complex data structures and algorithms. In the next section, we will explore circular queues and their applications.
Data Structures Course
Module 1: Introduction to Data Structures
Module 2: Lists
Module 3: Stacks
- Introduction to Stacks
- Basic Operations with Stacks
- Stack Implementation
- Applications of Stacks
- Exercises with Stacks
Module 4: Queues
- Introduction to Queues
- Basic Operations with Queues
- Circular Queues
- Priority Queues
- Exercises with Queues
Module 5: Trees
Module 6: Graphs
- Introduction to Graphs
- Graph Representation
- Graph Search Algorithms
- Shortest Path Algorithms
- Applications of Graphs
- Exercises with Graphs
