1. Double-linked list spldoublylinkedlist
Structure
Class definition:
Spldoublylinkedlist implements Iterator, arrayaccess, countable {/* method */public __construct (void)//constructor Publi c void Add (mixed $index, mixed $newval)//Add a value at a specific location, the value of the original position is back to public mixed bottom (void)//Returns the list header value public int count (void)//list depth public mixed current (void)//present pointer node value public int getiteratormode (void)//Get list iteration mode, 0 for linked list, It_mode_lifo (Stack style) Spldoublylinkedlist::it_mode_fifo (Queue style) public bool IsEmpty (void)//Determine if the list is empty public mixed key (void)//key value p for the current node ublic void Next (void)//pointer down public bool Offsetexists (mixed $index)//Determine if node exists (via key value)) Public mixed Offsetget (mi Xed $index)//Get node value public void Offsetset (mixed $index, mixed $newval)//Modify node value public void Offsetunset (mixed $ Index)//destroys the specified node, does not affect the current node public mixed pop (void)//delete chain tail link tail public void prev (void)//pointer migration public void push (mixed $val UE)//chain end insert public void rewind (void)//pointer initialization public string serialize (void)//Serialize list for character public void Setiteratormode ( int $mode )//Set Traverse mode public mixed shift (void) Delete link first public mixed top (void)//link footer public void unserialize (string $serialized)/ /deserialization store public void unshift (mixed $value)//Chain header interpolation public bool valid (void)//Determine if the list is valid}
Test code:
<?php//the call function out ($name) {Global $doub of the parameterless type method, if (Is_array ($name)) {foreach ($name as $value) {echo "$value is:". $doub $value (). Php_eol;}} Else{echo "$name is:". $doub, $name (). Php_eol;} return;} $echo = Array (); $doub = new Spldoublylinkedlist (); $doub->push (1); $doub->push (2);//Insert value from tail node $doub->unshift (9) ;//Insert $doub->push (4) from the first node, $doub->push (5), $doub->push (6); $doub->push (7); $doub->push (8); $doub Push (one), $doub->pop ();//delete tail node $doub->shift ();//Delete First node $doub->rewind (); Initializes the current pointer Print_r ($doub); $doub->add (1,10);//Inserts a value Print_r ($doub) at a specific position of 1; $echo = [' count ', ' bottom ', ' top ', ' Getiteratormode ', ' serialize ', ' current ', ' Next ', ' Next ', ' Next ', ' Next ', ' current ', ' prev ', ' current ', ' IsEmpty '];out ($ echo); $doub->offsetset (3, ');//$doub->offsetunset (1);p rint_r ($doub); Out ([' current ', ' valid ']);
2. Stack Splstack
Structure:
Stack inherits all the methods of the doubly linked list
<?php$stack = new Splstack (); $stack->push ("Hello"); Echo ' stack pops is: '. $stack->pop (). Php_eol;
3. Queue Splqueue
Structure diagram:
Inherits all the methods of the doubly linked list
Two additional methods were added
Mixed dequeue (void)//out of queue void Enqueue (mixed $value )//into queue
<?php$queue = new Splqueue (); $queue->enqueue ("queue");//Queue $queue->enqueue ("second"); Echo ' Outbound data is '. $queue- >dequeue ();//Team Queue
4. Heap Splheap
The heap is a completely binary tree, and the node value is larger than the value of the child (big Top heap) or smaller than the value of the child (small top heap)
Large Top pile structure:
Class definition:
Abstract Splheap implements Iterator , countable {/* method */public __construct (void) abstract protected int compare (mixed $value 1 , mixed $value 2 )//abstract method, need to be implemented in their own class, by comparison decide whether to be big top heap or small top heap public int count (void) public mixed current (void) Public mixed extract (void) public void Insert (mixed $value ) public bool IsEmpty (voi d) Public mixed key (void) public void next (void) public void recoverfromcorruption (void) public void Rewind (void) public mixed top (void) public bool valid (void)}
Heap becomes empty after using foreach on heap (no data in heap)
Test code:
<?phpclass Mysimpleheap extends splheap{ //compare () method is used to compare the size of two elements, absolutely their position in the heap public function Compare ($value 1, $value 2) { return ($value 1-$value 2);//Large Top heap, if return $value2-$value 1 is a small top heap }} $obj = new Mysimplehe AP (); $obj->insert (4);//inserting data into the heap $obj->insert (8); $obj->insert (1); $obj->insert (0); Echo ' top is: '. $obj->top (). Php_eol; 8echo ' count is: '. $obj->count (). Php_eol; 4$obj->insert (6); $obj->insert (7);p Rint_r ($obj); Echo ' extract: '. $obj->extract (). php_eol;//extracts the top node from the heap while removing print_r ($obj), $obj->recoverfromcorruption (), and/or recovering foreach from the unordered heap ($obj as $number) {echo ' = ‘. $number. Php_eol;} Print_r ($obj);//print out the heap without data because foreach is used on the heap
Big Top heap: splmaxheap, small top heap splminheap inherit Splheap class, turn Compar into private method
<?php$obj = new Splmaxheap (); $obj->insert (4); $obj->insert (8); $obj->insert (1); $obj->insert (0); Echo ' /***** Big Top heap *****/'; Print_r ($obj); $obj = new Splminheap (); $obj->insert (4); $obj->insert (8); $obj->insert (1); $obj->insert (0) ; Echo '/***** small top heap *****/'; Print_r ($obj);
In addition, there are priority queues, fixed-length arrays, object storage and other structures
PHP SPL Data structure