php循环链表的实现
我编写这个类是为了实现链表:php循环链表的实现,php,linked-list,Php,Linked List,我编写这个类是为了实现链表: class Node{ public $data; public $link; function __construct($data, $next = NULL){ $this->data = $data; $this->link = $next; } } class CircularLinkedList{ private $first; private $cur
class Node{
public $data;
public $link;
function __construct($data, $next = NULL){
$this->data = $data;
$this->link = $next;
}
}
class CircularLinkedList{
private $first;
private $current;
private $count;
function __construct(){
$this->count = 0;
$this->first = null;
$this->current = null;
}
function isEmpty(){
return ($this->first == NULL);
}
function push($data){
//line 30
$p = new Node($data, $this->first);
if($this->isEmpty()){
$this->first = $p;
$this->current = $this->first;
}
else{
$q = $this->first;
//line 38
while($q->link != $this->first)
$q = $q->link;
$q->link = $p;
}
$this->count++;
}
function find($value){
$q = $this->first;
while($q->link != null){
if($q->data == $value)
$this->current = $q;
$q = $q->link;
}
return false;
}
function getNext(){
$result = $this->current->data;
$this->current = $this->current->link;
return $result;
}
}
function push($data){
$p = new Node($data);
if($this->isEmpty()){
$this->first = $p;
$this->current = $this->first;
}
else{
$q = $this->first;
while($q->link != null)
$q = $q->link;
$q->link = $p;
}
$this->count++;
}
但当我试图推动一些价值时
$ll = new CircularLinkedList();
$ll->push(5);
$ll->push(6);
$ll->push(7);
$ll->push(8);
$ll->push(9);
$ll->push(10);
//$ll->find(7);
for($j=0;$j<=30;$j++){
$result = $ll->getNext();
echo $result."<br />";
}
对于线性链表-更改以下内容:
function push($data){
if($this->isEmpty()){
$this->first = new Node($data);
$this->current = $this->first;
$this->count++;
}
else{
$this->current->link = new Node($data);
$this->current = $this->current->link;
$this->count++;
}
}
这种结构产生:
CircularLinkedList Object
(
[first:CircularLinkedList:private] => Node Object
(
[data] => 2
[link] => Node Object
(
[data] => 10
[link] => Node Object
(
[data] => 3
[link] => Node Object
(
[data] => 9
[link] =>
)
)
)
)
[current:CircularLinkedList:private] => Node Object
(
[data] => 9
[link] =>
)
[count:CircularLinkedList:private] => 4
)
CircularLinkedList Object
(
[first:CircularLinkedList:private] => Node Object
(
[data] => 2
[link] => Node Object
(
[data] => 10
[link] => Node Object
(
[data] => 3
[link] => Node Object
(
[data] => 9
[link] => Node Object
*RECURSION*
)
)
)
)
[current:CircularLinkedList:private] => Node Object
(
[data] => 9
[link] => Node Object
(
[data] => 2
[link] => Node Object
(
[data] => 10
[link] => Node Object
(
[data] => 3
[link] => Node Object
*RECURSION*
)
)
)
)
[count:CircularLinkedList:private] => 4
)
对于通告-更改为:
function push($data){
if($this->isEmpty()){
$this->first = new Node($data);
$this->current = $this->first;
$this->count++;
}
else{
$this->current->link = new Node($data, $this->first);
$this->current = $this->current->link;
$this->count++;
}
}
这种结构产生:
CircularLinkedList Object
(
[first:CircularLinkedList:private] => Node Object
(
[data] => 2
[link] => Node Object
(
[data] => 10
[link] => Node Object
(
[data] => 3
[link] => Node Object
(
[data] => 9
[link] =>
)
)
)
)
[current:CircularLinkedList:private] => Node Object
(
[data] => 9
[link] =>
)
[count:CircularLinkedList:private] => 4
)
CircularLinkedList Object
(
[first:CircularLinkedList:private] => Node Object
(
[data] => 2
[link] => Node Object
(
[data] => 10
[link] => Node Object
(
[data] => 3
[link] => Node Object
(
[data] => 9
[link] => Node Object
*RECURSION*
)
)
)
)
[current:CircularLinkedList:private] => Node Object
(
[data] => 9
[link] => Node Object
(
[data] => 2
[link] => Node Object
(
[data] => 10
[link] => Node Object
(
[data] => 3
[link] => Node Object
*RECURSION*
)
)
)
)
[count:CircularLinkedList:private] => 4
)
你可以用
$ll = new CircularLinkedList();
$ll->push(5);
$ll->push(6);
$ll->push(7);
$ll->push(8);
$ll->push(9);
$ll->push(10);
echo "<pre>";
类节点
class CircularLinkedList implements Countable {
private $data;
private $current;
private $count;
function __construct() {
$this->count = 0;
$this->data = null;
$this->current = null;
}
function isEmpty() {
return ($this->data == NULL);
}
function push($data) {
$p = new Node($data);
if ($this->isEmpty()) {
$this->data = $p;
$this->current = $this->data;
} else {
$this->current->link = $p ;
$this->current = $this->current->link;
}
$this->count ++;
}
function find($value) {
$q = $this->data;
while ( $q->link != null ) {
if ($q->data == $value)
$this->current = $q;
$q = $q->link;
}
return false;
}
function getCurrent() {
return $this->current;
}
function getNext() {
$this->current = $this->current->link;
}
function hasNext() {
return isset($this->current->link);
}
function isValid() {
return isset($this->current);
}
function reset() {
$this->current = $this->data;
}
function count() {
return $this->count;
}
}
类循环链接列表
我的假设是你的链接是不正确的。因此,第38行是一个无限循环。一些调试可能会证明这一点。我知道问题出在第38行。但逻辑似乎是正确的。问题是如何调试您是否签出了
SplDoublyLinkedList
和朋友?你可能不需要自己做这一切。可以扩展一个SPL类@Kris:它不支持循环链表和我需要的一些功能,扩展该类并进行修改可能会再次产生类似的问题!我接受了乔·布朗的答案,因为它更接近我的实现。但是你的好答案也是+1!我只是觉得($j=0;$jSorry,我忘了检查,这不是一个循环链接列表!它是线性的!Ali,第二个示例是循环的,因为最后一个节点链接指向第一个节点。因此结构告诉您它本身是递归的。嗨,如果我想在第一个位置插入一个新节点,您能告诉我push方法更改应该是什么吗
class CircularLinkedList implements Countable {
private $data;
private $current;
private $count;
function __construct() {
$this->count = 0;
$this->data = null;
$this->current = null;
}
function isEmpty() {
return ($this->data == NULL);
}
function push($data) {
$p = new Node($data);
if ($this->isEmpty()) {
$this->data = $p;
$this->current = $this->data;
} else {
$this->current->link = $p ;
$this->current = $this->current->link;
}
$this->count ++;
}
function find($value) {
$q = $this->data;
while ( $q->link != null ) {
if ($q->data == $value)
$this->current = $q;
$q = $q->link;
}
return false;
}
function getCurrent() {
return $this->current;
}
function getNext() {
$this->current = $this->current->link;
}
function hasNext() {
return isset($this->current->link);
}
function isValid() {
return isset($this->current);
}
function reset() {
$this->current = $this->data;
}
function count() {
return $this->count;
}
}