php標準函式庫spl之鍊錶,堆疊,佇列

WBOY
發布: 2016-08-08 09:18:59
原創
1364 人瀏覽過

雙向鍊錶類別:SplDoublyLinkedList

1. 增刪節點的方法

push:向鍊錶的尾部插入一個節點
pop:取得鍊錶中的尾部節點,並且從鍊錶中刪除這個節點;操作不會改變目前指標的位置
unshift:將一個節點插入鍊錶的頭部
shift:刪除一個鍊錶頭部節點

2. 指標操作方法

rewind:使鍊錶的目前指標指向鍊錶的頭部(即bottom)
current:取得鍊錶目前節點指標指向的元素,必須在呼叫之前先呼叫rewind。當指向的節點被刪除之後,會指向一個空節點
next:讓鍊錶目前的指標指向下一個節點,curent的回傳值隨之改變
bottom:取得鍊錶頭部元素,目前指針位置不變
top:取得鍊錶尾部元素,目前指標位置不變

3.其他方法(用法見堆疊類別)

valid:檢查鍊錶中是否還存在節點,循環輸出時,可以用作判斷
count:統計鍊錶中節點的個數
key:傳回目前節點的鍵值
offsetSet :設定指定鍵的值,注意:如鍵為0,在鍊錶中0指向頭部即bottom,在堆疊中指向棧頂。
offunset:註銷指定鍵的值

<code><span><?</span>php
<span>/**
 * Created by 马廷广
 * User: 马廷广
 * Date: 2015/8/5
 * Time: 10:52
 */</span><span>$obj</span><span>=</span><span>new</span> SplDoublyLinkedList();
<span>$obj</span><span>-></span>push(<span>'b'</span>);
<span>$obj</span><span>-></span>push(<span>'c'</span>);
<span>$obj</span><span>-></span>unshift(<span>'a'</span>);
var_dump(<span>$obj</span>);
<span>/*   array(3) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
  }
)
*/</span><span>$obj</span><span>-></span>rewind();
var_dump(<span>$obj</span><span>-></span>current());<span>//string(1) "a"</span><span>$obj</span><span>-></span>next();
var_dump(<span>$obj</span><span>-></span>current());<span>//string(1) "a"</span>
var_dump(<span>$obj</span><span>-></span>bottom());<span>//string(1) "a"</span>
var_dump(<span>$obj</span><span>-></span>top());<span>//string(1) "c"</span>
var_dump(<span>$obj</span><span>-></span>pop());<span>//string(1) "c"</span>
var_dump(<span>$obj</span>);
<span>/*
*  array(2) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
  }
*/</span>
var_dump(<span>$obj</span><span>-></span>shift());<span>//string(1) "a"</span>
var_dump(<span>$obj</span>);
<span>/*
* array(1) {
    [0]=>
    string(1) "b"
  }
*/</span></code>
登入後複製

堆疊類別:繼承自SplDoublyLinkedList類別的SplStack類別

原理:堆疊類別的底層是由堆疊實現的,堆疊是一種先進後出的資料結構,所以SplStack類別的一些繼承自SplDoublyLinkedList類別的方法有一些理解上的不同,如rewind方法,spl使用rewind方法後指標會指向圖中的棧頂,push和pop操作的是棧頂元素,unshift和shift操作的是棧底元素

<code><span><?</span>php
<span>/**
 * Created by 马廷广
 * User: 马廷广
 * Date: 2015/8/5
 * Time: 11:47
 */</span><span>$stack</span><span>=</span><span>new</span> SplStack();
<span>$stack</span><span>-></span>push(<span>'a'</span>);
<span>$stack</span><span>-></span>push(<span>'b'</span>);
<span>$stack</span><span>-></span>push(<span>'c'</span>);
echo <span>$stack</span><span>-></span>count();<span>//3</span><span>$stack</span><span>-></span>rewind();
echo <span>$stack</span><span>-></span>current();<span>//c</span><span>$stack</span><span>-></span>offsetSet(<span>0</span>,<span>'d'</span>);<span>//offsetSet中0指向的是图中的栈顶,由栈顶向下递增1,2,3,4</span><span>while</span>(<span>$stack</span><span>-></span>valid()){
    echo <span>$stack</span><span>-></span>key()<span>.</span><span>"->"</span><span>.</span><span>$stack</span><span>-></span>current();
    <span>$stack</span><span>-></span>next();
}
<span>/*2->d
1->b
0->a
*/</span></code>
登入後複製

隊列類別:繼承自SplDoublyLinkedList類別的SplQueue類別

enqueue:進入佇列
dequeue:退出隊列
隊列類別的rewind,offsetSet等方法類似鍊錶

<code><?php
/**
 * Created <span>by</span> 马廷广
 * <span>User</span>: 马廷广
 * <span>Date</span>: <span>2015</span>/<span>8</span>/<span>5</span>
 * <span>Time</span>: <span>12</span>:<span>36</span>
 */

$obj = <span>new</span> SplQueue();
$obj->enqueue(<span>'a'</span>);
$obj->enqueue(<span>'b'</span>);
$obj->enqueue(<span>'c'</span>);
var_dump($obj);
/*    [<span>0</span>]<span>=></span>
    string(<span>1</span>) <span>"a"</span>
    [<span>1</span>]<span>=></span>
    string(<span>1</span>) <span>"b"</span>
    [<span>2</span>]<span>=></span>
    string(<span>1</span>) <span>"c"</span>
  }
*/
$obj->unshift(<span>"d"</span>);
$obj->push(<span>'e'</span>);
var_dump($obj);
/*
*  array(<span>5</span>) {
    [<span>0</span>]<span>=></span>
    string(<span>1</span>) <span>"d"</span>
    [<span>1</span>]<span>=></span>
    string(<span>1</span>) <span>"a"</span>
    [<span>2</span>]<span>=></span>
    string(<span>1</span>) <span>"b"</span>
    [<span>3</span>]<span>=></span>
    string(<span>1</span>) <span>"c"</span>
    [<span>4</span>]<span>=></span>
    string(<span>1</span>) <span>"e"</span>
  }
*/
$obj->rewind();
echo $obj->current();<span>//</span>d
$obj->offsetSet(<span>0</span>,<span>'h'</span>);
var_dump($obj);
/*
* array(<span>5</span>) {
    [<span>0</span>]<span>=></span>
    string(<span>1</span>) <span>"h"</span>
    [<span>1</span>]<span>=></span>
    string(<span>1</span>) <span>"a"</span>
    [<span>2</span>]<span>=></span>
    string(<span>1</span>) <span>"b"</span>
    [<span>3</span>]<span>=></span>
    string(<span>1</span>) <span>"c"</span>
    [<span>4</span>]<span>=></span>
    string(<span>1</span>) <span>"e"</span>
  }
*/</code>
登入後複製

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

以上就介紹了php標準庫spl之鍊錶,堆疊,隊列,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!