What is a one-way linked list
A linked list is a structure that stores data in a linked way. It does not need continuous storage space. The data in the linked list is represented by nodes. Each node is composed of elements (storage data) and pointers (pointing to subsequent nodes).
One way linked list (also called single linked list) is the simplest form of linked list. Each node contains only one element and one pointer.
It has a header, and all nodes except the last one have their successors.
Its storage structure is shown in the figure below
code implementation
Defining nodes
class Node { public $data; /** * @var null | Node */ public $next; public function __construct($data) { $this->data = $data; $this->next = null; } }
Single chain table implementation
/** * Class SingleLinkList * Example of single link implementation: simple filling, inserting, deleting, querying, length, traversing */ class SingleLinkList { /** * Chain header node. The header node must exist, * @var Node */ public $header; private $size = 0; /** * Constructor. A sentinel node is added by default. The element of this node is empty * SingleLinkList constructor. */ public function __construct() { $this->header = new Node(null); } /** * Add a node at the end of the list * @param Node $node * @return int */ public function addNode(Node $node) { $current = $this->header; while ($current->next != null) { $current = $current->next; } $current->next = $node; return ++$this->size; } /** * Insert node at specified location * @param int $index Node location, counting from 1 * @param Node $node * @return int * @throws Exception */ public function insertNodeByIndex($index, Node $node) { if ($index < 1 || $index > ($this->size + 1)) { throw new Exception(sprintf('The position you want to insert exceeds the length of the list %d', $this->size)); } $current = $this->header; $tempIndex = 1; do { if ($index == $tempIndex++) { $node->next = $current->next; $current->next = $node; break; } } while ($current->next != null && ($current = $current->next)); return ++$this->size; } /** * Delete node * @param int $index Node location, counting from 1 * @return int * @throws Exception */ public function deleteNodeByIndex($index) { if ($index < 1 || $index > ($this->size + 1)) { throw new Exception('The node you deleted does not exist'); } $current = $this->header; $tempIndex = 1; do { if ($index == $tempIndex++) { $current->next = $current->next->next; break; } } while ($current->next != null && ($current = $current->next)); return --$this->size; } /** * Query node * @param int $index Node location, counting from 1 * @return Node|null * @throws Exception */ public function searchNodeByIndex($index) { if ($index < 1 || $index > ($this->size + 1)) { throw new Exception('The node you queried does not exist'); } $current = $this->header; $tempIndex = 1; do { if ($index == $tempIndex++) { return $current->next; } } while ($current->next != null && ($current = $current->next)); } /** * Get node length * @return int */ public function getLength() { return $this->size; } /** * Traversal list */ public function showNode() { $current = $this->header; $index = 1; while ($current->next != null) { $current = $current->next; echo 'index --- ' . $index++ . ' --- '; echo var_export($current->data); echo PHP_EOL; } } }
Example
$link = new SingleLinkList(); $link->addNode(new Node(1)); $link->addNode(new Node(2)); $link->insertNodeByIndex(3, new Node(3)); $link->addNode(new Node(4)); $link->addNode(new Node(5)); echo $link->getLength(), PHP_EOL; $link->showNode(); echo '-----------', PHP_EOL; var_dump($link->searchNodeByIndex(3)); echo '-----------', PHP_EOL; $link->deleteNodeByIndex(3); $link->showNode();