Featured image of post Implementation of Linked List in PHP

Implementation of Linked List in PHP

Linked list is a common data structure

Starting to learn data structures


Today I changed the code font I use. Previously I used console which looked good, but today I found a more preferable style Source Code Pro.
Here are two screenshots - it’s quite pleasing!


Now let’s get to the main topic: linked list operations

Node Class

  • First we need a Node class to store data
<?php

namespace LinkedList;

class Node
{
    /**
     * @var $data integer
     */
    public $data;

    /**
     * Pointer to next node
     *
     * @var $next Node
     */
    public $next;

    public function __construct(int $data = null)
    {
        // Initialize data assignment, can also be assigned via $node->data = X;
        $this->data = $data;
    }
}

Linked List Manager Class (For node operations)

  • We’ll break down the manager class code into sections due to its length

Head Insertion

  • As the name suggests, insert at the head
  • If list is empty, initialize with current node
  • If not empty, make new node the head
public function insertHead(int $data) : bool
{
    $newNode = new Node($data);
    $newNode->next = $this->head;
    $this->head = $newNode;

    return true;
}

Insert at Position (index=0 is head, returns false for invalid positions)

public function insert(int $index = 0, int $data) : bool
{
    if (is_null($this->head) || $index === 0) {
        return $this->insertHead($data);
    }

    $currNode = $this->head;
    $startIndex = 1;
    
    for ($currIndex = $startIndex; ! is_null($currNode); ++ $currIndex) {
        if ($currIndex === $index) {
            $newNode = new Node($data);
            $newNode->next = $currNode->next;
            $currNode->next = $newNode;
            return true;
        }
        $currNode = $currNode->next;
    }

    return false;
}

Usage example:

<?php
// Autoload code omitted, see github
require __DIR__.'/../vendor/bootstrap.php';

// Instantiate a linked list manager
$manager = new \LinkedList\Manager();
// 8
$manager->insertHead(8);
// 5 8
$manager->insertHead(5);
// 1 5 8
$manager->insertHead(1);
// 1 2 5 8
$manager->insert(1, 2);
// false (insufficient nodes for position 5)
$manager->insert(5, 4);
// 1 2 5 8 9
$manager->insertEnd(9);

// Returns 3
$manager->find(8);

// Result: 1 2 8 9
$manager->delete(2);

Search Operation

  • Simply traverse the list to find value
public function find(int $data) : int
{
    $currNode = $this->head;
    for ($i = 0; ! is_null($currNode); ++ $i) {
        if ($currNode->data === $data) {
            return $i;
        }
        $currNode = $currNode->next;
    }
    return -1;
}

Delete Operation

public function delete(int $index) : bool
{
    if (is_null($this->head)) {
        return false;
    } elseif ($index === 0) {
        $this->head = $this->head->next;
    }

    $startIndex = 1;
    $currNode = $this->head;

    for ($i = $startIndex; ! is_null($currNode->next); ++ $i) {
        if ($index === $i) {
            $currNode->next = $currNode->next->next;
            break;
        }
        $currNode = $currNode->next;
    }
    return true;
}

End

  • The code has been hosted on github
  • Will continue learning data structures (doubly linked lists, trees, etc.) when time permits!