0% found this document useful (0 votes)
5 views11 pages

2.data Structures in Python-Queue

A queue is a linear data structure that follows the FIFO (First In First Out) principle, allowing insertion at the rear and deletion from the front. It includes operations such as Enqueue for insertion and Dequeue for deletion, with real-life applications like printer spooling and CPU scheduling. The document also provides Python code examples for implementing queue operations using lists, including functions for inserting, deleting, and displaying elements.

Uploaded by

diyavrghs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

2.data Structures in Python-Queue

A queue is a linear data structure that follows the FIFO (First In First Out) principle, allowing insertion at the rear and deletion from the front. It includes operations such as Enqueue for insertion and Dequeue for deletion, with real-life applications like printer spooling and CPU scheduling. The document also provides Python code examples for implementing queue operations using lists, including functions for inserting, deleting, and displaying elements.

Uploaded by

diyavrghs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Queue

• Queue is a linear list which follows FIFO (First In First Out)


approach.

• Queue allows insertion of element only at one end called REAR (end of list)
& Deletion of element only from FRONT end (beginning of list).

• The operation of insertion and deletion is known as Enqueue and


Dequeue respectively.

• Real life example: Queue of People for


ticket

Example:

Queue consists of five elements as


shown:

Index → 0 1 2 3 4

front=0 rear =4

Implementation of Queue using


List

1. Creating a Queue using list 2.


Inserting an element in a Queue 3.
Checking for Empty Queue 4.
Deleting Elements from a Queue
5. Traversal/Displaying a Queue

Applications of Queue:
 Printer Spooling

 CPU Scheduling

 Mail Service

 Keyboard Buffering

 Elevator

1 11 15 3 6

1
1. Program to insert, delete and display the numbers using
list -
Implementing List as a
Queue
2
2. Program to add, remove and display the book details using
list -
Implementing List as a Queue
.
3
3. Write functions to add, remove and display the book details using
list -
Implementing List as a
Queue def Insert(bk):
book_id=int(input("Enter Book Number:"))
bname=input("enter book name:")
b=[book_id,bname] [Link](b) #inserting
Element(node) to a Queue

def Delete(bk):
if bk==[]:
print("Underflow!Queue is empty") else:
print("Deleted element is :",[Link](0)) #Deleting node from a
queue def Display(bk):
if bk==[]:
print("Underflow!Queue is empty") else:
print("\nBook Details") print("------------------") print("Book
Id\tBook Name") for i in range(0, len(bk)):
#Traversal/Displaying a Queue
book_id,bname=bk[i] print(
book_id,"\t",bname) import sys
books=[] while True:
print("\nQUEUE OPERATIONS")
print("[Link]")
print("[Link]")
print("[Link]") print("[Link]")
ch=int(input("Enter the choice:"))
if ch==1:
Insert(books) elif
ch==2:
Delete(books) elif
ch==3:
Display(books) elif
ch==4: [Link]()

4
4. Write a program to perform insert and delete operations on a Queue
containing
Member’s details given in the following definition of each
node:
Member No. integer Member Name string def Enqueue(Qu): #
Code to insert member details using Queue
mem_id=int(input("Enter Member Number:"))
mname=input("Enter Member name:")
[Link]([mem_id,mname]) if len(Qu)==1:
front=rear=0 #first element inserted
else:
front=0 rear=len(Qu)-1
print("Front:",front," Rear:",rear)

def Dequeue(Qu): # Code to delete a member using


Queue
if Qu==[]:
print("Underflow!Queue is empty") else:
print("Deleted element is :",[Link](0)) if
len(Qu)==0:
front=rear=None # after the last elemented deleted
else:
front=0;rear=len(Qu)-1
print("Front:",front," Rear:",rear)

def Display(Qu):
if Qu==[]:
print("Underflow!Queue is empty")
else:
front=0 rear=len(Qu)-1
print("\nMember Details ")
print("-----------------------")
print("Member Id\tMember Name")
for i in range(front,rear+1):
mem_id,mname=Qu[i] print(
mem_id,"\t\t",mname,)

5
#__main__
import sys
member=[]
while True:
print("\nQUEUEOPERATIONS\[Link]\[Link]\[Link]
y
queue\[Link]")
ch=int(input("Enter the choice:")) if ch==1:
Enqueue(member) #calling the function push()
elif ch==2:
Dequeue(member) elif
ch==3:
Display(member) elif
ch==4:
[Link]() [Note: For Practical Record – follow
the above program ]
Sample Questions using
Queue(Theory)
1. Write a function in Python, INSERTQ(Arr) and DELETEQ(Arr) for performing
insertion and deletion operations in a Queue. Arr is the list of numbers
used for implementing queue and data is the value to be inserted.

def INSERTQ(Arr):
data=int(input("enter data to be inserted: "))
[Link](data) def DELETEQ(Arr):
if (Arr==[]):
print( "Queue empty") else:
print ("Deleted element is: ",Arr[0])
del (Arr[0])

6
2. Write a function in Python to perform insert and delete operations on a
Queue containing Member’s details as given in the following definition of
item node:
Member No integer Member Name String Age integer The size of the Queue
is 10(ie it can insert only 10 members). Also write the display function to
display all the members having age greater than 60.

def Insert(M):
if len(M)>=10:
print("Overflow!!!Queue is full") else:
mem_id=int(input("Enter Member Number:"))
mname=input("Enter Member name:")
age=int(input("Enter Age:"))
[Link]([mem_id,mname,age])

def Delete(M):
if M==[]:
print("Underflow!Queue is empty") else:
print("Deleted element is
:",[Link](0))

def Display(M):
if M==[]:
print("Underflow!Queue is empty") else:
print("\nMember Details (Age>60)")
print("-----------------------------------")
print("Member Id\tMember Name\tAge")
for i in range(0, len(M)):
mem_id,mname,age=M[i]
if age>60:
print( mem_id,"\t",mname,"\t",age)

You might also like