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(); ?> |
Linked List: 1 2 3 Reversed Linked List: 3 2 1 Linked List after deleting 2: 3 1
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.
Contact Us