Rumah  >  Artikel  >  php教程  >  SPL标准库常用的数据结构

SPL标准库常用的数据结构

WBOY
WBOYasal
2016-09-14 09:23:591083semak imbas

栈数据结构

<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>

 

队列数据结构

<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>

 

 

堆数据结构

<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>

 

固定长度的数组数据结构

<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>

 

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn