数据结构——2

xiaoxiao2021-02-28  5

class Queue(object): """队列(双端队列)""" def __init__(self): """私有化,封装""" self.queue = [] def enqueue(self,item): "从尾部往队列中添加一个元素" self.queue.append(item) def enqueue_start(self,item): """从头部添加元素(双)""" self.queue.insert(0,item) def dequeue(self): """从对列头部删除一个元素""" return self.queue.pop(0) def dequeue_end(self): """从队列尾部删除一个元素(双)""" return self.queue.pop() def is_empty(self): """判空""" return len(self.queue) == 0 def size(self): """返回队列大小""" return len(self.queue) if __name__ == "__main__": s = Queue() print(type(s)) s.enqueue(1) s.enqueue(2) s.enqueue(3) s.enqueue(4) print(s.is_empty()) print(s.size()) print(s.dequeue()) print(s.dequeue()) print(s.dequeue()) print(s.dequeue()) s.enqueue_start(5) s.enqueue_start(6) s.enqueue_start(7) print(s.size()) print(s.dequeue_end()) print(s.dequeue_end()) print(s.dequeue_end())
转载请注明原文地址: https://www.6miu.com/read-2350037.html

最新回复(0)