• php实现链表的基本操作


    <?php  
    
    class node{
        private $value;
        private $next;
        public function __construct($value=0,$next=null){
            $this->value=$value;
            $this->next=$next;
        }
        public function getValue(){
            return $this->value;
        }
        public function setValue($value){
            return $this->value=$value;
        }
        public function getNext(){
            return $this->next;
        }
        public function setNext($next){
            return $this->next=$next;
        }
    }
    function reverse($node){
        if (null == $node || null == $node->getNext()) {
            return $node;
        }
        $reversednode = reverse($node->getNext());
        $node->getNext()->setNext($node);
        $node->setNext(null);
        return $reversednode;
    }
    function insert($node,$value,$position){
        $tmp=$node;
        for($i=0;$i<$position;$i++){
            $tmp=$tmp->getNext();
        }
        $insertnode=new node($value);
        $insertnode->setNext($tmp->getNext());
        $tmp->setNext($insertnode);
    }
    function delete($node,$position){
        $tmp=$node;
        for($i=0;$i<$position;$i++){
            $tmp=$tmp->getNext();
        }
        $tmp->setNext($tmp->getNext()->getNext());
    }
    echo "<pre>";
    $node=new node();
    $tmp=$node;
    for($i=1;$i<10;$i++){
        $nextnode=new node($i);
        $tmp->setNext($nextnode);
        $tmp=$nextnode;
    }
    print_r($node);
    $node=reverse($node);
    insert($node,11,3);
    delete($node,3);
    print_r($node);
    ?>
  • 相关阅读:
    ffmpeg
    HDU 1031 Design T-Shirt
    HDU 1029 Ignatius and the Princess IV
    HDU 1022 Train Problem I
    HDU 1017 A Mathematical Curiosity
    HDU 1015 Safecracker
    HDU 1002 A + B Problem II
    HDU 1070 Milk
    高精度算法(一)
    codeblocks 使用心得
  • 原文地址:https://www.cnblogs.com/leedaily/p/8250263.html
Copyright © 2020-2023  润新知