python里边有的
append,appendleft,clear,count,extend,extendleft,pop,popleft,remove…
最基础有:[append, appendleft, pop, popleft] 希望是O(1)的时间复杂度
而在过去的内容中有循环双端队列是可以实现的,在这里就将其继承,然后实现以上功能就好了。
都比较简单。
# -*- coding:utf-8 -*-
# Author: Greed_Vic(PL Z)
# Product_name: PyCharm
# File_name: Deque
# @Time: 13:45 2021/6/9
from CircularDoubleLinkedList import *
class DeQue(object):
def __init__(self, maxsize):
self.maxsize = maxsize
self.deq = CircularDoubleLinkedList()
def append(self, val):
self.deq.append(val)
def appendleft(self, val):
self.deq.appendleft(val)
def poptail(self):
val = self.deq.tailnode().val
self.deq.remove(self.deq.tailnode())
return val
def pophead(self):
val = self.deq.headnode().val
self.deq.remove(self.deq.headnode())
return val
if __name__ == '__main__':
def test():
dq = DeQue(12)
for i in range(10):
dq.append(i)
assert [i for i in dq.deq] == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(1, 4):
dq.appendleft(i * 10)
assert [i for i in dq.deq] == [30, 20, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
assert 30 == dq.pophead()
assert 9 == dq.poptail()
print("Test Done")
test()