From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Doubly linked lists, stacks, and queues

Doubly linked lists, stacks, and queues - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Doubly linked lists, stacks, and queues

Although iterators were the initial focus of the standard PHP library, several useful data structures were added in PHP 5.3, including doubly linked lists, stacks, and queues. A linked list is similar to a PHP array, but it differs in that each element knows about its neighbor. In a singly linked list, each time an element is added, it creates a link to the one preceding it. In a doubly linked list which is what SPL has implemented, the links go both ways. So each element has a link to the preceding and following elements. To start a list, create an instance of the SplDoublyLinkedList class. You can add elements to the list using bracket notation in exactly the same way as an array. Alternatively, use the object push method, passing the value as an argument to the method. Keep building the list by adding new elements on the end, using either push or bracket notation. The first element is referred to as the bottom of the list. The most recent element is known as the top. You can get…

Contents