[go: up one dir, main page]

0% found this document useful (0 votes)
8 views7 pages

Chapter 4 Queue

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)
8 views7 pages

Chapter 4 Queue

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
You are on page 1/ 7

Chapter 4- QUEUE 2025

SUDHEER KN HOD VANI PU COLLEGE Page 1


Chapter 4- QUEUE 2025

Implementation Of Queue Using Python

• Let’s create a queue named myQueue. We can create it by assigning an empty list.

myQueue = list()

• A function (enqueue) to insert a new element at the end of queue. The function has two parameters - name of
the queue and element which is to be inserted in the queue.

def enqueue(myQueue, element):


myQueue.append(element)

A function (isEmpty) to check, if the queue has an element or not?


def isEmpty(myQueue):
if len(myQueue)==0:
return True
else:
return False
• A function (dequeue) to delete an element from the front of the queue. It has one parameter – name of the
queue and returns the deleted element. The function first checks if the queue is empty or not, for successful
deletion.
def dequeue(myQueue):
if not (isEmpty(myQueue)):
return myQueue.pop(0)
else :
print(“Queue is empty”)

SUDHEER KN HOD VANI PU COLLEGE Page 2


Chapter 4- QUEUE 2025
• A function (size) to get the number of elements in the queue. We can use the len() function of Python’s list to
find the number of elements in the queue.
def size(myQueue):
return len(myQueue)
A function (peek) to simply read, but not to delete, the element at the front end of the queue. For this, we can read the
element at index[0] of the queue.
def peek(myQueue):
if isEmpty(myQueue):
print('Queue is empty')
return None
else:
return myQueue[0]

Program 4-1
myQueue = list()
# each person to be assigned a code as P1, P2, P3,...
element = input("enter person’s code to enter in queue :”)
enqueue(myQueue,element)
element = input("enter person’s code for insertion in queue :")
enqueue(myQueue,element)
print("person removed from queue is:", dequeue(myQueue))
print(“Number of people in the queue is :”,size(myQueue))
element = input("enter person’s code to enter in queue :")
enqueue(myQueue,element)
element = input("enter person’s code to enter in queue :")
enqueue(myQueue,element)
element = input("enter person’s code to enter in queue :")
enqueue(myQueue,element)
print("Now we are going to remove remaining people from the
queue")
while not isEmpty(myQueue):
print("person removed from queue is ",
dequeue(myQueue))

Output
enter person’s code to enter in queue :P1
enter person’s code to enter in queue :P2
person removed from the queue is :p1
number of people in the queue is :1
enter person’s code to enter in queue :P3
enter person’s code to enter in queue :P4
enter person’s code to enter in queue :P5
Now we are going to remove remaining people from the queue
person removed from the queue is :p2
person removed from the queue is :p3
person removed from the queue is :p4

SUDHEER KN HOD VANI PU COLLEGE Page 3


Chapter 4- QUEUE 2025
person removed from the queue is :p5
Queue is empty

Introduction To Deque
Deque (pronounced as “deck”) is an arrangement in which addition and removal of element(s) can happen
from any end, i.e. head/front or tail/rear. This data structure does not apply any restriction on the side from
which addition/removal of elements should happen, so it can be used to implement stack or queue in the
program. It is also known as Double ended queue, because it permits insertion, deletion operations from any
end.

SUDHEER KN HOD VANI PU COLLEGE Page 4


Chapter 4- QUEUE 2025

SUDHEER KN HOD VANI PU COLLEGE Page 5


Chapter 4- QUEUE 2025

SUDHEER KN HOD VANI PU COLLEGE Page 6


Chapter 4- QUEUE 2025

4. Additive |
Precedence of operator +, - 11. Logical AND && (in C/C++) (in
e.g., a + b, a - b Python)
12. Logical OR
1. Parentheses 5. Shift || (in C/C++) or or (in Python)
() <<, >> (in C/C++) 13. Conditional / Ternary operator
Highest precedence – evaluated first. e.g., a <<b ? : (in C/C++)
--- 6. Relational 14. Assignment
2. Unary operators <, <=, >, >= =, +=, -=, *=, /=, etc.
+, -, !, ~, ++, --, sizeof (in C/C++) 7. Equality 15. Comma
e.g., -a, !flag ==, != ,
--- 8. Bitwise AND Lowest precedence
3. Multiplicative & Example:
*, /, % 9. Bitwise XOR int result = 3 + 4 * 2; // Multiplication
e.g., a * b, a % b ^ happens before addition
--- 10. Bitwise OR // result = 3 + (4 * 2) = 11

SUDHEER KN HOD VANI PU COLLEGE Page 7

You might also like