Open In App

Implementation of Linked List in PHP

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A linked list is a fundamental data structure in computer science, commonly used for efficient data management and manipulation. Unlike arrays, linked lists store elements in nodes that are not placed contiguously in memory, making them ideal for situations where the size of the data structure can change dynamically. Each node in a linked list contains the data and a reference (or link) to the next node in the sequence. This article will explore how to implement a basic linked list in PHP, including node creation, linked list creation, traversal, reverse traversal, and deletion of an element from the linked list.

Node Creation

The first step in implementing a linked list is to define the structure of a node. Each node should contain the data it holds and a reference to the next node.

Syntax to Create Node

class ListNode {
public $data = NULL;
public $next = NULL;
public function __construct($data = NULL) {
$this->data = $data;
}
}

Linked List Operations

With the node structure defined, the next step is to create a class to manage the linked list. This class will include methods for adding nodes, traversing the list, reversing the list, and deleting nodes.

1. Creating a Linked List and Adding Nodes

$list = new LinkedList();
$list->insert(1);
$list->insert(2);
$list->insert(3);

2. Traversing the List

To display the elements in the list:

echo "Linked List: \n";
$list->traverse();

3. Reversing the List

To reverse the list and then display the elements:

$list->reverse();
echo "Reversed Linked List: \n";
$list->traverse();

4. Deleting a Node from the List

To delete a node with a specific value:

$list->delete(2);
echo "Linked List after deleting 2: \n";
$list->traverse();

Example: This example creates the linked list and perform the above operations.

PHP




<?php
  
class ListNode {
    public $data = NULL;
    public $next = NULL;
  
    public function __construct($data = NULL) {
        $this->data = $data;
    }
}
  
class LinkedList {
    private $firstNode = NULL;
  
    // Add a new node to the end of the list
    public function insert($data) {
        $newNode = new ListNode($data);
        if ($this->firstNode === NULL) {
            $this->firstNode = $newNode;
        } else {
            $currentNode = $this->firstNode;
            while ($currentNode->next !== NULL) {
                $currentNode = $currentNode->next;
            }
            $currentNode->next = $newNode;
        }
    }
  
    // Traverse the list
    public function traverse() {
        $currentNode = $this->firstNode;
        while ($currentNode !== NULL) {
            echo $currentNode->data . "\n";
            $currentNode = $currentNode->next;
        }
    }
  
    // Reverse the list
    public function reverse() {
        $prev = NULL;
        $current = $this->firstNode;
        while ($current !== NULL) {
            $next = $current->next;
            $current->next = $prev;
            $prev = $current;
            $current = $next;
        }
        $this->firstNode = $prev;
    }
  
    // Delete a node from the list
    public function delete($data) {
        $current = $this->firstNode;
        $prev = NULL;
        while ($current !== NULL) {
            if ($current->data === $data) {
                if ($prev === NULL) {
                    $this->firstNode = $current->next;
                } else {
                    $prev->next = $current->next;
                }
                return;
            }
            $prev = $current;
            $current = $current->next;
        }
    }
}
  
$list = new LinkedList();
$list->insert(1);
$list->insert(2);
$list->insert(3);
  
echo "Linked List: \n";
$list->traverse();
  
$list->reverse();
echo "Reversed Linked List: \n";
$list->traverse();
  
$list->delete(2);
echo "Linked List after deleting 2: \n";
$list->traverse();
  
?>


Output

Linked List: 
1
2
3
Reversed Linked List: 
3
2
1
Linked List after deleting 2: 
3
1

Doubly Linked List

A doubly linked list extends the basic linked list by including references to both the next and the previous nodes.

PHP




<?php
  
class DoublyNode {
    public $data;
    public $next;
    public $prev;
  
    public function __construct($data) {
        $this->data = $data;
        $this->next = null;
        $this->prev = null;
    }
}
  
class DoublyLinkedList {
    private $head;
  
    public function __construct() {
        $this->head = null;
    }
  
    public function insert($data) {
        $newNode = new DoublyNode($data);
        $newNode->next = $this->head;
        if ($this->head !== null) {
            $this->head->prev = $newNode;
        }
        $this->head = $newNode;
    }
  
    public function append($data) {
        $newNode = new DoublyNode($data);
        $newNode->next = null;
  
        if ($this->head === null) {
            $newNode->prev = null;
            $this->head = $newNode;
            return;
        }
  
        $last = $this->head;
        while ($last->next !== null) {
            $last = $last->next;
        }
  
        $last->next = $newNode;
        $newNode->prev = $last;
    }
  
    public function delete($data) {
        $current = $this->head;
  
        while ($current !== null && $current->data !== $data) {
            $current = $current->next;
        }
  
        if ($current === null) {
            return;
        }
  
        if ($current->prev !== null) {
            $current->prev->next = $current->next;
        } else {
            $this->head = $current->next;
        }
  
        if ($current->next !== null) {
            $current->next->prev = $current->prev;
        }
    }
  
    public function display() {
        $current = $this->head;
        while ($current !== null) {
            echo $current->data . " ";
            $current = $current->next;
        }
    }
}
  
$doublyList = new DoublyLinkedList();
$doublyList->insert(3);
$doublyList->insert(7);
$doublyList->insert(10);
$doublyList->append(5);
  
$doublyList->display(); // Output: 10 7 3 5
$doublyList->delete(7);
$doublyList->display(); // Output: 10 3 5
  
?>


Output

10 7 3 5 10 3 5 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads