A deque, also known as a double-ended queue, is an ordered collection of items similar to the queue. It has two ends, a front, and rear, and the items remain positioned in the collection.
What makes a deque different is the unrestrictive nature of adding and removing items. New items can be added at either the front or the rear. Likewise, existing items can be removed from either end.
In a sense, this hybrid linear structure provides all the capabilities of stacks and queues in a single data structure.
It is important to note that even though the deque can assume many of the characteristics of stacks and queues, it does not require the LIFO and FIFO orderings that are enforced by those data structures. It is up to you to make consistent use of the addition and removal operations.
class Deque: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def addFront(self, item): self.items.append(item) def addRear(self, item): self.items.insert(0,item) def removeFront(self): return self.items.pop() def removeRear(self): return self.items.pop(0) def size(self): return len(self.items)
d = Deque() d.addFront('hello') d.addRear('world') d.size()
Output: 2
print(d.removeFront() + ' ' + d.removeRear())
Output: hello world
d.size()
Output: 0
Recommended Reading:
Implementation of Queue in Python 👈
Implementation of Stack in Python 👈
If you like my post please follow me to read my latest post on programming and technology.
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…
Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…