Implementing priority queue using linked list

So far, we have seen a linked list using only one value, which is the node data. Now we need to pass another value that will be the priority. In order to achieve that, we need to change our ListNode implementation:

class ListNode {

public $data = NULL;
public $next = NULL;
public $priority = NULL;

public function __construct(string $data = NULL, int $priority =
NULL) {
$this->data = $data;
$this->priority = $priority;
}

}

Now we have both the data and the priority as part of the node. In order to allow this priority to be considered during the insert operation, we also need to change our insert() implementation inside the LinkedList class. Here is the modified implementation:

public function insert(string $data = NULL, int $priority = NULL) { 
$newNode = new ListNode($data, $priority);
$this->_totalNode++;

if ($this->_firstNode === NULL) {
$this->_firstNode = &$newNode;
} else {
$previous = $this->_firstNode;
$currentNode = $this->_firstNode;
while ($currentNode !== NULL) {
if ($currentNode->priority < $priority) {

if ($currentNode == $this->_firstNode) {
$previous = $this->_firstNode;
$this->_firstNode = $newNode;
$newNode->next = $previous;
return;
}
$newNode->next = $currentNode;
$previous->next = $newNode;
return;
}
$previous = $currentNode;
$currentNode = $currentNode->next;
}
}

return TRUE;
}

As we can see, our insert method has been changed to take both the data and the priority during the insert operation. As usual, the first process is to create a new node and increment the node count. There can be three possibilities for insertion, shown as follows:

  • The list is empty, so the new node is the first node.
  • The list is not empty, but the new item has the highest priority, so. So it becomes the first node and the previous first node follows it.
  • The list is not empty and the priority is not the highest, so it inserts the new node inside the list, or maybe at the end of the list.

In our implementation, we have considered all three possibilities,  three facts. As a result, we always have the highest priority item at the beginning of the list. Now let us run the AgentQueue implementation with this new code, as shown in the following example:

try { 
$agents = new AgentQueue(10);
$agents->enqueue("Fred", 1);
$agents->enqueue("John", 2);
$agents->enqueue("Keith", 3);
$agents->enqueue("Adiyan", 4);
$agents->enqueue("Mikhael", 2);
$agents->display();
echo $agents->dequeue()." ";
echo $agents->dequeue()." ";
} catch (Exception $e) {
echo $e->getMessage();
}

If there was no priority, then the queue should have been Fred, John, Keith, Adiyan, and Mikhael. But since we have added priorities to the list, the output is:

Adiyan
Keith
John
Mikhael
Fred

Since Adiyan has the highest priority, it is placed at the beginning of the queue, even though it was inserted in the fourth place in the queue.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset