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();
  
  //1.如果头不为空并且值存储在
  //头等于键,继续调整
  // 头作为下一个头并删除前一个头
  //直到新头变为空或不等于键
  while($this->head != null && $this->head->data == $key) {
    $nodeToDelete = $this->head;
    $this->head = $this->head->next;
    $nodeToDelete = null;
  } 

  //2。创建一个临时节点来遍历
  //列出并删除值等于的节点
  // 键入并相应调整链接
  $temp = $this->head;        
  if($temp != null) {
    while($temp->next != null) {
      if($temp->next->data == $key) {
        $nodeToDelete = $temp->next;
        $temp->next = $temp->next->next;
        $nodeToDelete = null;
      } else {
        $temp = $temp->next;
      }
    }
  }
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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

<?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;
    } else {
      $temp = new Node();
      $temp = $this->head;
      while($temp->next != null) {
        $temp = $temp->next;
      }
      $temp->next = $newNode;
    }    
  }

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

    $temp = $this->head;        
    if($temp != null) {
      while($temp->next != null) {
        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($temp != null) {
        echo $temp->data." ";
        $temp = $temp->next;
      }
      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();  
?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

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

The list contains: 10 20 30 10 20 
The list contains: 10 30 10 
  • 1
  • 2