一、数组
class ArrayList
{
private $data;
private $capacity;
private $length;
public function __construct($capacity)
{
$capacity = intval($capacity);
if ($capacity <= 0) return null;
$this->data = array();
$this->capacity = $capacity;
$this->length = 0;
}
public function getLength()
{
return $this->length;
}
public function insert($index, $value)
{
$index = intval($index);
if ($index < 0) return false;
if ($index > $this->getLength()) return false;
if ($this->checkIfFull()) return false;
for ($i = $this->getLength(); $i > $index; --$i) {
$this->data[$i] = $this->data[$i - 1];
}
$this->data[$index] = $value;
++$this->length;
return true;
}
public function delete($index)
{
$index = intval($index);
if ($index < 0) return false;
if ($this->checkOutOfRange($index)) return false;
$value = $this->data[$index];
for ($i = $index; $i < $this->getLength() - 1; ++$i) {
$this->data[$i] = $this->data[$i + 1];
}
--$this->length;
return $value;
}
public function find($index)
{
$index = intval($index);
if ($index < 0) return [0, -1];
if ($this->checkOutOfRange($index)) return [0, -1];
return [1, $this->data[$index]];
}
public function printData()
{
$string = '';
for ($i = 0; $i < $this->getLength(); ++$i) {
$string .= " [{$this->data[$i]}] ";
}
echo "{$string}\n length={$this->getLength()}\n";
}
private function checkOutOfRange($index)
{
$index = intval($index);
return $index >= $this->length;
}
private function checkIfFull()
{
return $this->length == $this->capacity;
}
}
二、链表
class SingleLinkedListNode
{
public $data;
public $next;
public function __construct($data = null)
{
$this->data = $data;
$this->next = null;
}
}
class TestSingleLinkedList
{
private $header;
private $length;
public function __construct(SingleLinkedListNode $header = null)
{
$this->header = !is_null($header) ? $header : new SingleLinkedListNode();
$this->length = 0;
}
public function getLength()
{
return $this->length;
}
public function insert($data)
{
return $this->insertDataAfter($this->header, $data);
}
public function insertDataBefore(SingleLinkedListNode $node, $data)
{
if (is_null($node)) return false;
$new = new SingleLinkedListNode($data);
$preNode = $this->getPreNode($node);
$new->next = $preNode->next;
$preNode->next = $new;
++$this->length;
return true;
}
public function insertDataAfter(SingleLinkedListNode $node, $data)
{
if (is_null($node)) return false;
$new = new SingleLinkedListNode($data);
$new->next = $node->next;
$node->next = $new;
++$this->length;
return true;
}
public function insertNodeAfter(SingleLinkedListNode $node, SingleLinkedListNode $new)
{
if (is_null($node)) return false;
if (is_null($new)) return false;
$new->next = $node->next;
$node->next = $new;
++$this->length;
return true;
}
public function delete(SingleLinkedListNode $node)
{
if (is_null($node)) return false;
$preNode = $this->getPreNode($node);
$preNode->next = $preNode->next->next;
--$this->length;
return true;
}
/**
* 删除链表倒数第n个节点
* @param $last
* @return bool
*/
public function deleteLastNode($last)
{
$last = intval($last);
if ($last > $this->getLength()) return false;
$slow = $fast = $this->header;
$mark = 0;
while (!is_null($fast->next)) {
if ($mark >= $last) {
$slow = $slow->next;
}
$fast = $fast->next;
++$mark;
}
$slow->next = $slow->next->next;
--$this->length;
return true;
}
public function getNodeByIndex($index)
{
$index = intval($index);
if ($index < 0) return null;
if ($index >= $this->length) return null;
$currNode = $this->header;
$i = 0;
while (!is_null($currNode->next) && $index != $i++) {
$currNode = $currNode->next;
}
return $currNode->next;
}
public function printData()
{
$string = '';
$currNode = $this->header;
$len = $this->getLength();
while (!is_null($currNode->next) && $len--) {
$currNode = $currNode->next;
$string .= " [$currNode->data] ";
}
echo "{$string}\n length={$this->getLength()}\n";
}
public function getPreNode(SingleLinkedListNode $node)
{
if (is_null($node)) return null;
$currNode = $this->header;
while ($currNode->next !== $node && !is_null($currNode->next)) {
$currNode = $currNode->next;
}
return $currNode;
}
/**
* 单链表反转
*/
public function reverse()
{
if ($this->getLength() <= 1) return;
$preNode = null;
$remainNode = null;
$currNode = $this->header->next;
while (!is_null($currNode)) {
$remainNode = $currNode->next;
$currNode->next = $preNode;
$preNode = $currNode;
$currNode = $remainNode;
}
$this->header->next = $preNode;
}
}
三、栈
四、队列