All Courses

How to implement Deque in python using list?

By Neha, 2 years ago
  • Bookmark
0

Implement deque data structure using list methods

Deque
List
Python
1 Answer
0

Implementation of Deque :

Methods 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.


Deque Implementation :


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)
   
  def print(self):           
    return self.items


d = Deque()

d.addFront('hello')

d.addRear('world')

d.print() # output ['world', 'hello']

d.size() # output 2

print (d.removeFront() + ' ' +  d.removeRear()) # output  hello world

d.size() # output 0

Your Answer

Webinars

More webinars

Related Discussions

Running random forest algorithm with one variable

View More