May 2017
Intermediate to advanced
340 pages
8h 16m
English
As we did with the stack implementation, we are going to use our linked list implementation in Chapter 3, Using Linked Lists, to implement the queue here. We can use the insert() method to ensure that we are always inserting at the end. We can use deleteFirst() for a dequeue operation and getNthNode() for a peek operation. Here is the sample implementation of a queue using a linked list:
class AgentQueue implements Queue { private $limit; private $queue; public function __construct(int $limit = 20) { $this->limit = $limit; $this->queue = new LinkedList(); } public function dequeue(): string { if ($this->isEmpty()) { throw new UnderflowException('Queue is empty'); } else { $lastItem = $this->peek(); ...Read now
Unlock full access