Linked List Data Structure: Creation and Traversal in DSA

Creation and Traversal

Introduction

A Linked List is a fundamental data structure where elements (nodes) are connected using pointers.

πŸ‘‰ Unlike arrays:

  • No fixed size
  • Dynamic memory allocation
  • Efficient insertions

Each node contains:

  • Data
  • Pointer to next node

πŸ”Ή Structure of a Node

[ Data | Next ]

πŸ‘‰ Example:

10 β†’ 20 β†’ 30 β†’ NULL

πŸ”Ή 1. Creation of Linked List in PHP

πŸ§‘β€πŸ’» Step 1: Create Node Class

<?php
class Node {
    public $data;
    public $next;    
    public function __construct($data) {
        $this->data = $data;
        $this->next = null;
    }
}
?>

πŸ§‘β€πŸ’» Step 2: Create Linked List Class

<?php
class LinkedList {
    public $head;   
    public function __construct() {
        $this->head = null;
    }    
// Create list by adding nodes at end
    public function insert($data) {
        $newNode = new Node($data);        
        if ($this->head == null) {
            $this->head = $newNode;
            return;
        }        
        $temp = $this->head;
        while ($temp->next != null) {
            $temp = $temp->next;
        }        
        $temp->next = $newNode;
    }
}
?>

πŸ§ͺ Example: Creating a Linked List

<?php
$list = new LinkedList();
$list->insert(10);
$list->insert(20);
$list->insert(30);
?>

πŸ‘‰ Created List:

10 β†’ 20 β†’ 30 β†’ NULL

πŸ”Ή 2. Traversal of Linked List

πŸ’‘ Concept

Traversal means visiting each node one by one.

πŸ‘‰ Start from head
πŸ‘‰ Move using next pointer


πŸ§‘β€πŸ’» Traversal Code

<?php
public function traverse() {
    $temp = $this->head;    
    while ($temp != null) {
        echo $temp->data . " β†’ ";
        $temp = $temp->next;
    }    
    echo "NULL";
}
?>

πŸ§ͺ Full Example

<?php
class Node {
    public $data;
    public $next;    public function __construct($data) {
        $this->data = $data;
        $this->next = null;
    }
}
class LinkedList {
    public $head;    public function __construct() {
        $this->head = null;
    }    
    public function insert($data) {
        $newNode = new Node($data);        
        if ($this->head == null) {
            $this->head = $newNode;
            return;
        }        $temp = $this->head;
        while ($temp->next != null) {
            $temp = $temp->next;
        }        $temp->next = $newNode;
    }    
    public function traverse() {
        $temp = $this->head;        
        while ($temp != null) {
            echo $temp->data . " β†’ ";
            $temp = $temp->next;
        }        echo "NULL";
    }
}// Usage
$list = new LinkedList();
$list->insert(10);
$list->insert(20);
$list->insert(30);$list->traverse();?>

⚑ Time Complexity

OperationComplexity
CreationO(n)
TraversalO(n)

🎯 Key Points

βœ… Linked List is dynamic
βœ… No index access (unlike arrays)
βœ… Traversal is required to access elements

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *