Python Implement a priority queue ADT using an unsorted doub
[Python] Implement a priority queue ADT using an unsorted doubly linked list.
Solution
class Node(object):
#declaring node
def __init__(self, data, prev, next):
self.data = data
self.prev = prev
self.next = next
class DoubleList(object):
#initializing head and tail to none
head = None
tail = None
def append(self, data):
new_node = Node(data, None, None)
if self.head is None:
self.head = self.tail = new_node
else:
new_node.prev = self.tail
new_node.next = None
self.tail.next = new_node
self.tail = new_node
d.sort()
def remove(self, node_value):
current_node = self.head
while current_node is not None:
if current_node.data == node_value:
# if it\'s not the first element
if current_node.prev is not None:
current_node.prev.next = current_node.next
current_node.next.prev = current_node.prev
else:
# otherwise we have no prev (it\'s None), head is the next one, and prev becomes None
self.head = current_node.next
current_node.next.prev = None
current_node = current_node.next
def show(self):
print \"\ List data:\"
#initialization of head of list to current_node
current_node = self.head
while current_node is not None:
print current_node.data,
#Moving current node to next node
current_node = current_node.next
def sort(self):
current_node = self.head
if self.head!=self.tail:
while current_node is not None:
j=current_node.next;
while j is not None:
if j.data>current_node.data:
temp=current_node.data
current_node.data=j.data
j.data=temp
print current_node.data
current_node = current_node.next
j=j.next
d = DoubleList()
d.append(5)
d.append(6)
d.append(50)
d.append(30)
d.show()
d.remove(50)
d.remove(5)
d.show()
OUTPUT:
List data:
5 6 30 50
List data:
6 30
![[Python] Implement a priority queue ADT using an unsorted doubly linked list.Solutionclass Node(object): #declaring node def __init__(self, data, prev, next): s [Python] Implement a priority queue ADT using an unsorted doubly linked list.Solutionclass Node(object): #declaring node def __init__(self, data, prev, next): s](/WebImages/9/python-implement-a-priority-queue-adt-using-an-unsorted-doub-999824-1761514913-0.webp)
![[Python] Implement a priority queue ADT using an unsorted doubly linked list.Solutionclass Node(object): #declaring node def __init__(self, data, prev, next): s [Python] Implement a priority queue ADT using an unsorted doubly linked list.Solutionclass Node(object): #declaring node def __init__(self, data, prev, next): s](/WebImages/9/python-implement-a-priority-queue-adt-using-an-unsorted-doub-999824-1761514913-1.webp)