PHP如何使用SplDoublyLinkedList add()函数?

2021年3月19日18:21:45 发表评论 799 次浏览

SplDoublyLinkedList::add()function是PHP中的内置函数, 用于在给定索引处添加新值

语法如下:

void SplDoublyLinkedList::add( $index, $newval )

参数:它包含上面提到和下面描述的两个参数:

  • $ index:它保存要在其中插入新元素的索引值。
  • $ newval:它包含要插入或添加的元素。

返回值:它不返回任何值。

下面的程序说明了SplDoublyLinkedList :: add()PHP中的功能:

程序1:

<?php 
  
// Declare an empty SplDoublyLinkedList
$list = new \SplDoublyLinkedList;
  
// Use SplDoublyLinkedList::add() function to 
// add elements to the SplDoublyLinkedList
$list ->add(0, 1); 
  
$list ->add(1, "Geeks" ); 
  
$list ->add(2, "G" ); 
  
$list ->add(3, 10); 
  
print_r( $list ); 
?>

输出如下:

SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 1
            [1] => Geeks
            [2] => G
            [3] => 10
        )

)

程式2:

<?php 
  
// Declare an empty SplDoublyLinkedList
$list = new \SplDoublyLinkedList;
  
// Use SplDoublyLinkedList::add() function to 
// add elements to the SplDoublyLinkedList
$list ->add(0, 30);
$list ->add(1, 20);
$list ->add(2, 30);
$list ->add(3, "Geeks" );
$list ->add(4, 'G' );
  
print_r( $list ); 
?>

输出如下:

SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 30
            [1] => 20
            [2] => 30
            [3] => Geeks
            [4] => G
        )

)

参考: https://www.php.net/manual/en/spldoublylinkedlist.add.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: