class Node:
def init(self,data):
self.data=data
self.next=None
self.prev=None
class MyCircularDeque:
def __init__(self, k: int):
"""
Initialize your data structure here. Set the size of the deque to be k.
"""
self.head=None
self.tail=None
self.size=k
self.length=0
def insertFront(self, value: int) -> bool:
"""
Adds an item at the front of Deque. Return true if the operation is successful.
"""
if(self.length <self.size):
newNode=Node(value)
if self.head == None:
self.head=newNode
self.tail=self.head
self.length+=1
return True
else:
newNode.next=self.head
self.head.prev= newNode
self.head=newNode
self.length+=1
return True
else:
return False
def insertLast(self, value: int) -> bool:
"""
Adds an item at the rear of Deque. Return true if the operation is successful.
"""
if(self.length<self.size):
newNode=Node(value)
if self.head == None:
self.head=newNode
self.tail=self.head
self.length+=1
return True
else:
self.tail.next= newNode
newNode.prev=self.tail
self.tail=newNode
self.length+=1
return True
else:
return False
def deleteFront(self) -> bool:
"""
Deletes an item from the front of Deque. Return true if the operation is successful.
"""
if self.length>0:
self.head=self.head.next
self.length-=1
return True
else:
return False
def deleteLast(self) -> bool:
"""
Deletes an item from the rear of Deque. Return true if the operation is successful.
"""
if self.length>0:
self.tail=self.tail.prev
self.tail.next = None
self.length-=1
return True
else:
return False
def getFront(self) -> int:
"""
Get the front item from the deque.
"""
if(self.head!=None):
return self.head.data
else:
return -1
def getRear(self) -> int:
"""
Get the last item from the deque.
"""
if(self.head != None):
return self.tail.data
else:
return -1
def isEmpty(self) -> bool:
"""
Checks whether the circular deque is empty or not.
"""
if self.length==0:
return True
else:
return False
def isFull(self) -> bool:
"""
Checks whether the circular deque is full or not.
"""
if self.length ==self.size:
return True
else:
return False