May 2017
Intermediate to advanced
340 pages
8h 16m
English
As lists are different from arrays, it is not easier to get elements from their positions directly. In order to get an element in the Nth position, we have to iterate to that position and get the element. Here is the code sample for this method:
public function getNthNode(int $n = 0) { $count = 1; if ($this->_firstNode !== NULL) { $currentNode = $this->_firstNode; while ($currentNode !== NULL) { if ($count === $n) { return $currentNode; } $count++; $currentNode = $currentNode->next; } } }
We have now written all the required operations for our LinkedList class. Now, let's run the program with different operations. If we run the following program, we will mostly cover all the operations we have ...
Read now
Unlock full access