class Dqueue():
# 双端队列实现
def __init__(self):
# 依然使用列表来存放元素
self.__dequeue = []
def add_front(self, item):
self.__dequeue.insert(0,item)
def add_rear(self, item):
self.__dequeue.append(item)
def remove_front(self):
return self.__dequeue.pop(0)
def remove_rear(self):
return self.__dequeue.pop(-1)
pass
def is_empty(self):
return self.__dequeue==[]
def size(self):
return len(self.__dequeue)
if __name__=='__main__':
dequeue = Dqueue()
dequeue.add_front(1)
dequeue.add_rear(2)
dequeue.add_rear(3)
dequeue.add_front(4)
print(dequeue.remove_front())
print(dequeue.remove_rear())
print(dequeue.is_empty())
print(dequeue.size())
python数据结构之双端队列
最新推荐文章于 2022-08-23 20:23:16 发布