forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Priority_Queue.py
65 lines (51 loc) · 1.77 KB
/
Priority_Queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
queue = []
def insert(data): # To insert values into the Queue
queue.append(data)
def delete(): # TO remove value from the Priority Queue
try: # Here highest value will be deleted first
max = 0
for i in range(len(queue)):
if queue[i] > queue[max]:
max = i
item = queue[max]
del queue[max]
return item
except IndexError: # Instead of giving error for empty queue
print("Queue is empty!!") # Displays that 'Queue is empty'
while 1:
print("\t1. Insert value in queue\n\t2. Delete value in queue\n\t3. Print Queue\n\t4. Exit")
opt = int(input("Enter your option:"))
if opt == 1:
value = int(input("Enter the value:"))
insert(value)
elif opt == 2:
del_value = delete()
print("\n" + str(del_value) + " is deleted")
elif opt == 3:
print(queue)
elif opt == 4:
break
else:
print("\tInvalid option!\n\tTry Again!!")
# The program is made of user choice
'''
Example:
1. Insert value in queue
2. Delete value in queue
3. Print Queue
4. Exit
Enter your option: 1 # Here you have to give value to be inserted in the queue
Enter the value: 12
1. Insert value in queue
2. Delete value in queue
3. Print Queue
4. Exit
Enter your option:2 # To delete the value of highest priority(here highest value)
14 is deleted
Enter your option:3 # display the queue
[12, 1, 7]
Enter your option:4 #to exit
If you enter any other value other than (1,2,3,4), You will be shown:
Invalid option!
Try Again!!
'''