• Implementation of Linked List in PHP

    A linked list is a linear data structure, which contains node structure and each node contains two elements. A data part that stores the value at that node and next part that stores the link to the next node as shown in the below image:


    Linked List Node

    The first node also known as HEAD is usually used to traverse through the linked list. The last node (next part of the last node) points to NULL. The list can be visualized as a chain of nodes, where every node points to the next node.


    Linked List

    Implementation of Singly Linked List


    Representation:


    In PHP, singly linked list can be represented as a class and a Node as a separate class. The LinkedList class contains a reference of Node class type.


    //node structure
    class Node {
      public $data;
      public $next;
    }
    
    class LinkedList {
      public $head;
    
      //constructor to create an empty LinkedList
      public function __construct(){
        $this->head = null;
      }
    };
    

    Read more →