队列(Queue)

队列(Queue)_第1张图片

队列(Queue)_第2张图片

队列(Queue)_第3张图片

队列(Queue)_第4张图片

队列(Queue)_第5张图片

队列(Queue)_第6张图片队列(Queue)_第7张图片队列(Queue)_第8张图片 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()

 

你可能感兴趣的:(数据结构,python)