Home > php教程 > php手册 > Commonly used data structures in the SPL standard library

Commonly used data structures in the SPL standard library

WBOY
Release: 2016-09-14 09:23:59
Original
1115 people have browsed it

Stack data structure

<span style="color: #008080;">1</span> <span style="color: #800080;">$stack</span> = <span style="color: #0000ff;">new</span> SplStack();    <span style="color: #008000;">//</span><span style="color: #008000;">栈数据结构->先进后出    </span>
<span style="color: #008080;">2 </span><span style="color: #800080;">$stack</span>->push('data1');      <span style="color: #008000;">//</span><span style="color: #008000;">入栈</span>
<span style="color: #008080;">3</span> <span style="color: #800080;">$stack</span>->push('data2');      <span style="color: #008000;">//</span><span style="color: #008000;">入栈</span>
<span style="color: #008080;">4</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$stack</span>->pop();         <span style="color: #008000;">//</span><span style="color: #008000;">出栈->data2</span>
<span style="color: #008080;">5</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$stack</span>->pop();         <span style="color: #008000;">//</span><span style="color: #008000;">出栈->data1</span>
Copy after login

Queue data structure

<span style="color: #008080;">1</span> <span style="color: #800080;">$queue</span> = <span style="color: #0000ff;">new</span> SplQueue();    <span style="color: #008000;">//</span><span style="color: #008000;">队列数据结构->先进先出</span>
<span style="color: #008080;">2</span> <span style="color: #800080;">$queue</span>->enqueue('data1');    <span style="color: #008000;">//</span><span style="color: #008000;">入列</span>
<span style="color: #008080;">3</span> <span style="color: #800080;">$queue</span>->enqueue('data2');    <span style="color: #008000;">//</span><span style="color: #008000;">入列</span>
<span style="color: #008080;">4</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$queue</span>->dequeue();    <span style="color: #008000;">//</span><span style="color: #008000;">出列->data1</span>
<span style="color: #008080;">5</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$queue</span>->dequeue();    <span style="color: #008000;">//</span><span style="color: #008000;">出列->data2</span>
Copy after login

Heap data structure

<span style="color: #008080;">1</span> <span style="color: #800080;">$heap</span> = <span style="color: #0000ff;">new</span> SplMinHeap();    <span style="color: #008000;">//</span><span style="color: #008000;">堆数据结构</span>
<span style="color: #008080;">2</span> <span style="color: #800080;">$heap</span>->insert('data1');        <span style="color: #008000;">//</span><span style="color: #008000;">存放</span>
<span style="color: #008080;">3</span> <span style="color: #800080;">$heap</span>->insert('data2');        <span style="color: #008000;">//</span><span style="color: #008000;">存放</span>
<span style="color: #008080;">4</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$heap</span>-><span style="color: #008080;">extract</span>();        <span style="color: #008000;">//</span><span style="color: #008000;">取出->data1</span>
<span style="color: #008080;">5</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$heap</span>-><span style="color: #008080;">extract</span>();        <span style="color: #008000;">//</span><span style="color: #008000;">取出->data2</span>
Copy after login

Fixed length array data structure

<span style="color: #008080;">1</span> <span style="color: #800080;">$array</span> = <span style="color: #0000ff;">new</span> SplFixedArray(10);    <span style="color: #008000;">//</span><span style="color: #008000;">固定长度的数组数据结构,声明长度为10</span>
<span style="color: #008080;">2</span> <span style="color: #800080;">$array</span>[0] = 0<span style="color: #000000;">;
</span><span style="color: #008080;">3</span> <span style="color: #800080;">$array</span>[9] = 9<span style="color: #000000;">;
</span><span style="color: #008080;">4</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$array</span><span style="color: #000000;">);    
</span><span style="color: #008080;">5</span> <span style="color: #008000;">//</span><span style="color: #008000;">object(SplFixedArray)#1 (10) { [0]=> int(0) [1]=> NULL [2]=> NULL [3]=> NULL [4]=> NULL [5]=> NULL [6]=> NULL [7]=> NULL [8]=> NULL [9]=> int(9) }
</span><span style="color: #008080;">6</span> <span style="color: #008000;">//下标1-8无论有没有使用,都会分配内存空间</span>
Copy after login

source:php.cn
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template