Skip to content

Instantly share code, notes, and snippets.

@TechZi
Created June 22, 2010 09:31
Show Gist options
  • Save TechZi/448240 to your computer and use it in GitHub Desktop.
Save TechZi/448240 to your computer and use it in GitHub Desktop.
<?php
class Link {
public $key;
public $value;
public $next = null;
public function __construct($key, $value) {
$this->key = $key;
$this->value = $value;
}
public function displayLink() {
echo '{'.$this->key.' : '.$this->value.'} ';
}
}
class LinkList {
private $first = null;
public function isEmpty() {
return empty($this->first);
}
public function insertFirst($key, $value) {
$link = new Link($key, $value);
if (!$this->isEmpty()) {
$link->next = $this->first;
}
$this->first = $link;
}
public function deleteFirst() {
if ($this->isEmpty()) {
return null;
}
$this->first = $this->first->next;
return $this->first;
}
public function displayList() {
if ($this->isEmpty()) {
echo 'LinkList is null';
return ;
}
$current = $this->first;
while ($current !== null) {
$current->displayLink();
$current = $current->next;
}
}
}
$linkList = new LinkList();
$linkList->insertFirst(1, 1.99);
$linkList->insertFirst(2, 2.99);
$linkList->insertFirst(3, 3.99);
$linkList->insertFirst(4, 4.99);
$linkList->displayList();
echo "\n";
$linkList->deleteFirst();
$linkList->displayList();
echo "\n";
$linkList->deleteFirst();
$linkList->displayList();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment