
Surprised? Well, the Queue goest from left to right, while the arrows between the items go from right to left.Īctually, the items of the Queue can be represented by a simple Node class. dequeue: Removes the element from the front (left side) of. Let’s try to draw it and see what we can figure out. Queue Implementation in Python enqueue: Inserts an element at the rear (right side) of the queue. Well, we need to be able to keep an order of the Queue. How do you represent the above items of the Queue?
#Enqueue python how to#
Step 2: How to represent a Queue item with an element Normally, a Queue would also have a function is_empty, which checks whether the Queue is empty.

Python deque uses the opposite rule, LIFO queue, or last in first out. This resembles the real world, where we use queues in the grocery store, pharmacy, you name it. Python queue is a built in library that allows you to create a list that uses the FIFO rule, first in first out. One scenario is when a resource is shared among multiple consumers, then a Queue is set in front.
#Enqueue python update#
Then update tail to point at created Node. enqueue Create a new Node with the element.

A Queue is used in computer science in many scenarios. The enqueue function has the special case where the Queue is empty, otherwise it will do as follows.
