php implementation of a single linked list of instance code

  • 2020-05-30 19:43:40
  • OfStack


<?php
// The linked list node  
class node { 
    public $id; // node id 
    public $name; // The name of the node  
    public $next; // Under the 1 node  
    
    public function __construct($id, $name) { 
        $this->id = $id; 
        $this->name = $name; 
        $this->next = null; 
    } 
}
// Singly linked lists  
class singelLinkList { 
    private $header; // Chain header node  
    
    // A constructor  
    public function __construct($id = null, $name = null) { 
        $this->header = new node ( $id, $name, null ); 
    } 

    // Gets the length of the list  
    public function getLinkLength() { 
        $i = 0; 
        $current = $this->header; 
        while ( $current->next != null ) { 
            $i ++; 
            $current = $current->next; 
        } 
        return $i; 
    } 

    // Add node data  
    public function addLink($node) { 
        $current = $this->header; 
        while ( $current->next != null ) { 
            if ($current->next->id > $node->id) { 
                break; 
            } 
            $current = $current->next; 
        } 
        $node->next = $current->next; 
        $current->next = $node; 
    } 

    // Delete the list node  
    public function delLink($id) { 
        $current = $this->header; 
        $flag = false; 
        while ( $current->next != null ) { 
            if ($current->next->id == $id) { 
                $flag = true; 
                break; 
            } 
            $current = $current->next; 
        } 
        if ($flag) { 
            $current->next = $current->next->next; 
        } else { 
            echo " Not found id=" . $id . " The node! <br>"; 
        } 
    } 

    // Access list  
    public function getLinkList() { 
        $current = $this->header; 
        if ($current->next == null) { 
            echo (" The list is empty !"); 
            return; 
        } 
        while ( $current->next != null ) { 
            echo 'id:' . $current->next->id . '   name:' . $current->next->name . "<br>"; 
            if ($current->next->next == null) { 
                break; 
            } 
            $current = $current->next; 
        } 
    } 

    // Get the node name  
    public function getLinkNameById($id) { 
        $current = $this->header; 
        if ($current->next == null) { 
            echo " The list is empty !"; 
            return; 
        } 
        while ( $current->next != null ) { 
            if ($current->id == $id) { 
                break; 
            } 
            $current = $current->next; 
        } 
        return $current->name; 
    } 

    // Update node name  
    public function updateLink($id, $name) { 
        $current = $this->header; 
        if ($current->next == null) { 
            echo " The list is empty !"; 
            return; 
        } 
        while ( $current->next != null ) { 
            if ($current->id == $id) { 
                break; 
            } 
            $current = $current->next; 
        } 
        return $current->name = $name; 
    } 
}
$lists = new singelLinkList (); 
$lists->addLink ( new node ( 5, 'eeeeee' ) ); 
$lists->addLink ( new node ( 1, 'aaaaaa' ) ); 
$lists->addLink ( new node ( 6, 'ffffff' ) ); 
$lists->addLink ( new node ( 4, 'dddddd' ) ); 
$lists->addLink ( new node ( 3, 'cccccc' ) ); 
$lists->addLink ( new node ( 2, 'bbbbbb' ) ); 
$lists->getLinkList (); 
echo "<br>----------- Remove nodes --------------<br>"; 
$lists->delLink ( 5 ); 
$lists->getLinkList ();
echo "<br>----------- Update node name --------------<br>"; 
$lists->updateLink ( 3, "222222" ); 
$lists->getLinkList ();
echo "<br>----------- Get the node name --------------<br>"; 
echo $lists->getLinkNameById ( 5 );
echo "<br>----------- Gets the length of the list --------------<br>"; 
echo $lists->getLinkLength (); 
?>


Related articles: