zoukankan      html  css  js  c++  java
  • PHP算法学习(7) 双向链表 实现栈

    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);

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

  • 相关阅读:
    hdu 3037 Saving Beans fzu 2020 组合 hit 2813 Garden visiting hrbeu 组合数 fzu 1564 Combination
    PKU 2429 GCD & LCM Inverse
    Discrete Logging hunnu10590 pku2417 fzu 1352 hit 1928 zoj 1898
    HDUBased Game Theory
    Perfect Pth Powers zoj 2124 pku1730 hunnu10585
    More Divisors zoj 2562
    服务器的安全配置技巧总结
    sql server中datetime字段只取年月日如20060421,默认值如何设置?getdate()得到的是包含时分秒的时间
    获取当前打印机的名称、驱动程序、打印端口信息
    如何使用Delphi设计强大的服务器程序
  • 原文地址:https://www.cnblogs.com/zx-admin/p/10432095.html
Copyright © 2011-2022 走看看