双端队列

class Deque():
    def __init__(self):
        self.__items=[]
    def is_empty(self):
        return self.__items==[]
    def length(self):
        return len(self.__items)
    def travel(self):
        if self.is_empty():
            return False
        for i in self.__items:
            print(i,end='')
        print('')
    # 尾部添加元素
    def add(self,item):
        self.__items.append(item)
    # 头部添加元素
    def insert(self,item):
        self.__items.insert(0,item)
    # 尾部删除元素
    def pop(self):
        self.__items.pop()
    # 头部删除元素
    def remove(self):
        self.__items.pop(0)
if __name__ == '__main__':
    dd=Deque()
    for i in range(5):
        dd.add(i)
    for i in range(5,10):
        dd.insert(i)
    dd.travel()
    print(dd.length())
    dd.pop()
    dd.travel()
    dd.remove()
    dd.travel()

 

posted @ 2019-01-06 18:46  名字到底要多长  阅读(163)  评论(0编辑  收藏  举报