Cara menggunakan queue using array python

Queue in Python

Improve Article

Save Article

Like Article

  • Difficulty Level : Easy
  • Last Updated : 10 Jan, 2023

  • Read
  • Discuss
  • Courses
  • Practice
  • Video
  • Improve Article

    Save Article

    Like stack, queue is a linear data structure that stores items in First In First Out (FIFO) manner. With a queue the least recently added item is removed first. A good example of queue is any queue of consumers for a resource where the consumer that came first is served first.
     

    Operations associated with queue are: 
     

    • Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition – Time Complexity : O(1)
    • Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition – Time Complexity : O(1)
    • Front: Get the front item from queue – Time Complexity : O(1)
    • Rear: Get the last item from queue – Time Complexity : O(1)

     

    Implementation

    There are various ways to implement a queue in Python. This article covers the implementation of queue using data structures and modules from Python library.
    Queue in Python can be implemented by the following ways:
     

    • list
    • collections.deque
    • queue.Queue

     

    Implementation using list

    List is a Python’s built-in data structure that can be used as a queue. Instead of enqueue() and dequeue(), append() and pop() function is used. However, lists are quite slow for this purpose because inserting or deleting an element at the beginning requires shifting all of the other elements by one, requiring O(n) time.
     

    Python3




    # Python program to 

    # demonstrate queue implementation

    # using list

      

    # Initializing a queue

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 0Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 1 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 2

      

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 4

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 5Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 5Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 9Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 2Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

      

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 7Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque 0

      

    Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque 2

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque 5Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque 8Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque 90 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False 0

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque 8Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque 90 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False 0

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque 8Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque 90 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False 0

      

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6# Python program to 2Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque 0

      

    # Python program to 7

    # Python program to 8

    # Python program to 9

    Output: 
     

    Initial queue ['a', 'b', 'c'] Elements dequeued from queue a b c Queue after removing elements []

     

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list

     

    Implementation using collections.deque

    Queue in Python can be implemented using deque class from the collections module. Deque is preferred over list in the cases where we need quicker append and pop operations from both the ends of container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity. Instead of enqueue and deque, append() and popleft() functions are used.
     

    Python3




    # demonstrate queue implementation0

    # demonstrate queue implementation

    # demonstrate queue implementation2

      

      

    # demonstrate queue implementation5 # demonstrate queue implementation6# demonstrate queue implementation7 # demonstrate queue implementation8

      

    # Initializing a queue

    # using list1Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 1 # using list3

      

    # using list5

    # using list6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    # using list6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 9Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    # using list6Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 2Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

      

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 7Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5# Initializing a queue1

      

    # Initializing a queue3

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6# Initializing a queue6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5# Initializing a queue9

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5# Initializing a queue9

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5# Initializing a queue9

      

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6# Python program to 2Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5# Initializing a queue1

      

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 12

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 13

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 14

    Output: 
     

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([])

     

    Traceback (most recent call last): File "/home/b2fa8ce438c2a9f82d6c3e5da587490f.py", line 23, in q.popleft() IndexError: pop from an empty deque

     

    Implementation using queue.Queue

    Queue is built-in module of Python which is used to implement a queue. queue.Queue(maxsize) initializes a variable to a maximum size of maxsize. A maxsize of zero ‘0’ means a infinite queue. This Queue follows FIFO rule. 
    There are various functions available in this module: 
     

    • maxsize – Number of items allowed in the queue.
    • empty() – Return True if the queue is empty, False otherwise.
    • full() – Return True if there are maxsize items in the queue. If the queue was initialized with maxsize=0 (the default), then full() never returns True.
    • get() – Remove and return an item from the queue. If queue is empty, wait until an item is available.
    • get_nowait() – Return an item if one is immediately available, else raise QueueEmpty.
    • put(item) – Put an item into the queue. If the queue is full, wait until a free slot is available before adding the item.
    • put_nowait(item) – Put an item into the queue without blocking. If no free slot is immediately available, raise QueueFull.
    • qsize() – Return the number of items in the queue.

     

    Python3




    # demonstrate queue implementation0

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 16

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 17

      

      

    # demonstrate queue implementation5 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 0# demonstrate queue implementation7 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 23

      

    # Initializing a queue

    # using list1Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 1 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 28Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 1 Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 30Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

      

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 33

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 34

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 36

      

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 38

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 39Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 39Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 9Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 39Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 2Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

      

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 49

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 50

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 53Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 54

      

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 56

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6# Initializing a queue6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 62

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 62

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 62

      

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 68

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 50

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 72Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 73

      

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 39Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 76Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 7

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 72Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 81

    Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 5Initial queue deque(['a', 'b', 'c']) Elements dequeued from the queue a b c Queue after removing elements deque([]) 6Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 84Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 85

      

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 87

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 88

    Traceback (most recent call last): File "/home/ef51acf025182ccd69d906e58f17b6de.py", line 25, in print(queue.pop(0)) IndexError: pop from empty list 89

    Output: 
     

    0 Full: True Elements dequeued from the queue a b c Empty: True Empty: False Full: False

     

    Recommended

    Solve DSA problems on GfG Practice.

    Solve Problems


    My Personal Notes arrow_drop_up

    Save

    Please Login to comment...

    Apakah queue dapat diimplementasikan menggunakan array?

    Operasi pada queue dapat diimplementasikan dengan menggunakan array, structure (record) dan pointer.

    Bagaimana cara kerja queue?

    Queue adalah struktur data linier yang menerapkan prinsip operasi dimana elemen data yang masuk pertama akan keluar lebih dulu. Prinsip ini dikenal dengan istilah FIFO (First In, First Out).

    Apa itu struktur data queue?

    Queue atau antrian adalah suatu kumpulan data yang penambahan elemennya hanya bisa dilakukan pada suatu ujung (disebut dengan sisi belakang atau rear), dan penghapusan atau pengambilan elemen dilakukan lewat ujung yang lain (disebut dengan sisi depan atau front).

    Append python buat apa?

    Append. Salah satu fitur dalam array python yang cukup sering digunakan adalah fungsi append. Fungsi append ini berguna untuk menambahkan nilai array pada urutan terakhir. Fungsi ini sedikit berbeda dengan fungsi insert, dimana fungsi insert bisa menambahkan nilai array pada posisi tertentu.

    Postingan terbaru

    LIHAT SEMUA