How to Implement a Queue Using queue Module in Python


In this example, we will learn how to output a queue using the queue module in Python.

Syntax

Below shows the constructor for a FIFO queue.


class queue.Queue(maxsize=0)

When maxsize is set to less than or equal to 0, the queue size is infinite.

Source Code


#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import queue

tmp = int(input('please input num of element'))
q = queue.Queue()

for i in range(tmp):
    Element = int(input('please input element: '))
    q.put(Element)

while not q.empty():
    print (q.get())

Output:

please input num of element5
please input element: 2
please input element: 3
please input element: 1
please input element: 4
please input element: 5
2
3
1
4
5
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement