Example of Definition and Inversion of Linked List in PHP

  • 2021-10-15 10:00:47
  • OfStack

This paper describes the definition and inversion function of PHP to realize linked list. Share it for your reference, as follows:

PHP defines linked lists and operations such as adding, removing, and traversing:


<?php
class Node
{
  private $Data;// Node data 
  private $Next;// Under 1 Node 
 
  public function setData($value){
    $this->Data=$value;
  }
 
  public function setNext($value){
     $this->Next=$value;
  }  
 
  public function getData(){
    return $this->Data;
  }
 
  public function getNext(){
    return $this->Next;
  }
 
  public function __construct($data,$next){
    $this->setData($data);
    $this->setNext($next);
  }
}
class LinkList
{
  private $header;// Head node 
  private $size;// Length 
  public function getSize()
 {
    $i=0;
    $node=$this->header;
    while($node->getNext()!=null)
    {  
  $i++;
      $node=$node->getNext();
    }
    return $i;
  }
 
  public function setHeader($value){
    $this->header=$value;
  }
 
  public function getHeader(){
    return $this->header;
  }
 
  public function __construct(){
    header("content-type:text/html; charset=utf-8");
    $this->setHeader(new Node(null,null));
  }
  /**
  *@author MzXy
  *@param $data-- To add the data of the node 
  * 
  */
  public function add($data)
  {
    $node=$this->header;
    while($node->getNext()!=null)
    {
      $node=$node->getNext();
    }
    $node->setNext(new Node($data,null));
  }
   /**
  *@author MzXy
  *@param $data-- To remove the data of the node 
  * 
  */
  public function removeAt($data)
  {
    $node=$this->header;
    while($node->getData()!=$data)
    {
      $node=$node->getNext();
    }
    $node->setNext($node->getNext());
    $node->setData($node->getNext()->getData());
  }
   /**
  *@author MzXy
  *@param  Traversal 
  * 
  */
  public function get()
  {
    $node=$this->header;
    if($node->getNext()==null){
      print(" The dataset is empty !");
      return;
    }
    while($node->getNext()!=null)
    {
      print('['.$node->getNext()->getData().'] -> ');
      if($node->getNext()->getNext()==null){break;}
      $node=$node->getNext();
    }
  }
   /**
  *@author MzXy
  *@param $data-- Data of the node to be accessed 
  * @param  This method is just a demonstration and has no practical significance 
  * 
  */
  public function getAt($data)
  {
    $node=$this->header->getNext();
  if($node->getNext()==null){
      print(" The dataset is empty !");
      return;
    }
    while($node->getData()!=$data)
    {
      if($node->getNext()==null){break;}
      $node=$node->getNext();
    }
    return $node->getData();    
  }
   /**
  *@author MzXy
  *@param $value-- The original data of the node to be updated  --$initial--- Updated data 
  * 
  */
  public function update($initial,$value)
  {
     $node=$this->header->getNext();
 if($node->getNext()==null){
     print(" The dataset is empty !");
      return;
    }
    while($node->getData()!=$data)
    {
      if($node->getNext()==null){break;}
      $node=$node->getNext();
    }
 $node->setData($initial);   
  }
}
$lists = new LinkList();
$lists -> add(1);
$lists -> add(2);
$lists -> get();
echo '<pre>';
print_r($lists);
echo '</pre>';
?>

Reverse linked list operation:

1. Commonly used methods: Alternate left and right, save the next node, and replace the next node of the node with the upper node. Implement replacement.

Code:


function ReverseList($pHead)
{
  // write code here
  if($pHead == null || $pHead->next == null){
    return $pHead;
  }
  $p = $pHead;
  $q = $pHead->next;
  $pHead->next = null;//$pHead  Become a tail pointer 
  while($q){
    $r = $q->next;
    $q->next = $p;
    $p = $q;
    $q = $r;
  }
  return $p;
}

2. Use recursive methods. 3 nodes, head node, first node and second node. Take all the nodes after the first node as the second node, and loop down in turn, because it is necessary to meet the requirements of $pHead != null || $pHead->next != null ; Therefore, there will be no endless traversal


function ReverseList($pHead)
{
  // write code here
  if($pHead == null || $pHead->next == null){
    return $pHead;
  }
  $res = ReverseList($pHead->next);
  $pHead->next->next = $pHead;
  $pHead->next = null;
  return $res;
}

For more readers interested in PHP related contents, please check the topics of this site: "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php String (string) Usage Summary", "PHP Array (Array) Operation Skills Complete Book", "PHP Common Traversal Algorithms and Skills Summary" and "PHP Mathematical Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: