File tree 1 file changed +40
-0
lines changed 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Author: OMKAR PATHAK
2
+
3
+ class PriorityQueue (object ):
4
+ def __init__ (self ):
5
+ self .queue = []
6
+
7
+ def __str__ (self ):
8
+ return ' ' .join ([str (i ) for i in self .queue ])
9
+
10
+ # for checking if the queue is empty
11
+ def isEmpty (self ):
12
+ return len (self .queue ) == []
13
+
14
+ # for inserting an element in the queue
15
+ def insert (self , data ):
16
+ self .queue .append (data )
17
+
18
+ # for popping an element based on Priority
19
+ def delete (self ):
20
+ try :
21
+ max = 0
22
+ for i in range (len (self .queue )):
23
+ if self .queue [i ] > self .queue [max ]:
24
+ max = i
25
+ item = self .queue [max ]
26
+ del self .queue [max ]
27
+ return item
28
+ except IndexError :
29
+ print ()
30
+ exit ()
31
+
32
+ if __name__ == '__main__' :
33
+ myQueue = PriorityQueue ()
34
+ myQueue .insert (12 )
35
+ myQueue .insert (1 )
36
+ myQueue .insert (14 )
37
+ myQueue .insert (7 )
38
+ print (myQueue ) # 12 1 14 7
39
+ while not myQueue .isEmpty ():
40
+ print (myQueue .delete (), end = ' ' ) # 14 12 7 1
You can’t perform that action at this time.
0 commit comments