class myDeque:
def __init__(self,iterable=None,maxlen=4):
if iterable==None:
self._content=[]
self._current=0
else:
self._content=list(iterable) #队列内容
self._current=len(iterable) #队列长度
self._size=maxlen #队列的最大长度
if self._size<self._current:
self._size=self._current
def __del__(self):
del self._content
#修改队列大小方法1
def setSize1(self,size):
if size<self._current:
for i in range(size,self._current)[::-1]: #利用逆序从后面删除
del self._content[i]
self._size=size
def setSize2(self,size):
if size<self._current:
self._content=self._content[:size]
self._size=size
#右入队
def appendright(self,element):
if self._current<self._size:
self._content.append(element)
self._current+=1
else:
print('quen is full')
#左入队
def appendleft(self,element):
if self._current<self._size:
self._content.insert(0,element)
self._current+=1
else:
print('quen is full')
#右出队
def popright(self):
if not self.isEmpty():
self._content.pop(0)
self._current-=1
else:
print('quen is empty')
#左出队
def popleft(self):
if not self.isEmpty():
self._content.pop()
self._current-=1
else:
print('quen is empty',self.isEmpty())
#队列置空
def clear(self):
self._content=[]
self._current=0
#判空
def isEmpty(self):
return not self._current
#判满
def isfull(self):
if self._current==self._size:
return True
else:
return False
#获取队列信息
def get(self):
print(self._content,self._current,self._size)
if __name__ == '__main__':
myquen=myDeque([1,3,5])
# myquen.setSize1()
# myquen.appendleft(5)
myquen.isfull()
# myquen.isEmpty()
# myquen.get()
|