0% found this document useful (0 votes)
6 views5 pages

Priority Queue

The document defines a PriorityQueue class with various methods for managing a queue based on priority, including enqueue, dequeue, and search functionalities. It also includes methods for updating priorities and checking the queue's order. The implementation utilizes a linked list structure with a Node class to hold priority and data values.

Uploaded by

yuzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Priority Queue

The document defines a PriorityQueue class with various methods for managing a queue based on priority, including enqueue, dequeue, and search functionalities. It also includes methods for updating priorities and checking the queue's order. The implementation utilizes a linked list structure with a Node class to hold priority and data values.

Uploaded by

yuzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

class Node:

def __init__(self, priority, data):


self.__priority = priority
self.__data = data
self.__next = None

@property
def priority(self):
return self.__priority

@priority.setter
def priority(self, priority):
self.__priority = priority

@property
def data(self):
return self.__data

@data.setter
def data(self, data):
self.__data = data

@property
def next(self):
return self.__next

@next.setter
def next(self, value):
self.__next = value

class PriorityQueue:
def __init__(self):
self.__head = None

def enqueue(self, priority, data):


node = Node(priority, data)

if self.__head is None or self.__head.priority > priority:


node.next = self.__head
self.__head = node

else:
current_node = self.__head
while current_node.next and current_node.next.priority <= priority:
current_node = current_node.next
node.next = current_node.next
current_node.next = node

def dequeue(self):
if self.__head is None:
print("Queue is empty.")
return None

removed_node = self.__head
self.__head = self.__head.next
return removed_node.priority, removed_node.data

def dequeue_rear(self):
if self.__head is None:
print("Queue is empty.")
return None

if self.__head.next is None:
removed_node = self.__head
self.__head = self.__head.next
return removed_node.priority, removed_node.data

current_node = self.__head
prev_node = None # To keep track of the node before the last one

while current_node.next:
prev_node = current_node
current_node = current_node.next

prev_node.next = None # Remove the last node


return current_node.priority, current_node.data

def dequeue_specific(self, priority):


if self.__head is None:
print("Queue is empty.")
return None

if self.__head.priority == priority:
removed_node = self.__head
self.__head = self.__head.next
return removed_node.priority, removed_node.data

current_node = self.__head
prev_node = None # To keep track of the node before the target one

while current_node and current_node.priority != priority:


prev_node = current_node
current_node = current_node.next

if current_node is None:
print(f"Element with priority {priority} not found.")
return None

prev_node.next = current_node.next # Skip the node with the target


priority
return current_node.priority, current_node.data

def update_priority(self, old_priority, new_priority):


current_node = self.__head
prev_node = None # To keep track of the previous node

while current_node:
if current_node.priority == old_priority:
current_node.priority = new_priority
self._reorder_queue()
return

prev_node = current_node
current_node = current_node.next

print(f"Element with priority {old_priority} not found.")


def _reorder_queue(self):
# Sort the queue again after a priority change.
if self.__head is None:
return

sorted_queue = None
current_node = self.__head

while current_node:
next_node = current_node.next
current_node.next = None

if sorted_queue is None or sorted_queue.priority >


current_node.priority:
current_node.next = sorted_queue
sorted_queue = current_node

else:
temp = sorted_queue

while temp.next and temp.next.priority <= current_node.priority:


temp = temp.next
current_node.next = temp.next
temp.next = current_node

current_node = next_node

self.__head = sorted_queue

def search(self, priority):


current_node = self.__head

while current_node:

if current_node.priority == priority:
return current_node.priority, current_node.data

current_node = current_node.next

print(f"Element with priority {priority} not found.")


return None

def front(self):
if self.__head is None:
print("Queue is empty.")
return None

return self.__head.priority, self.__head.data

def rear(self):
if self.__head is None:
print("Queue is empty.")
return None

current_node = self.__head

while current_node.next:
current_node = current_node.next
return current_node.priority, current_node.data

def is_empty(self):
return self.__head is None

def size(self):
current_node = self.__head
count = 0

while current_node:
count += 1
current_node = current_node.next

return count

def display(self):
current_node = self.__head

while current_node:
print(f"Priority: {current_node.priority}, Data: {current_node.data}")
current_node = current_node.next

def is_priority(self):
if self.__head is None:
return True # An empty queue is trivially ordered

current_node = self.__head

while current_node and current_node.next:

if current_node.priority > current_node.next.priority:


return False # If any node has lower priority than the next one

current_node = current_node.next

return True

# Create a priority queue object


queue = PriorityQueue()

# Test if the queue is empty


print("Is the queue empty?", queue.is_empty()) # Expect True

# Enqueue elements with different priorities


queue.enqueue(3, "Task A")
queue.enqueue(1, "Task B")
queue.enqueue(2, "Task C")
queue.enqueue(5, "Task D")
queue.enqueue(4, "Task E")

# Display the queue after enqueuing elements


print("\nQueue after enqueueing elements:")
queue.display()

# Test size and front/rear


print("\nSize of queue:", queue.size())
print("Front:", queue.front())
print("Rear:", queue.rear())

queue.display()

# Test dequeueing from the front


print("\nDequeue from front:", queue.dequeue())

# Test dequeueing from the rear


print("Dequeue from rear:", queue.dequeue_rear())

# Test dequeueing a specific priority


print("Dequeue specific priority 2:", queue.dequeue_specific(2))

queue.display()

# Test updating priority


print("\nUpdating priority of Task E from 4 to 6.")
queue.update_priority(4, 6)
queue.display()

# Test if the queue maintains priority order


print("\nIs the queue maintaining priority order? (Expect True):",
queue.is_priority())

# Search for a task with specific priority


print("Search for priority 6:", queue.search(6))

# Display final state of the queue


print("\nFinal queue state:")
queue.display()

You might also like