函式名稱:SplDoublyLinkedList::rewind()
函式描述:該函式用於將雙向連結串列的內部指標指向連結串列的第一個元素。
適用版本:該函式在 PHP 5 和 PHP 7 中均可用。
語法:public SplDoublyLinkedList::rewind(): void
返回值:該函式沒有返回值。
示例:
// 建立一個雙向連結串列物件
$list = new SplDoublyLinkedList();
// 向連結串列中新增元素
$list->push('apple');
$list->push('banana');
$list->push('orange');
// 將內部指標指向連結串列的第一個元素
$list->rewind();
// 獲取當前指標指向的元素
$current = $list->current();
echo $current; // 輸出:apple
在上面的示例中,我們建立了一個空的雙向連結串列物件 $list
,然後使用 push()
函式向連結串列中新增了三個元素:apple、banana 和 orange。接下來,我們使用 rewind()
函式將連結串列的內部指標指向第一個元素。然後,透過 current()
函式獲取當前指標指向的元素,並將其儲存在變數 $current
中。最後,我們輸出變數 $current
的值,即連結串列的第一個元素:apple。