Reputation: 1951
I've written this class to implement linked list:
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;
}
}
but when I try to push some value,
$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 />";
}
the script hangs out at the second push and gives max_execution_time
error.
It works fine if I change the two lines 30 and 38 of the calss as shown above , as a normal LinkedList. (by removing the last node link to the first node).
So what's the problem and how to solve it?
UPDATE: by changing push()
function to this , it works fine as a linear linked list:
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++;
}
Upvotes: 3
Views: 1874
Reputation: 95121
You can use
$ll = new CircularLinkedList();
$ll->push(5);
$ll->push(6);
$ll->push(7);
$ll->push(8);
$ll->push(9);
$ll->push(10);
echo "<pre>";
Usage
echo count($ll); // returns 6
$ll->find(7);
echo $ll->getCurrent(); // returns 7
$ll->reset(); // Reset from current position to beginning
while ( $ll->isValid() ) {
echo $ll->getCurrent() . "<br />";
$ll->getNext();
}
Output
5
6
7
8
9
10
Class Node
class Node {
public $data;
public $link;
function __construct($data, $next = NULL) {
$this->data = $data;
$this->link = $next;
}
function __toString() {
return "$this->data";
}
}
Class CircularLinkedList
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;
}
}
Upvotes: 1
Reputation: 637
For linear linked list - change the following:
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++;
}
}
This structure yields:
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
)
For circular - change to this:
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++;
}
}
This structure yields:
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
)
Upvotes: 1