zoukankan      html  css  js  c++  java
  • 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);
    ?>
  • 相关阅读:
    css3
    如何去渲染数据?
    ajax
    Java多线程-线程安全
    java多线程-基础
    Git-团队开放中的代码同步与提交
    IDEA 调试Spring-boot 应用
    微服务-各种pom的配置和注解
    微服务-服务与注册中心
    微服务
  • 原文地址:https://www.cnblogs.com/leedaily/p/8250263.html
Copyright © 2011-2022 走看看