Categories: Data Structurepython

Implementation of Deques in Python

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.

Method and Attributes πŸ€—πŸ‘‡

  • Deque() creates a new deque that is empty. It needs no parameters and returns an empty deque.
  • addFront(item) adds a new item to the front of the deque. It needs the item and returns nothing.
  • addRear(item) adds a new item to the rear of the deque. It needs the item and returns nothing.
  • removeFront() removes the front item from the deque. It needs no parameters and returns the item. The deque is modified.
  • removeRear() removes the rear item from the deque. It needs no parameters and returns the item. The deque is modified.
  • isEmpty() tests to see whether the deque is empty. It needs no parameters and returns a boolean value.
  • size() returns the number of items in the deque. It needs no parameters and returns an integer.

Now we are going to implement our own Deque class πŸ‘‡πŸ‘‡

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 πŸ‘ˆ

Follow Me ❀😊

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Recent Posts

What is object oriented design patterns

A design pattern isΒ a reusable solution to a commonly occurring problem in software design. They…

1 month ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

1 month ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

7 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

7 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

8 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

8 months ago