How to implement deletion and insertion of nodes in a double linked list in PHP

墨辰丷
Release: 2023-03-26 10:02:01
Original
1525 people have browsed it

This article mainly introduces the method of deleting and inserting nodes in a doubly linked list in PHP. It analyzes the definition of a doubly linked list in PHP and the related implementation techniques of node operations in the form of examples. Friends in need can refer to the following

for details As follows:

Overview:

Doubly linked list is also called doubly linked list. It is a type of linked list. Each of its data nodes contains There are two pointers, pointing to the direct successor and the direct predecessor. Therefore, starting from any node in the doubly linked list, you can easily access its predecessor nodes and successor nodes. Generally we construct a two-way circular linked list.

Implementation code:

<?php 
class node{
  public $prev;
  public $next;
  public $data;
  public function __construct($data,$prev=null,$next=null){
    $this->data=$data;
    $this->prev=$prev;
    $this->next=$next;
  }
}
class doubleLinkList{
  private $head;
  public function __construct()
  {
    $this->head=new node("head",null,null);
  }
  //插入节点
  public function insertLink($data){
    $p=new node($data,null,null);
    $q=$this->head->next;
    $r=$this->head;
    while($q){
      if($q->data>$data){
        $q->prev->next=$p;
        $p->prev=$q->prev;
        $p->next=$q;
        $q->prev=$p;
      }else{
      $r=$q;$q=$q->next;
      }
    }
    if($q==null){
      $r->next=$p;
      $p->prev=$r;
    }
  }
  //从头输出节点
  public function printFromFront(){
    $p=$this->head->next;
    $string="";
    while($p){
    $string.=$string?",":"";
    $string.=$p->data;
    $p=$p->next;
    }
    echo $string."<br>";
  }
  //从尾输出节点
  public function printFromEnd(){
    $p=$this->head->next;
    $r=$this->head;
    while($p){
    $r=$p;$p=$p->next;
    }
    $string="";
    while($r){
      $string.=$string?",":"";
      $string.=$r->data;
      $r=$r->prev;
    }
    echo $string."<br>";
  }
  public function delLink($data){
    $p=$this->head->next;
    if(!$p)
    return;
    while($p){
      if($p->data==$data)
      {
        $p->next->prev=$p->prev;
        $p->prev->next=$p->next;
        unset($p);
        return;
      }
      else{
        $p=$p->next;
      }
    }
    if($p==null)
    echo "没有值为{$data}的节点";
  }
}
$link=new doubleLinkList();
$link->insertLink(1);
$link->insertLink(2);
$link->insertLink(3);
$link->insertLink(4);
$link->insertLink(5);
$link->delLink(3);
$link->printFromFront();
$link->printFromEnd();
$link->delLink(6);
Copy after login

Running result:

1,2,4,5
5,4,2,1,head
没有值为6的节点
Copy after login

Related recommendations:

JS operation DOM insertion node

How to implement DOM insertion node in JS

jQuery insertion node insertAfter and mobile node insertBefore usage examples

The above is the detailed content of How to implement deletion and insertion of nodes in a double linked list in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template