Implementation of Linked List in PHP
Last Updated :
14 Feb, 2024
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();
?>
OutputLinked 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
?>
Similar Reads
Circular Linked List Implementation in Java A Circular Linked List is a variation of the traditional linked list data structure. In the traditional linked list, the last node points to the null and it can indicating the end of the list. However, in the circular linked list, the last node points back to the first node and forms the circle or l
7 min read
Applications of linked list data structure A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image: Linked-ListApplications of linked list in computer science:Implementation of stacks and queuesImplementa
5 min read
C Program to Implement Singly Linked List A linked list is a linear data structure used to store elements of the same data type but not in contiguous memory locations. It is a collection of nodes where each node contains a data field and a next pointer indicating the address of the next node. So only the current node in the list knows where
9 min read
C Program to Implement Circular Linked List A linked list is a dynamic data structure where elements are not stored in contiguous memory locations but linked using pointers. In a circular linked list, the last node points back to the first node instead of pointing to NULL, forming a circle.In this article, we will learn how to implement the c
8 min read
Circular Linked List in C++ A circular linked list is a linear data structure similar to a singly linked list where each node consists of two data members data and a pointer next, that stores the address of the next node in the sequence but in a circular linked list, the last node of the list points to the first node instead o
10 min read
Linked List of Linked List Linked list of linkeÂd list, also known as nested linkeÂd lists, is a type of Linked List where each main node stores another full linked list. This structure beats old, plain linked lists. It gives way more flexibility to store and manage compleÂx data. In this article we will learn about the bas
9 min read