• 技术文章 >php教程 >php手册

    在python的开发过程中如何进行单链表的实现(代码)

    坏嘻嘻坏嘻嘻2018-09-15 09:33:11原创8682
    本文介绍了如何实现单链表,希望大家耐心学习。

    //节点class node{
        //初始化变量,包括存储的内容 和 下一个数据的指针
        public $id = 0;    public $data = '';    public $next = null;    //构造函数,设置存储内容的数据
        public function __construct($id, $data)
        {
            $this->id = $id;    
                $this->data = $data;
        }
    }//单链表 
      class singelLinkList{
        private $header;
         //链表头节点   
        //添加节点数据   
        public function addLink($id = null, $name = null)
        {
            $node = new node ($id, $name);     
               $current = $this->header;    
                   if (!$current) {       
                        $this->header = $node;
                 } else {       
                      # 链表头插
                 $node->next = $current;   
                           $this->header = $node;  
                                     # 链表尾插
                /*# 循环,获取对象中最后一个元素
                while ($current->next != null) {
                    $current = $current->next;
                }
                # 最后一个元素的next指针指向$node
                $current->next = $node;*/
            }
        }    public function delLink($id = null, $name = null)
        {
            $current = $this->header;        # 循环
            while ($current->next != null) {        
                # 查找待删除元素 $delCurrent 的上一个元素
                if ($current->next->id == $id) {         
                       $delCurrent = $current->next;         
                              # 查找待删除元素 $delCurrent 的下一个元素
                    $current->next = $delCurrent->next;        
                            # 删除元素 $delCurrent
                    $delCurrent = null;      
                              break;
                }         
                   $current = $current->next;
            }
    
        }
    }$lists = new singelLinkList();
    $lists->addLink(1, 'aaaaaa');
    $lists->addLink(2, 'bbbbbb');
    $lists->addLink(3, 'cccccc');
    $lists->addLink(4, 'dddddd');
    $lists->addLink(5, 'eeeeee');
    $lists->delLink(4);echo '<pre>'; 
    print_r($lists);

    以上就是在python的开发过程中如何进行单链表的实现(代码)的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:单链表,PHP
    上一篇:如何理解Elasticsearch单机双节点集群部署 下一篇:没有了
    大前端线上培训班

    相关文章推荐

    • python单链表实现代码实例• php实现单链表的实例代码_php实例• php兑现单链表(静态链表)• 韩顺平_PHP软件工程师玩转算法公开课(第一季)02_单链表在内存中存在形式剖析_学习笔记_源代码图解_PPT文档整理

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网