0% found this document useful (0 votes)
3 views7 pages

Group - 1 Data Structures

A dequeue, or double-ended queue, is a versatile linear data structure that allows insertion and deletion from both ends. Common operations include adding and removing items from the front and rear, as well as checking if the dequeue is empty or full. Dequeues are useful in various applications such as implementing undo/redo functionality and processing data from multiple sources.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Group - 1 Data Structures

A dequeue, or double-ended queue, is a versatile linear data structure that allows insertion and deletion from both ends. Common operations include adding and removing items from the front and rear, as well as checking if the dequeue is empty or full. Dequeues are useful in various applications such as implementing undo/redo functionality and processing data from multiple sources.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Dequeue (Double-

Ended Queue)
GROUP 1
• A dequeue, also known as a double-ended
queue, is a linear data structure that
allows insertion and deletion from both
ends. This makes it more versatile than a
traditional queue, which only allows
insertion from the rear and removal from
the front.
Operations

Here are the common operations performed on a


dequeue:
- enqueue_front(item) : Adds an item to the front of
the dequeue.
- enqueue_rear(item) : Adds an item to the rear of
the dequeue.
- dequeue_front() : Removes and returns the item at
the front of the dequeue.
- dequeue_rear() : Removes and returns the item at
the rear of the dequeue.
- is_empty() : Checks if the dequeue is empty.
- is_full() : Checks if the dequeue is full (if using a
fixed- size implementation).
Python Implementation
class Deque:
def __init__(self):
self.items = []

def enqueue_front(self, item):

self.items.insert(0, item)

def enqueue_rear(self,item):

self.items.append(item)

def dequeue_front(self):
if not self.is_empty():
return self.items.pop(0)

else:
def dequeue_rear(self):
if not self.is_empty():
return self.items.pop()
else:
return None

def is_empty(self):
return len(self.items) == 0

def is_full(self):
# This implementation doesn't have a
fixed size, so always
returns False.
return False
# Example usage
my_deque = Deque()
my_deque.enqueue_front(10)
my_deque.enqueue_rear(20)
my_deque.enqueue_front(5)
print(my_deque.dequeue_front())
# Output:
5print(my_deque.dequeue_rear())
# Output: 20print(my_deque.items)
# Output: [10]
Key Points

- Flexibility: Dequeues offer more flexibility than traditional


queues due to the ability to add and remove elements from
both ends.
- Applications: Dequeues are commonly used in various
applications, including:
- Implementing undo/redo functionality: You can store the
previous actions in a dequeue, allowing users to undo or
redo their operations.
- Processing data from multiple sources: A dequeue can be
used to hold data received from different sources and
process it in a specific order.
- Implementing a circular buffer: A circular buffer is a fixed-
size data structure that uses a dequeue to manage data
efficiently.

You might also like