SPL data structure 1-bidirectional linked list, stack, queue

Time:2021-7-29

In PHP, the SPL extension provides some common data structures for us to use. Only because there is a universal array in PHP, there will be less understanding of data structures and the use of related data structures. Some data structures and usage instructions provided in SPL are sorted out below, so that we can have a certain understanding of relevant data structures and basic usage.
This article will introduce bidirectional linked lists, stacks and queues.

Bidirectional linked list

The following is the explanation of the two-way linked list in Baidu Encyclopedia

Bidirectional linked list, also known as double linked list, is a kind of linked list. There are two pointers in each data node, pointing to the direct successor and direct precursor respectively. Therefore, starting from any node in the two-way linked list, you can easily access its predecessor node and successor node. Generally, we construct a two-way circular linked list.

In PHP SPL, spldoublylinkedlist is provided to implement two-way lists. The following code lists some common functions

$list = new SplDoublyLinkedList();
$list->push('a');
$list->push('b');
$list->push('c');
$list->push('d');

Echo "linked list length:". Count ($list). PHP_ EOL;

$list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO);
echo "FIFO :\n";
for ($list->rewind(); $list->valid(); $list->next()) {
    echo $list->current() . "\n";
}

$list->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO);
echo "FILO :\n";
for ($list->rewind(); $list->valid(); $list->next()) {
    echo $list->current() . "\n";
}

//Common methods of bidirectional linked list
Echo "linked list header element:". $list - > bottom(). PHP_ EOL;
Echo "end element of linked list:". $list - > pop(). PHP_ EOL;

$list - > push ("push an element at the end of the linked list");

//Pop out an element from the end of the linked list
Echo "elements in pop linked list:". $list - > pop(). PHP_ EOL;

//Insert elements at the head of the linked list
$list->unshift("first");
//Delete elements at the head of the linked list
$list->shift();

//The current node element is null
Echo "current node element:". $list - > current(). PHP_ EOL;
$list->rewind();    //  Reset the current pointer to the end of the linked list
Echo "node element after reset:". $list - > current(). PHP_ EOL;

//Note that the value obtained by pre next is associated with the current linklist mode
$list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO);
$list->prev();
Echo "current node element:". $list - > current(). PHP_ EOL;
$list->next();
Echo "current node element:". $list - > current(). PHP_ EOL;

Stack

Stack is a common data structure. The feature is that data can only be inserted and deleted at one end (called top of stack). That is, our common LIFO (last in first out). Splstack class is provided in SPL to implement stack. Splstack is based on the bidirectional list implementation and inherits from the spldoublylinkedlist class

$obj = new SplStack();
$obj->push(1);
$obj->push('two');
$obj->push(3);

echo "pop: " . $obj->pop() . PHP_EOL;
echo "pop: " . $obj->pop() . PHP_EOL;
echo "pop: " . $obj->pop() . PHP_EOL;

//If you continue to pop, an error will be reported
//echo "pop: ".$obj->pop().PHP_EOL;

$obj->push(111);
$obj->push('222');
$obj->push(333);

$obj->rewind();
while ($obj->valid()) {
    echo $obj->current(), "\n";
    $obj->next();
}

Queue

Queue should be the most used data structure in our development. The well-known message queue is often implemented based on queue. Queues are sometimes called linear tables. The biggest feature is that the splqueue class in FIFO. SPL implements the data structure of queue. The bottom layer is also based on two-way list.

$obj = new SplQueue();
$obj->enqueue('a');
$obj->enqueue('b');
$obj->enqueue('c');

//Out of the team
Echo "out of line:". $obj - > dequeue(). PHP_ EOL;
Echo "out of line:". $obj - > dequeue(). PHP_ EOL;
Echo "out of line:". $obj - > dequeue(). PHP_ EOL;

//When there are elements in the queue and they are out of the queue, an error runtimeException will be reported:
//Echo "out of line:". $obj - > dequeue(). PHP_ EOL;

Next:Heap, Max heap, min heap