PHP中的SplDoublyLinkedList

A. 双链表 包含指向下一个和上一个节点的链接的列表。双链表(DLL)包含一个额外的指针,通常称为前一个指针,以及下一个指针和数据,它们位于单链表中。与单链表不同,双链表只允许单向遍历,而双链表允许双向遍历。

null

图片[1]-PHP中的SplDoublyLinkedList-yiteyi-C++库

以下是理解双链表概念的重要术语:

  • 链接 :链表的每个节点都可以存储指向另一个名为 链接 .
  • 下一个 :链表的每个节点都包含指向下一个名为 下一个 .
  • :链表的每个节点都包含一个指向前一个名为 .

你可以参考关于 双链表(简介) 详细了解双链接列表。

这个 SplDoublyLinkedList 类是一个PHP库类,它提供PHP中双链表的主要功能。

下面是SplDoublyLinkedList类的一些基本函数:

  • 推(): 此函数用于在双链接列表的末尾插入新节点。 语法 :
    list_name.push(value);
    

    这将推动 价值 在列表的末尾 列出你的名字 .

  • pop(): 此函数用于从双链表末尾删除节点。 语法 :
    list_name.pop()
    

    这将从列表中删除最后一个节点 列出你的名字 .

  • top(): 此函数用于拾取双链接列表末尾的节点。 语法 :
    list_name.top()
    

    这将返回列表中的最后一个节点 列出你的名字 .

  • 底部() 此函数用于拾取双链接列表开头的节点。 语法 :
    list_name.bottom()
    

    这将返回列表开头的节点 列出你的名字 .

  • 添加(): 此函数用于在双链接列表中的指定索引处插入新值,这也会导致该索引处的前一个值(以及所有后续值)在列表中向上移动。 语法 :
    list_name.add('index', "value");
    

    这将增加 价值 处于位置 指数 在名单上 列出你的名字 .

  • count(): 此函数用于计算给定双链表中存在的元素数。 语法 :
    list.count()
    

    这将返回列表中元素总数的计数 列出你的名字 .

下面的程序演示了创建和使用双链接列表的上述功能: 节目:

<?php
// Program to implement basic PHP functions
// for doubly linked list
// Instantiating an object of class SplDoublyLinkedList
$dlist = new SplDoublyLinkedList();
// Inserting elements at the end of the list
$dlist ->push( 'geeks' );
$dlist ->push( 'for' );
$dlist ->push( 'practice' );
// Displaying the list
echo "Original List : " ;
for ( $dlist -> rewind (); $dlist ->valid(); $dlist ->next()) {
echo $dlist ->current(). " " ;
}
// Deleting element from the end of the list
$dlist ->pop();
// Adding a new element at specific index
// Add 'code' at index 2
$dlist ->add(2, "code" );
// Displaying the updated list
echo "Updated List : " ;
for ( $dlist -> rewind (); $dlist ->valid(); $dlist ->next()) {
echo $dlist ->current(). " " ;
}
// Printing the count of nodes
echo "Count = " . $dlist -> count () . "" ;
// Printing the node at top of the list
echo "Top = " . $dlist ->top() . "" ;
// Printing the node at bottom of the list
echo "Bottom = " . $dlist ->bottom() . "" ;
?>


输出:

Original List : geeks for practice 

Updated List: geeks for code 

Count = 3
Top = code
Bottom = geeks
© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享