লিঙ্ক লিস্ট কীভাবে পিএইচপি-তে ম্যানেজ হয়?

1 Like

আমার জানামতে PHP-তে লিঙ্কড লিস্ট ম্যানেজ করার জন্য আপনাকে ম্যানুয়ালি ডেটা স্ট্রাকচার তৈরি করতে হয়, কারণ PHP বিল্ট-ইনভাবে লিঙ্কড লিস্ট সাপোর্ট করে না। একটি লিঙ্কড লিস্ট তৈরি করতে নোড এবং পয়েন্টারের ধারণা ব্যবহার করা হয়। প্রতিটি নোডে দুটি অংশ থাকে:

  • ডেটা: নোডে থাকা ভ্যালু
  • নেক্সট: পরবর্তী নোডের রেফারেন্স
<?php
//implementation of Link List 

//1st Step create Node
class Node{
    public $data;
    public $next;

    public function __construct($data){
        $this->date = $data;
        $this->next = null;
    }
}

//2nd Step Create Linklist that will insert a new node and will connect each other, show valus and delete nodes
class LinkList{
    //Declare a head for pointing first node
    public $head;
    public function __construct(){
        $this->head = null;
    }

    //Insert a node and connect each other
    public function insert($data){
        //create new node
        $newNode = new Node($data);

        //connect this new node between next node
        if($this->head == null){
            $this->head = $newNode;
        }
        else{
            $current = $this->head;
            while($current->next != null){
                $current = $current->next;
            }
            $current->next = $newNode;
        }
        
    }
    //travarse the node values
    public function ShowData(){
        $current = $this->head;
        while($current != null){
            echo $current->date . " ";
            $current = $current->next;
        }
        echo "\n";
    }
}

$ListList = new LinkList;

$ListList->insert(10);
$ListList->insert(20);
$ListList->insert(30);
$ListList->insert(40);
$ListList->insert(50);
$ListList->ShowData();