Queue Data Structure with Python
5 min read
Queue data structure can be understand by railway ticket lines, whom come
first will get ticket first.
Queue can be defined as a data structure in which the first element entered
inside an array or a list will be removed first.
Let's try to understand with an example:-
print("Welcome to codewithdevops.")
print("Hit enter to input next item, enter r to remove and q to quit.")
queue = []
while True:
data = input("Enter the element: ")
if str.lower(data) == "q":
print("Breaking the loop....")
break
elif str.lower(data) == "r":
print("Removing:",queue.pop(0))
else:
queue.append(data)
print(queue)
]
for item in queue:
print("You entered:",item)
In this example you have to focus what is happening, first you can see a
statement trying to simplyfy functions for you, then empty queue is created.
for those functions a loop is working to give functions. that's it.
Here is an output of the program:-
Welcome to codewithdevops.
Hit enter to input next item, enter r to remove and q to quit.
Enter the element: 12
['12']
Enter the element: 23
['12', '23']
Enter the element: 34
['12', '23', '34']
Enter the element: 45
['12', '23', '34', '45']
Enter the element: 56
['12', '23', '34', '45', '56']
Enter the element: r
Removing: 12
['23', '34', '45', '56']
Enter the element: r
Removing: 23
['34', '45', '56']
Enter the element: r
Removing: 34
['45', '56']
Enter the element: r
Removing: 45
['56']
Enter the element: q
Breaking the loop....
You entered: 56
Above example is quite simple and easy to understand things very easily. Let's
make that example much more functional and good looking.
queue = []
def enqueue():
while True:
print("Select numbers according your element to
put in queue: ")
print("1.Int 2.Float 3.String 4.Bool 0.Quit")
choice_queue = int(input("Enter your choice: "))
if choice_queue == 1: queue_element = int(input("Enter
your integer value: "))
queue.append(queue_element)
print(queue_element,"is added to
queue")
elif choice_queue == 2: queue_element = float(input("Enter
your float value: "))
queue.append(queue_element)
print(queue_element,"is added to
queue")
elif choice_queue == 3: queue_element = input("enter the
element: ")
queue.append(queue_element)
print(queue_element,"is added to
queue")
elif choice_queue == 4: queue_element = int(input("enter the
element: "))
if queue_element < 1: queue.append(False)
print(False,"is added
to queue")
elif queue_element >= 1: queue.append(True)
print(True,"is added
to queue")
elif choice_queue == 0: break
else: print("Enter the correct option..")
def dequeue():
if not queue: print("queue is empty.") else: e = queue.pop(0)
print("Removed element",e)
def display():
print(queue)
while True:
print("Welcome to CodeWithDevops!")
print("Today you will see the queue Data Structure...")
print("Select the operation 1.add 2.remove 3.show 4.quit")
choice = int(input("Enter your choice: "))
if choice == 1:
enqueue()
elif choice == 2:
dequeue()
elif choice == 3:
display()
elif choice == 4:
break
else:
print("Enter the correct operation..")
If you look in this program each and every thing is functioning same but code
is totally changed.
Output is:-
Welcome to CodeWithDevops!
Today you will see the queue Data Structure...
Select the operation 1.add 2.remove 3.show 4.quit
Enter your choice: 1
Select numbers according your element to put in queue:
1.Int 2.Float 3.String 4.Bool 0.Quit
Enter your choice: 1
Enter your integer value: 12
12 is added to queue
Select numbers according your element to put in queue:
1.Int 2.Float 3.String 4.Bool 0.Quit
Enter your choice: 2
Enter your float value: 21
21.0 is added to queue
Select numbers according your element to put in queue:
1.Int 2.Float 3.String 4.Bool 0.Quit
Enter your choice: 3
enter the element: CodeWithDevops
CodeWithDevops is added to queue
Select numbers according your element to put in queue:
1.Int 2.Float 3.String 4.Bool 0.Quit
Enter your choice: 4
enter the element: 1
True is added to queue
Select numbers according your element to put in queue:
1.Int 2.Float 3.String 4.Bool 0.Quit
Enter your choice: 0
Welcome to CodeWithDevops!
Today you will see the queue Data Structure...
Select the operation 1.add 2.remove 3.show 4.quit
Enter your choice: 3
[12, 21.0, 'CodeWithDevops', True]
Welcome to CodeWithDevops!
Today you will see the queue Data Structure...
Select the operation 1.add 2.remove 3.show 4.quit
Enter your choice: 2
Removed element 12
Welcome to CodeWithDevops!
Today you will see the queue Data Structure...
Select the operation 1.add 2.remove 3.show 4.quit
Enter your choice: 2
Removed element 21.0
Welcome to CodeWithDevops!
Today you will see the queue Data Structure...
Select the operation 1.add 2.remove 3.show 4.quit
Enter your choice: 2
Removed element CodeWithDevops
Welcome to CodeWithDevops!
Today you will see the queue Data Structure...
Select the operation 1.add 2.remove 3.show 4.quit
Enter your choice: 2
Removed element True
Welcome to CodeWithDevops!
Today you will see the queue Data Structure...
Select the operation 1.add 2.remove 3.show 4.quit
Enter your choice: 2
queue is empty.
Welcome to CodeWithDevops!
Today you will see the queue Data Structure...
Select the operation 1.add 2.remove 3.show 4.quit
Enter your choice: 4
There is a module in Python named as collections if you import it in your
program it provides you much more functionality.
In below program you will be introduced with deque() method that works same as
queue.
import collections
queue = collections.deque()
queue.appendleft(10)
queue.appendleft(20)
queue.appendleft(30)
print(queue)
print(queue.pop())
Above program have soe changes like appendleft() method, this method add
values to the left side of list.
Output is:-
deque([30, 20, 10])
10
There is one more way to solve queue data structure i.e. queue module.
import queue
a = queue.Queue()
a.put(10)
a.put(20)
a.put(30)
a.put(40)
a.put(50)
print(a)
print(a.get())
print(a.get())
print(a.get())
Here you can see that after importing queue module you get 2 more methods i.e.
put and get. They are similar to append and pop so no need of explanation to
them.
Output is:-
<queue.Queue object at 0x000001F160357FD0>
10
20
30