-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 19 priority queue as linked list.py
75 lines (62 loc) · 1.79 KB
/
Day 19 priority queue as linked list.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
66
67
68
69
70
71
72
73
74
75
class Node:
def __init__(self,data=None):
self.data=data
self.next=Node
class PriorityQueue:
def __init__(self):
self.head=None
self.tail=None
def enqueue(self,element):
element=Node(element)
if self.head is None:
element.next=self.head
self.head=self.tail=element
elif self.head.data < element.data:
element.next=self.head
self.tail=self.head
self.head=element
elif self.tail.data > element.data:
element.next=None
self.tail.next=element
self.tail=element
else:
p=self.head
temp=self.head.next
while temp is not None:
if temp.data<element.data:
break
p=p.next
temp=temp.next
element.next=temp
p.next=element
def dequeue(self,p="h"):
if self.head is None:
print("Empty")
elif p=="h":
temp=self.head
self.head=self.head.next
print("dequeue element is:",temp.data)
else:
temp=self.head
while temp.next is not None:
temp=temp.next
temp.next=None
print("dequeue element is:",temp.data)
self.tail=temp
def printpq(self):
temp=self.head
while temp is not None:
print(temp.data,end="->")
temp=temp.next
print("\n")
pq=PriorityQueue()
for i in range(5,9):
pq.enqueue(i*5)
pq.printpq()
pq.dequeue("l")
pq.printpq()
pq.dequeue("h")
pq.printpq()
for i in range(10,14):
pq.enqueue(i*10)
pq.printpq()