Skip to content

Instantly share code, notes, and snippets.

@TechZi
Created June 25, 2010 06:17
Show Gist options
  • Save TechZi/452503 to your computer and use it in GitHub Desktop.
Save TechZi/452503 to your computer and use it in GitHub Desktop.
<?php
class Link {
public $value;
public $next;
public function __construct($value) {
$this->value = $value;
}
public function displayLink() {
echo $this->value.' ';
}
}
class FirstLastList {
public $first;
public $last;
public function __construct() {
$this->first = null;
$this->last = null;
}
public function isEmpty() {
return empty($this->first);
}
public function insertLast($value) {
$link = new Link($value);
if ($this->isEmpty()) {
$this->first = $link;
} else {
$this->last->next = $link;
}
$this->last = $link;
}
public function deleteFirst() {
if ($this->isEmpty()) {
return null;
}
$first = $this->first;
if ($this->first->next == null) {
$this->first = null;
$this->last = null;
return $first;
}
$this->first = $this->first->next;
return $first;
}
public function displayList() {
$current = $this->first;
while ($current != null) {
$current->displayLink();
$current = $current->next;
}
}
}
class LinkQueue {
private $linkList = null;
public function __construct() {
$this->linkList = new FirstLastList();
}
public function insert($value) {
$this->linkList->insertLast($value);
}
public function remove() {
$this->linkList->deleteFirst();
}
public function display() {
$this->linkList->displayList();
}
}
$queue = new LinkQueue();
$queue->insert(10);
$queue->insert(20);
$queue->insert(30);
$queue->insert(40);
$queue->display();
echo "/n";
$queue->remove();
$queue->display();
echo "/n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment