Queue Program in Python

Queue Program in Python with Source Code. Queue widely used irrespective of programming language. Today we are showing the code in python. This python project is useful for beginners and CBSE KV School Class 11 and Class 12 students computer science practical file and NIELIT O Level Programming and Problem Solving through Python (Module M3-R5).

What is Queue in Computer Science ?

A queue is an abstract data type that holds an ordered, linear sequence of items. It is described as a first in, first out (FIFO) structure; the first element to be added to the queue will be the first element to be removed from the queue. New elements are added to the back or rear of the queue. When an element is removed, the remaining elements do not move up to take the empty space. To keep the order of the queue we need to maintain a pointer to the front, which indicates the element at the front of the queue, and one to the rear, which indicates the element at the back of the queue.

Objective : Write a menu based program to demonstrate operation on a stack

Source code screenshot of Queue Program in Python is given below.

Explanation of Python program code

#Indentation is not correct in below program the source code program at the end of this file has proper and tested indentation . Download it. #Here I will Explain the code

def isEmpty(qu): # a function is Empty(qu) is made which take qu as input
if len(qu)==0: #if lenght of qu is zero
return True #return true
else:
return False #return false
def ENQUEUE(qu,item): # for entering a object in queue.
qu.append(item) #add the item
if len(qu)==1:
rear=front=0
else:
rear=len(qu)-1
front=0
def DEQUEUE(qu): #for taking out an object deleting
if isEmpty(qu):
print(“UNDERFLOW CONDITION”)
else:
a= qu.pop(0)
print(“ELEMENT DELETED:”,a)

def peek(stk): #for peeking the rear element
return stk[-1]

def display(qu): #for displaying
if isEmpty(qu):
print(“NO ELEMENT PRESENT”)
else:
for i in range(len(qu)):
if i==0:
print(“FRONT”,qu[i])
elif i==len(qu)-1:
print(“REAR “,qu[i])
else:
print(” “,qu[i])

qu=[]
while True: # making the main search portal
print(“\t\t QUEUE OPERATIONS”)
print(“\t\t1.ENQUEUE”)
print(“\t\t2.DEQUEUE”)
print(“\t\t3.DISPLAY QUEUE”)
print(“\t\t4.PEEK”)
print(“\t\t5.EXIT”)
ch=int(input(“\t\tEnter your choice: “))
if ch==1:
x=input(“Enter the element to be inserted: “)
ENQUEUE(qu,x)
print(“ELEMENT HAS BEEN INSERTED”)
elif ch==2:
DEQUEUE(qu)
elif ch==3:
display(qu)
elif ch==4:
if isEmpty(qu):
print(“UNDERFLOW CONDITION”)
else:
print(peek(qu))
elif ch==5:
break
else:
print(“INVALID CHOICE ENTERED”)
print(“THANKS FOR USING MY SERVICES”)

Output of program

Testing of Python Program

First three element is added in the queue by enqueue -25,89,29
then we input for display which gives: front 25
89
Rear 29 which is correct
then we peeked and it showed 29 which is correct.
then we dequeue and 25 left the queue giving front 89 and Rear 29.

Testing is done and it passes with golden colour.

Run the python program and enter the required data.

Source code of Program -> click here

Thanks for reading and learning about this program.

for more python programs -> click here

Hello World Program in Python

Input a welcome message and display it in Python

Display the larger / smaller number in Python.

Greatest of Three Numbers in Python using Nested if

Patterns using nested loop in Python

Program to Print Pattern in Python

Program to input the value of x and n and sum of series in Python

Python Program for Armstrong, Prefect Number, Palindrome

Program of Prime number by recursion in python

Prime Number Program in Python

Write a Program to Print Fibonacci Series in Python

LCM and GCD program in Python

Python program to count number of vowels in a string

Whether a String is Palindrome or not in Python

Bubble Sort Program in Python

Linear search in python using list

Program to read a text file in python

Python program to read a file line by line

Program to Count Vowels and Consonants in Python

Stack Program in Python

Queue Program in Python

Python Leap Year Program for Beginners

Python Program to Print Series and Addition

Binary Search Program in Python

Program to find sum of digits in python

Sum of numbers divisible by 3 and 5 in python