PHP如何实现单链表翻转,并问这是长尾词操作吗?
- 内容介绍
- 文章标签
- 相关推荐
本文共计726个文字,预计阅读时间需要3分钟。
本例展示了如何用PHP实现单链表的翻转操作。
单链表:当序列中仅包含指向其后续节点的指针时,称该链表为单链表。
以下是一个单链表的定义和翻转操作的示例代码:
phpclass Node { public $data; public $next;
public function __construct($data) { $this->data=$data; $this->next=null; }}
class LinkedList { public $head;
public function __construct() { $this->head=null; }
// 向链表末尾添加节点 public function append($data) { $newNode=new Node($data); if ($this->head==null) { $this->head=$newNode; return; } $current=$this->head; while ($current->next !=null) { $current=$current->next; } $current->next=$newNode; }
// 打印链表 public function display() { $current=$this->head; while ($current !=null) { echo $current->data . ; $current=$current->next; } echo \n; }
// 翻转链表 public function reverse() { $prev=null; $current=$this->head; $next=null; while ($current !=null) { $next=$current->next; $current->next=$prev; $prev=$current; $current=$next; } $this->head=$prev; }}
// 创建链表并添加元素$linkedlist=new LinkedList();$linkedlist->append(1);$linkedlist->append(2);$linkedlist->append(3);$linkedlist->append(4);
echo 原始链表:;$linkedlist->display();
// 翻转链表$linkedlist->reverse();
echo 翻转后的链表:;$linkedlist->display();
本文实例讲述了PHP实现单链表翻转操作。分享给大家供大家参考,具体如下:
当一个序列中只含有指向它的后继结点的链接时,就称该链表为单链表。
这里给出了一个单链表的定义及翻转操作方法:
<?php /** * @file reverseLink.php * @author showersun * @date 2016/03/01 10:33:25 **/ class Node{ private $value; private $next; public function __construct($value=null){ $this->value = $value; } public function getValue(){ return $this->value; } public function setValue($value){ $this->value = $value; } public function getNext(){ return $this->next; } public function setNext($next){ $this->next = $next; } } //遍历,将当前节点的下一个节点缓存后更改当前节点指针 function reverse($head){ if($head == null){ return $head; } $pre = $head;//注意:对象的赋值 $cur = $head->getNext(); $next = null; while($cur != null){ $next = $cur->getNext(); $cur->setNext($pre); $pre = $cur; $cur = $next; } //将原链表的头节点的下一个节点置为null,再将反转后的头节点赋给head $head->setNext(null); $head = $pre; return $head; } //递归,在反转当前节点之前先反转后续节点 function reverse2($head){ if (null == $head || null == $head->getNext()) { return $head; } $reversedHead = reverse2($head->getNext()); $head->getNext()->setNext($head); $head->setNext(null); return $reversedHead; } function test(){ $head = new Node(0); $tmp = null; $cur = null; // 构造一个长度为10的链表,保存头节点对象head for($i=1;$i<10;$i++){ $tmp = new Node($i); if($i == 1){ $head->setNext($tmp); }else{ $cur->setNext($tmp); } $cur = $tmp; } //print_r($head);exit; $tmpHead = $head; while($tmpHead != null){ echo $tmpHead->getValue().' '; $tmpHead = $tmpHead->getNext(); } echo "\n"; //$head = reverse($head); $head = reverse2($head); while($head != null){ echo $head->getValue().' '; $head = $head->getNext(); } } test(); ?>
运行结果:
0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数据结构与算法教程》、《php程序设计算法总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP常用遍历算法与技巧总结》及《PHP数学运算技巧总结》
希望本文所述对大家PHP程序设计有所帮助。
本文共计726个文字,预计阅读时间需要3分钟。
本例展示了如何用PHP实现单链表的翻转操作。
单链表:当序列中仅包含指向其后续节点的指针时,称该链表为单链表。
以下是一个单链表的定义和翻转操作的示例代码:
phpclass Node { public $data; public $next;
public function __construct($data) { $this->data=$data; $this->next=null; }}
class LinkedList { public $head;
public function __construct() { $this->head=null; }
// 向链表末尾添加节点 public function append($data) { $newNode=new Node($data); if ($this->head==null) { $this->head=$newNode; return; } $current=$this->head; while ($current->next !=null) { $current=$current->next; } $current->next=$newNode; }
// 打印链表 public function display() { $current=$this->head; while ($current !=null) { echo $current->data . ; $current=$current->next; } echo \n; }
// 翻转链表 public function reverse() { $prev=null; $current=$this->head; $next=null; while ($current !=null) { $next=$current->next; $current->next=$prev; $prev=$current; $current=$next; } $this->head=$prev; }}
// 创建链表并添加元素$linkedlist=new LinkedList();$linkedlist->append(1);$linkedlist->append(2);$linkedlist->append(3);$linkedlist->append(4);
echo 原始链表:;$linkedlist->display();
// 翻转链表$linkedlist->reverse();
echo 翻转后的链表:;$linkedlist->display();
本文实例讲述了PHP实现单链表翻转操作。分享给大家供大家参考,具体如下:
当一个序列中只含有指向它的后继结点的链接时,就称该链表为单链表。
这里给出了一个单链表的定义及翻转操作方法:
<?php /** * @file reverseLink.php * @author showersun * @date 2016/03/01 10:33:25 **/ class Node{ private $value; private $next; public function __construct($value=null){ $this->value = $value; } public function getValue(){ return $this->value; } public function setValue($value){ $this->value = $value; } public function getNext(){ return $this->next; } public function setNext($next){ $this->next = $next; } } //遍历,将当前节点的下一个节点缓存后更改当前节点指针 function reverse($head){ if($head == null){ return $head; } $pre = $head;//注意:对象的赋值 $cur = $head->getNext(); $next = null; while($cur != null){ $next = $cur->getNext(); $cur->setNext($pre); $pre = $cur; $cur = $next; } //将原链表的头节点的下一个节点置为null,再将反转后的头节点赋给head $head->setNext(null); $head = $pre; return $head; } //递归,在反转当前节点之前先反转后续节点 function reverse2($head){ if (null == $head || null == $head->getNext()) { return $head; } $reversedHead = reverse2($head->getNext()); $head->getNext()->setNext($head); $head->setNext(null); return $reversedHead; } function test(){ $head = new Node(0); $tmp = null; $cur = null; // 构造一个长度为10的链表,保存头节点对象head for($i=1;$i<10;$i++){ $tmp = new Node($i); if($i == 1){ $head->setNext($tmp); }else{ $cur->setNext($tmp); } $cur = $tmp; } //print_r($head);exit; $tmpHead = $head; while($tmpHead != null){ echo $tmpHead->getValue().' '; $tmpHead = $tmpHead->getNext(); } echo "\n"; //$head = reverse($head); $head = reverse2($head); while($head != null){ echo $head->getValue().' '; $head = $head->getNext(); } } test(); ?>
运行结果:
0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数据结构与算法教程》、《php程序设计算法总结》、《php字符串(string)用法总结》、《PHP数组(Array)操作技巧大全》、《PHP常用遍历算法与技巧总结》及《PHP数学运算技巧总结》
希望本文所述对大家PHP程序设计有所帮助。

