PHP 数据结构

删除循环单链表的偶数节点需要遍历链表,将偶数节点一一删除。它需要创建两个节点 - oddNode 和 EvenNode。如果列表包含多个节点,则将 oddNode 设为列表的第一个奇数节点,将 EvenNode 设为列表的第一个偶数节点。将当前奇数节点存储到临时节点并删除偶数节点。将两个节点移动到下一组奇偶节点。重复该过程,直到任何节点到达头部。最后,将最后一个节点与头部链接起来。

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

public function deleteEvenNodes() {
  if($this->head != null && $this->head->next != $this->head) {
  
    //1.如果列表有多个元素
    // 创建 EvenNode 节点 - 指向 head
    // oddNode 节点 - 指向头的下一个
    // 临时节点 - 存储最后一个奇数节点
    $oddNode = $this->head;
    $evenNode = $this->head->next; 
    $temp = new Node();

    while(true) {
      
      //2。删除偶数节点并更新偶数节点和
      // oddNode 到下一组奇偶节点
      // 将临时节点更新为最新的 oddNode 节点
      //继续该过程直到任何节点
      //到达头部
      $temp = $oddNode;
      $oddNode->next = $evenNode->next;
      $evenNode = null;
      $oddNode = $oddNode->next;
      $evenNode = $oddNode->next;
      if($oddNode == $this->head || $evenNode == $this->head)
        break;
    }
    
    //3。如果 oddNode 到达 head,则进行下一个
    // temp 作为头,否则生成 oddNode 的下一个
    //作为头
    if($oddNode == $this->head)
      $temp->next = $this->head;
    else
      $oddNode->next = $this->head;
  }
} 

下面是一个完整的程序,它使用上面讨论的删除循环单链表偶数节点的概念。

<?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;
    }    
  }

  //删除列表的偶数节点
  public function deleteEvenNodes() {
    if($this->head != null && $this->head->next != $this->head) {
      $oddNode = $this->head;
      $evenNode = $this->head->next; 
      $temp = new Node();

      while(true) {
        $temp = $oddNode;
        $oddNode->next = $evenNode->next;
        $evenNode = null;
        $oddNode = $oddNode->next;
        $evenNode = $oddNode->next;
        if($oddNode == $this->head || $evenNode == $this->head)
          break;
      }
      if($oddNode == $this->head)
        $temp->next = $this->head;
      else
        $oddNode->next = $this->head;
    }
  } 

  //显示列表内容
  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(40);
$MyList->push_back(50);

//显示列表内容。
$MyList->PrintList();

//删除列表的偶数节点
$MyList->deleteEvenNodes();

echo "After deleting even nodes.\n";
//显示列表内容。
$MyList->PrintList();
?>

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

The list contains: 10 20 30 40 50 
After deleting even nodes.
The list contains: 10 30 50