PHP 数据结构

该方法删除循环单链表中指定键(值)的所有节点。例如 - 如果给定列表是 10->20->30->10->20 并且删除所有出现的 20,则列表将变为 10->30->10。

如果循环单链表的头不为空且 head 存储的值等于 key,则将 head 调整为 head next,删除前一个 head 并调整链接。继续执行相同的过程,直到新头变为空或不等于密钥。之后创建一个临时节点来遍历列表并删除那些值等于键的节点并相应地调整链接。

函数pop_all就是为此目的而创建的。这是一个两步过程

public function pop_all($key) {     
  $nodeToDelete = new Node();
  $temp = new Node();
  
  //1.如果头不为空并且值存储在
  //头等于键,继续调整
  // 头作为下一个头并删除前一个头
  //直到新头变为空或不等于键
  while($this->head != null && $this->head->data == $key) {

    if($this->head->next == $this->head) {
      $this->head = null;
    } else {
      $nodeToDelete = $this->head;
      $temp = $this->head;
      //遍历到最后一个元素
      //调整链接的列表
      while($temp->next != $this->head) {
        $temp = $temp->next;
      } 
      $this->head = $this->head->next;
      $temp->next = $this->head;   
      $nodeToDelete = null;
    }
  }

  //2。创建一个临时节点来遍历
  //列出并删除值等于的节点
  // 键入并相应调整链接
  $temp = $this->head;        
  if($temp != null) {
    while($temp->next != $this->head) {
      if($temp->next->data == $key) {
        $nodeToDelete = $temp->next;
        $temp->next = $temp->next->next;
        $nodeToDelete = null;
      } else {
        $temp = $temp->next;
      }
    }
  }
} 

下面是一个完整的程序,它使用上面讨论的概念来删除所有出现的指定键(如果存在)循环单链表。

<?php
//节点结构
class Node {
  public $data;
  public $next;
}

class LinkedList {
  public $head;

  public function __construct(){
    $this->head = null;
  }
  
  //在列表末尾添加新元素
  public function push_back($newElement) {
    $newNode = new Node();
    $newNode->data = $newElement;
    $newNode->next = null; 
    if($this->head == null) {
      $this->head = $newNode;
      $newNode->next = $this->head;
    } else {
      $temp = new Node();
      $temp = $this->head;
      while($temp->next !== $this->head) {
        $temp = $temp->next;
      }
      $temp->next = $newNode;
      $newNode->next = $this->head;
    }    
  }

  //通过key删除所有节点
  public function pop_all($key) {     
    $nodeToDelete = new Node();
    $temp = new Node();
    
    while($this->head != null && $this->head->data == $key) {

      if($this->head->next == $this->head) {
        $this->head = null;
      } else {
        $nodeToDelete = $this->head;
        $temp = $this->head;
        while($temp->next != $this->head) {
          $temp = $temp->next;
        } 
        $this->head = $this->head->next;
        $temp->next = $this->head;   
        $nodeToDelete = null;
      }
    }

    $temp = $this->head;        
    if($temp != null) {
      while($temp->next != $this->head) {
        if($temp->next->data == $key) {
          $nodeToDelete = $temp->next;
          $temp->next = $temp->next->next;
          $nodeToDelete = null;
        } else {
          $temp = $temp->next;
        }
      }
    }
  } 

  //显示列表内容
  public function PrintList() {
    $temp = new Node();
    $temp = $this->head;
    if($temp != null) {
      echo "The list contains: ";
      while(true) {
        echo $temp->data." ";
        $temp = $temp->next;
        if($temp == $this->head)
          break;        
      }
      echo "\n";
    } else {
      echo "The list is empty.\n";
    }
  }    
};

//测试代码
$MyList = new LinkedList();

//列表中添加五个元素。
$MyList->push_back(10);
$MyList->push_back(20);
$MyList->push_back(30);
$MyList->push_back(10);
$MyList->push_back(20);
$MyList->PrintList();

//删除所有出现的20
$MyList->pop_all(20);
$MyList->PrintList(); 
?>

上面的代码将给出以下输出:

The list contains: 10 20 30 10 20 
The list contains: 10 30 10