双端队列是队列的扩展,可以在队列两端进行插入和删除。
代码实现
# -*- coding = utf-8 -*- # @Author: Wchime # @time: 2023/1/23 13:59 # @file: 双端队列.py class DoubleQueue(object): """ 双端队列 """ def __init__(self): self.__queue = [] def is_empty(self): """ 判断队列是否为空 :return: """ return self.__queue == [] def get_length(self): """ 获取队列长度 :return: """ return len(self.__queue) def append_front(self, item): """ 在头部插入元素 :param item: :return: """ self.__queue.insert(0, item) def append_back(self, item): """ 在尾部插入元素 :param item: :return: """ self.__queue.append(item) def pop_front(self): """ 在头部取元素 :return: """ return self.__queue.pop(0) def pop_back(self): """ 在尾部取元素 :return: """ return self.__queue.pop() if __name__ == "__main__": queue = DoubleQueue() print(queue.is_empty()) queue.append_front(2) queue.append_back(9) queue.append_front(5) queue.append_back(3) queue.append_front(1) queue.append_back(7) queue.append_front(6) queue.append_back(8) print(queue.get_length()) print(queue.pop_front()) print(queue.pop_back()) print(queue.pop_back()) print(queue.get_length()) print(queue.is_empty())
标签:return,队列,双端,self,back,queue,append From: https://www.cnblogs.com/moon3496694/p/17070945.html