博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP双链表
阅读量:7093 次
发布时间:2019-06-28

本文共 2560 字,大约阅读时间需要 8 分钟。

hot3.png

<?php

class Node{

private $_data;

private $_next = null;

    private $_pre = null;

function __construct($data){

$this->_data = $data;

}

function getData(){

   return $this->_data;

}

function getNext(){

   return $this->_next;

}

function setData($data){

   $this->_data = $data;

}

function setNext(Node $next){

   $this->_next = $next;

}

function getPre(){

   return $this->_pre;

}

function setPre(Node $pre){

   $this->_pre = $pre;

}

}

class LinkList{

    private $_head = null;

private $_tail = null;

private $_size = 0;

function __construct($data){

$this->addNode($data);

}

function addNode($data,$flag=0){

  if($this->_size > 0){

  $node = new Node($data);

      if($flag==0){

  $node->setNext($this->_head);

  $this->_head->setPre($node);

  $this->_head = $node;

  }else{

               $this->_tail->setNext($node);

  $node->setPre($this->_tail);

  $this->_tail = $node;

  }

  }else{

           $this->_tail = $this->_head = new Node($data);

  }

  ++$this->_size;

}

function findNodeByPos($pos,&$node)

{

if($pos == 0 || $pos == -$this->_size)

return $this->_head;

$k = abs($pos);

if($k > $this->_size-1)

return null;

$node = ($pos>0?$this->_head->getNext():$this->_tail);

for($i=1;$i<$k;$i++){

if($pos>0)

   $node = $node->getNext();

else

                $node = $node->getPre();

}

}

function insertNode($data,$pos = 0){

    if($pos == 0)

$this->addNode($data);

else if($pos >= $this->_size)

             $this->addNode($data,1);

else {

$this->findNodeByPos($pos-1,$node);

$newNode = new Node($data);

$newNode->setNext($node->getNext());

$newNode->setPre($node);

$node->getNext()->setPre($newNode);

             $node->setNext($newNode);

            

++$this->_size;

}

}

function delNode($data){

        $node = $this->_head;

if($node->getData() == $data){

            $this->_head = $note->getNext();

$this->_head->setPre(null);

--$this->_size;

return true;

}

for($i=1;$i<$this->_size;$i++){

if($node->getNext()->getData() == $data){

$node->getNext()->getNext()->setPre($note);

   $node->setNext($node->getNext()->getNext());

break;

}

$node = $node->getNext();

}

if($i < $this->_size){

--$this->_size;

if($i == $this->_size-1){

$this->_tail = $node;

}

return true;

}

return false;

}

    function iterate($flag=0){

if($flag == 0){

$node = $this->_head;

for($i=0;$i<$this->_size;$i++){

echo 'data:',$node->getData(),'<br/>';

$node = $node->getNext();

}

}else{

$node = $this->_tail;

for($i=0;$i<$this->_size;$i++){

echo 'data:',$node->getData(),'<br/>';

$node = $node->getPre();

}

}

}

function getSize(){

   return $this->_size;

}

}

$link = new LinkList('one');

$link->addNode('two');

$link->addNode('three');

$link->addNode('zero',1);

$link->insertNode('four',2);

$link->iterate(1);

转载于:https://my.oschina.net/u/196016/blog/263052

你可能感兴趣的文章
不知不觉我自己习惯了晚睡
查看>>
我的友情链接
查看>>
让电影尖叫:京东众筹试水跳板时代
查看>>
53 网络虚拟化技术进阶
查看>>
Linux(CentOS)最小化(mini)安装VMware Tools
查看>>
php中$_GET传递数组的实现
查看>>
Android源代码编译——下载
查看>>
if usage
查看>>
passive-interface / silent-interface
查看>>
Linux 基础环境配置
查看>>
业界动态
查看>>
磁盘df看还有剩余空间,但是创建文件时报错,提示磁盘已经满问题解决
查看>>
用艾宾浩斯曲线记忆周期来背单词是否有理论依据?
查看>>
分布式搜索elasticsearch单机与服务器环境搭建
查看>>
[转]云计算之hadoop、hive、hue、oozie、sqoop、hbase、zookeeper环境搭建及配置文件
查看>>
80.禁用消休眠功能
查看>>
堡垒(fortress)
查看>>
用UIImagePickerViewController自定义相机界面
查看>>
关于在系统中插入视频聊天的功能问题
查看>>
Leetcode#114Flatten Binary Tree to Linked List
查看>>