PHP算法学习(7) 双向链表 实现栈

时间:2023-03-08 17:11:26

2019年2月25日17:24:34

final class BasicNode {

    public $index;
public $data;
public $next = null;
public $pre = null; public function __construct($index, $data) {
$this->index = $index;
$this->data = $data;
} }
<?php

/*
* 双向链表
*/ final class DoublyLinkedList { //从链表尾部压入一个节点,节点自动维护,不需要要像main方法那样自己维护
public function add(BasicNode $head, BasicNode $Node) {
$current = $head; //让$current指向$head;
//顺序联表根据index排序
while ($current->next != null) {
//head元素为空,从第一个有数据元素开始检测
if ($current->next->index > $Node->index) {//如果有相同的index就不能插入
break;
//$current没有 next元素
} elseif ($current->next->index == $Node->index) {
throw new \Exception('index重复');
}
$current = $current->next;
}
// p($current);
//没有元素,和尾部插入元素
//中间插入情况
if ($current->next != null) {
$Node->next = $current->next;
}
$Node->pre = $current;
if ($current->next != null) {
$current->next->pre = $Node;
}
$current->next = $Node;
} //从链表尾压出一个节点
public function delete(BasicNode $head, $index) {
$current = $head; //让$current指向$head;
$has = false;
while ($current->next != null) {
//提前查找链表尾部是否为空,为空就是尾部,吧当前节点的next复制问NULL,就是尾部元素干掉
if ($current->next->index == $index) {
$has = true;
break;
}
$current = $current->next;
}
if (!$has) {
throw new \Exception('index没有找到');
}
if ($current->next != null) {
$current->next->pre = $current;
}
$current->next = $current->next->next;
} //修改数据
public function update(BasicNode $head, $index, $data) {
$current = $head; //让$current指向$head;
$has = false;
while ($current->next != null) {
if ($current->next->index == $index) {
$has = true;
break;
}
$current = $current->next;
}
if (!$has) {
throw new \Exception('index没有找到');
}
$current->next->data = $data;
} }

测试代码

$head = new BasicNode(null, []);
$a = new BasicNode(1, ['a']);
$b = new BasicNode(5, ['b']);
$c = new BasicNode(8, ['c']);
$d = new BasicNode(99, ['d']);
$e = new BasicNode(66, ['e']); //if ($head->next->index > 1) {
// pp('大于');
//} else {
// pp('小于');
//} $DoublyLinkedList = new DoublyLinkedList();
$DoublyLinkedList->add($head, $b);
//pp($head);
$DoublyLinkedList->add($head, $a);
//pp($head);
$DoublyLinkedList->add($head, $d);
$DoublyLinkedList->add($head, $e); $DoublyLinkedList->add($head, $c); //$DoublyLinkedList->update($head, 5, ['2312321']);
$DoublyLinkedList->delete($head, 99);
pp($head);

最麻烦的是新增的时候去维护互相连接的中间节点