예제 #1
0
파일: Node.php 프로젝트: tebru/tree
 /**
  * Add a child node
  *
  * If position if set on the node, add the child at that position.  This
  * will bump any node at that current position and all subsequent nodes
  * down in the list.
  *
  * This method also gives the user the ability to override the current
  * position set on the node.
  *
  * @param Node $node
  * @param int|null $position
  */
 public function addChild(Node $node, $position = null)
 {
     // if position was passed in, override current position
     if (null !== $position) {
         $node->setPosition($position);
     }
     // if the node doesn't have a position, just add it to the end
     $nodePosition = $node->getPosition();
     if (null === $nodePosition) {
         $this->children->push($node);
         return;
     }
     $this->children->add($nodePosition, $node);
 }
예제 #2
0
// offsetExists($index)
// 判断参索引是否存在,返回bool
$list->offsetExists(2);
// offsetGet($index)
// 返回参数索引的节点值
$list->offsetGet(2);
// offsetSet($index, $newValue)
// 设置参数索引的节点值, $index必须在链表的键范围中。
// 当一个节点用offsetUnset删掉时时,并不能用offsetSet重新给已删掉的节点设置值,只能对当前可见的节点的值进行修改
$list->offsetSet(3, 'value6');
// offsetUnset($index)
// 删除参数索引的节点
$list->offsetUnset(3);
// add($index, $value);
// 对指定的索引新增一个新值。 当一个节点用offsetUnset时,并没有直接删除,该节点还仍然会保存在内存中。用add可以重新给该节点设置值
$list->add(3, 'first');
// unshift($value)
// 在链表的开始节点插入value作为新的开始节点
$list->unshift('second');
// shift()
// 将链表的第一个移除
$list->shift();
// setIteratorMode(int $mode)
// 设置链表的模式。等价于下面的情况:
// IT_MODE_LIFO: 栈模式,先进后出;IT_MODE_FIFO:队列模式,先进先出
// IT_MODE_DELETE; IT_MODE_KEEP
$list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO);
// getIteratorMode()
// 获得链表的模式
$list->getIteratorMode();
echo "example: \n";
예제 #3
0
 /**
  * @param int $index
  * @param BufferInterface $value
  */
 public function add($index, $value)
 {
     $this->typeCheck($value);
     if (getenv('HHVM_VERSION') || version_compare(phpversion(), '5.5.0', 'lt')) {
         if ($index == $this->count()) {
             $this->push($value);
         } else {
             $size = count($this);
             $temp = [];
             for ($i = $size; $i > $index; $i--) {
                 array_unshift($temp, $this->pop());
             }
             $this->push($value);
             foreach ($temp as $value) {
                 $this->push($value);
             }
         }
     } else {
         parent::add($index, $value);
     }
 }
<?php

try {
    $dll = new SplDoublyLinkedList();
    var_dump($dll->add(12, 'Offset 12 should not exist'));
} catch (OutOfRangeException $e) {
    echo "Exception: " . $e->getMessage() . "\n";
}
<?php

try {
    $dll = new SplDoublyLinkedList();
    var_dump($dll->add(NULL, 2));
} catch (OutOfRangeException $e) {
    echo "Exception: " . $e->getMessage() . "\n";
}
예제 #6
0
<?php

$array1 = array(1, 2, 3, 4, 5, 6, 8, 9, 10);
function insertToPosition($position, $array, $insert)
{
    $arrayTemp = array_splice($array, $position, count($array), $insert);
    return array_merge($array, $arrayTemp);
}
print_r(insertToPosition(15, $array1, 1000));
echo "<hr />" . "<br />" . "Php functions using SplDoublyLinkedList";
$list = new SplDoublyLinkedList();
for ($i = 0; $i < 10; $i++) {
    $list->push($i);
}
$list->add(5, 55);
print_r($list);
예제 #7
0
<?php

$list = new SplDoublyLinkedList();
$list->push('a');
$list->add(1, 'e');
$list->push('b');
$list->push('c');
$list->push('d');
$list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO);
$list->rewind();
while ($list->valid()) {
    echo $list->current(), "\n";
    $list->next();
}
<?php

$dll = new SplDoublyLinkedList();
var_dump($dll->add());
<?php

$dll = new SplDoublyLinkedList();
// errors
try {
    $dll->add(2, 5);
} catch (OutOfRangeException $e) {
    echo "Exception: " . $e->getMessage() . "\n";
}
$dll->add(0, 6);
//	6
$dll->add(0, 3);
//	3 6
// Insert in the middle of the DLL
$dll->add(1, 4);
//	3 4 6
$dll->add(2, 5);
//	3 4 5 6
$dll->unshift(2);
//	2 3 5 4 6
// Insert at the beginning and end of the DLL
$dll->add(0, 1);
//	1 2 3 4 5 6
$dll->add(6, 7);
//	1 2 3 4 5 6 7
echo count($dll) . "\n";
echo $dll->pop() . "\n";
echo $dll->pop() . "\n";
echo $dll->pop() . "\n";
echo $dll->pop() . "\n";
echo $dll->pop() . "\n";
예제 #10
0
<?php

$dll = new SplDoublyLinkedList();
$dll->push('Never');
$dll->push('gonna');
$dll->push('give');
// at pos 0, shift everything up
$dll->add(0, 'you');
// Should be the end
$dll->add(4, 'up');
// Somewhere in the middle
$dll->add(2, 'let');
try {
    // Key 12 is unaccessible
    $dll->add(12, 'down');
} catch (OutOfRangeException $e) {
    echo $e->getMessage(), PHP_EOL;
}
foreach ($dll as $key => $val) {
    echo $key . '=>' . $val, PHP_EOL;
}
echo "count(): " . $dll->count(), PHP_EOL;
echo "top(): " . $dll->top(), PHP_EOL;
echo "bottom(): " . $dll->bottom(), PHP_EOL;
<?php

$dll = new SplDoublyLinkedList();
var_dump($dll->add(2));