队列(Queue)

 python完整代码:

from collections import deque

class Test:
    def test(self):
        # Create a queue
        queue = deque()

        # Add element
        # Time Complexity: O(1)
        queue.append(1)
        queue.append(2)
        queue.append(3)
        # [1,2,3]
        print(queue)

        # Get the head of the queue
        # Time Complexity: O(1)
        temp1 = queue[0]
        # 1
        print(temp1)

        # Remove the head of the queue
        # Time Complexity: O(1)
        temp2 = queue.popleft()
        # 1
        print(temp2)
        # [2,3]
        print(queue)

        # Queue is empty?
        # Time Complexity: O(1)
        len(queue) == 0

        # The length of queue
        # Time Complexity: O(1)
        len(queue)

        # Time Complexity: O(N)
        while len(queue) != 0:
            temp = queue.popleft()
            print(temp)

if __name__ == "__main__":
    test = Test()
    test.test()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值