# 641. Design Circular Deque¶

## Information¶

• Diffculty: Medium

• Created: 2000-01-01 00:00:00

• Last Motified: 2000-01-01 00:00:00

## Solution¶

```#!/usr/bin/env python
# coding=utf-8
#
# Author: Lucas
# Date: 2019-09-01 23:35:44

class MyCircularDeque:
def __init__(self, k: int):
"""
Initialize your data structure here. Set the size of the deque to be k.
"""
self.size = k
self.data = [None] * (3 * k)
self.start = k
self.end = k

def insertFront(self, value: int) -> bool:
"""
Adds an item at the front of Deque. Return true if the operation is successful.
"""
if self.isFull():
return False

if self.start == 0:
for i in range(self.start, self.end):
self.data[i + self.size] = self.data[i]
self.data[i] = None
self.start += self.size
self.end += self.size
self.start -= 1
self.data[self.start] = value
return True

def insertLast(self, value: int) -> bool:
"""
Adds an item at the rear of Deque. Return true if the operation is successful.
"""
if self.isFull():
return False

if self.end == len(self.data):
for i in range(self.start, self.end):
self.data[i - self.size] = self.data[i]
self.data[i] = None
self.start -= self.size
self.end -= self.size
self.data[self.end] = value
self.end += 1
return True

def deleteFront(self) -> bool:
"""
Deletes an item from the front of Deque. Return true if the operation is successful.
"""
if self.isEmpty():
return False
self.data[self.start] = None
self.start += 1
return True

def deleteLast(self) -> bool:
"""
Deletes an item from the rear of Deque. Return true if the operation is successful.
"""
if self.isEmpty():
return False
self.end -= 1
self.data[self.end] = None
return True

def getFront(self) -> int:
"""
Get the front item from the deque.
"""
if self.isEmpty():
return -1
return self.data[self.start]

def getRear(self) -> int:
"""
Get the last item from the deque.
"""
if self.isEmpty():
return -1
return self.data[self.end - 1]

def isEmpty(self) -> bool:
"""
Checks whether the circular deque is empty or not.
"""
return self.start == self.end

def isFull(self) -> bool:
"""
Checks whether the circular deque is full or not.
"""
return self.end - self.start >= self.size

# Your MyCircularDeque object will be instantiated and called as such:
# obj = MyCircularDeque(k)
# param_1 = obj.insertFront(value)
# param_2 = obj.insertLast(value)
# param_3 = obj.deleteFront()
# param_4 = obj.deleteLast()
# param_5 = obj.getFront()
# param_6 = obj.getRear()
# param_7 = obj.isEmpty()
# param_8 = obj.isFull()
```