Home > Backend Development > PHP Tutorial > Parsing the PHP standard library SPL data structure

Parsing the PHP standard library SPL data structure

coldplay.xixi
Release: 2023-04-09 15:46:01
forward
5504 people have browsed it

Parsing the PHP standard library SPL data structure

SPL provides doubly linked lists, stacks, queues, heaps, descending heaps, ascending heaps, priority queues, fixed-length arrays, and object containers

Parsing the PHP standard library SPL data structure

SplQueue queue class

In and out, first in, first out

Parsing the PHP standard library SPL data structure
<?php$obj = new SplQueue();//插入一个节点到top位置$obj->enqueue(1);
$obj->enqueue(2);
$obj->enqueue(3);/**
SplQueue Object
(
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)
*/$obj->offsetSet(0,&#39;C&#39;);/**
SplQueue Object
(
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => &#39;C&#39;
            [1] => 2
            [2] => 3
        )

)
*/$obj->rewind();//从队列中返回并删除bottom位置的元素$obj->dequeue();/**
&#39;C&#39;

SplQueue Object
(
    [flags:SplDoublyLinkedList:private] => 4
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 2
            [1] => 3
        )

)
*/复制代码
Copy after login

SplStack stack class

When entering and exiting single-ended, Last in, first out, first in, last out

Parsing the PHP standard library SPL data structure
$obj = new SplStack();/**
SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
        )

)
*///向堆栈放入一个节点到top位置$obj->push(1);
$obj->push(2);
$obj->push(3);/**
SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)
*///堆栈的offset = 0 是Top所在的位置,offset = 1 是top节点靠近bottom位置的相邻节点$obj->offsetSet(0,&#39;C&#39;);/**
SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 1
            [1] => 2
            [2] => &#39;C&#39;
        )

)
*/$obj->offsetSet(1,&#39;B&#39;);/**
SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 1
            [1] => &#39;B&#39;
            [2] => &#39;C&#39;
        )

)
*///堆栈的rewind使指针指向top节点,而双向链表的rewind指向bottom节点$obj->rewind();//堆栈的next是使指针向bottom方向相邻的节点移动一位,而链表是向top方向移动$obj->next();//遍历堆栈$obj->rewind();while($obj->valid()){    echo $obj->key().&#39;=>&#39;.$obj->current();
    $obj->next();
}/**
2=>&#39;C&#39;
1=>&#39;B&#39;
0=>1
*///从堆栈中取出top位置的节点并删除$obj->pop();复制代码
Copy after login

SplDoublyLinkedList Doubly linked list class

Parsing the PHP standard library SPL data structure
$obj = new SplDoublyLinkedList();//SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( ) )//把新的节点数据添加到链表的顶部Top$obj->push(1);
$obj->push(2);
$obj->push(3);//把新的节点数据添加到链表底部bottom$obj->unshift(4);/**
SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 4
            [1] => 1
            [2] => 2
            [3] => 3
        )

)
*///在没有调用rewind()方法之前输出为nullecho &#39;current&#39;.$obj->current();//输出: null//把指针指向bottom节点$obj->rewind();//获取当前指针指向的节点$obj->current();//输出: 4//将执行挪到下一个$obj->next();
$obj->current();//输出: 1$obj->prev();
$obj->current();//输出: 4//越界了$obj->next();
$obj->next();
$obj->next();
$obj->next();
$obj->current();//输出: null//判断当前节点是否有效$obj->valid()//输出: false$obj->prev();
$obj->valid()//输出: true$obj->shift();//把top节点从链表中删除并返回$obj->pop();//输出 3//把bottom节点从链表中删除并返回$obj->shift();//输出 4//返回top节点$obj->top();//返回bottom节点$obj->bottom();复制代码
Copy after login

Thanks If your reading is helpful to you, please follow "CRMEB". There are our open source mall projects and knowledge payment projects on Code Cloud, which are all developed based on PHP vue. You are welcome to use them for learning and research. Follow us to stay in touch!

If you want to learn more about programming, please pay attention to the php training column!

The above is the detailed content of Parsing the PHP standard library SPL data structure. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template