New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs

Implementing Queue in Python


Queue is a simple data structure which works on the simple principle of "First in First out" just like an ordinary queue at a coffee shop or ticket counters etc, where the first one to enter the queue gets served first.

In a queue, a new element is added from the rear, and existing elements are removed from the front.

Basic Queue Data Structure

As you can see in the diagram above, new elements are added from the back, and this function of adding a new element to the queue is called Enqueue. And elements are removed from the front, which is called Dequeue.

Hence, to create a simple queue data structure, we need to provide it with the following functions:

  1. enqueue: To add a new data element at the rear of the queue.
  2. dequeue: To remove an element from the front of the queue.
  3. isEmpty: To return True if the queue is empty, else return False.
  4. size: To check the size of the queue, in other words count the number of elements in the queue and return it.
  5. show: To print all the queue elements.

We will be using Python List for implementing queue data structure.

NOTE: We can also use the dequeue library to implement Queue in python and we can also use arrays.

Try changing the code as you like. Click the button, to Run the code again.