Created
June 27, 2010 13:36
-
-
Save TechZi/454911 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Link { | |
public $value; | |
public $next; | |
public $previous; | |
public function __construct($value) { | |
$this->value = $value; | |
$this->next = null; | |
$this->previous = null; | |
} | |
public function displayLink() { | |
echo $this->value.' '; | |
} | |
} | |
class DoublyLinkedList { | |
private $first; | |
private $last; | |
public function __construct() { | |
$this->first = null; | |
$this->last = null; | |
} | |
public function isEmpty() { | |
return empty($this->first); | |
} | |
public function displayForward() { | |
if ($this->isEmpty()) { | |
echo 'the list is null'; | |
return null; | |
} | |
$current = $this->first; | |
while ($current != null) { | |
$current->displayLink(); | |
$current = $current->next; | |
} | |
} | |
public function displayBackward() { | |
if ($this->isEmpty()) { | |
echo 'the list is null'; | |
return null; | |
} | |
$current = $this->last; | |
while ($current !== null) { | |
$current->displayLink(); | |
$current = $current->previous; | |
} | |
} | |
public function insertFirst($value) { | |
$link = new Link($value); | |
if ($this->isEmpty()) { | |
$this->first = $this->last = $link; | |
return ; | |
} | |
$this->first->previous = $link; | |
$link->next = $this->first; | |
$this->first = $link; | |
} | |
public function insertLast($value) { | |
$link = new Link($value); | |
if ($this->isEmpty()) { | |
$this->first = $this->last = $link; | |
return ; | |
} | |
$link->previous = $this->last; | |
$this->last->next = $link; | |
$this->last = $link; | |
} | |
public function insertAfter($value, $insertValue) { | |
if ($this->isEmpty()) { | |
echo 'the list is null'; | |
return null; | |
} | |
$current = $this->first; | |
while ($current !== null && $current->value != $value) { | |
$current = $current->next; | |
} | |
if ($current == null) { | |
echo 'not found the '.$this->value.' link'; | |
return ; | |
} | |
$link = new Link($insertValue); | |
if ($current == $this->last) { | |
$this->last = $link; | |
} else { | |
$link->next = $current->next; | |
$current->next->previous = $link; | |
} | |
$link->previous = $current; | |
$current->next = $link; | |
} | |
public function deleteFirst() { | |
} | |
public function deleteLast() { | |
} | |
public function deleteKey() { | |
} | |
} | |
$list = new DoublyLinkedList(); | |
$list->insertFirst(11); | |
$list->insertFirst(22); | |
$list->displayForward(); | |
echo "\n"; | |
$list->displayBackward(); | |
echo "\n"; | |
$list = new DoublyLinkedList(); | |
$list->insertLast(44); | |
$list->insertLast(33); | |
$list->displayForward(); | |
echo "\n"; | |
$list->displayBackward(); | |
echo "\n"; | |
$list->insertAfter(44, 55); | |
$list->insertAfter(33, 66); | |
$list->displayForward(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment