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
Implementation of Linked List in PHP
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.